def check_font(): flist = font_manager.findSystemFonts() names = [ font_manager.FontProperties(fname=fname).get_name() for fname in flist ] if not (FONT_NAME in names): font_manager._rebuild()
def getFontStyleList(self, ): from matplotlib import font_manager font_manager._rebuild() # 리스트의 원소(폰트파일의 경로)만큼 반복 fontDict = dict({}) for v in font_manager.findSystemFonts(fontpaths=None, fontext='ttf'): try: # 각 폰트파일의 경로를 사용하여 폰트 속성 객체 얻기 fprop = font_manager.FontProperties(fname=v) # 폰트 속성중 이름과 파일 경로를 딕셔러리로 구성하여 리스트에 추가. fontDict[fprop.get_name()] = fprop.get_file() except: continue fontDict = dict(sorted(fontDict.items())) #sort key newFontDict = dict({}) for key in fontDict.keys(): newFontDict["""<span style="font-family:""" + key + """;">""" + key + "</span>"] = fontDict[ key] #apply font style on display fontDict = newFontDict #<span style="font-family:Ami R;">Ami R</span> fontDict["NotoSans"] = "./font/NotoSansKR-Regular.otf" return newFontDict
def chinese_setting( url="http://cnbj1.fds.api.xiaomi.com/browser-algo-nanjing/data/SimHei.ttf" ): """ :param url: SimHei字体下载链接 :return: """ print('开始设置中文...') matplotlibrc_path = Path(matplotlib.matplotlib_fname()) ttf_path = matplotlibrc_path.parent.__str__() + '/fonts/ttf' ttf_url = 'https://raw.githubusercontent.com/Jie-Yuan/Jie-Yuan.github.io/master/SimHei.ttf' if url is None else url if list(Path(ttf_path).glob('SimHei.ttf')): pass else: print('下载字体...') os.popen("cd %s && wget %s" % (ttf_path, ttf_url)).read() os.popen("rm -rf ~/.matplotlib/*.cache").read() print('设置字体...') setting1 = 'font.family: sans-serif' setting2 = 'font.sans-serif: SimHei, Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde, sans-serif' setting3 = 'axes.unicode_minus: False' os.system('echo > %s' % matplotlibrc_path) os.system('echo %s >> %s' % (setting1, matplotlibrc_path)) os.system('echo %s >> %s' % (setting2, matplotlibrc_path)) os.system('echo %s >> %s' % (setting3, matplotlibrc_path)) _rebuild() print('请重启kernel测试...')
def test_font_priority(): with rc_context(rc={"font.sans-serif": ["cmmi10", "Bitstream Vera Sans"]}): # force the font manager to rebuild it self _rebuild() font = findfont(FontProperties(family=["sans-serif"])) assert_equal(os.path.basename(font), "cmmi10.ttf") # force it again _rebuild()
def colab_kvis(): subprocess.run(["apt-get", "install", "fonts-nanum*"]) path = '/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf' font = fm.FontProperties(fname=path, size=12) plt.rc('font', family=font.get_name()) fm._rebuild() matplotlib.rcParams['axes.unicode_minus'] = False return print("한글 시각화 준비 완료")
def initialize_mpl_fonts(self): from matplotlib.font_manager import _rebuild; _rebuild() fp = FontProperties(fname=self.font_path) rcParams['font.serif'] = fp.get_name() rcParams['font.family'] = 'serif'
def decode(args: Dict[str, str]): """ Performs decoding on a test set, and save the best-scoring decoding results. If the target gold-standard sentences are given, the function also computes corpus-level BLEU score. @param args (Dict): args from cmd line """ print("load test source sentences from [{}]".format( args['TEST_SOURCE_FILE']), file=sys.stderr) test_data_src = read_corpus(args['TEST_SOURCE_FILE'], source='src') if args['TEST_TARGET_FILE']: print("load test target sentences from [{}]".format( args['TEST_TARGET_FILE']), file=sys.stderr) test_data_tgt = read_corpus(args['TEST_TARGET_FILE'], source='tgt') print("load model from {}".format(args['MODEL_PATH']), file=sys.stderr) model = NMT.load(args['MODEL_PATH']) if args['--cuda']: model = model.to(torch.device("cuda")) hypotheses = beam_search(model, test_data_src, beam_size=int(args['--beam-size']), max_decoding_time_step=int( args['--max-decoding-time-step'])) if args['TEST_TARGET_FILE']: top_hypotheses = [hyps[0] for hyps in hypotheses] bleu_score = compute_corpus_level_bleu_score(test_data_tgt, top_hypotheses) print('Corpus BLEU: {}'.format(bleu_score * 100), file=sys.stderr) with open(args['OUTPUT_FILE'], 'w') as f: for src_sent, hyps in zip(test_data_src, hypotheses): top_hyp = hyps[0] hyp_sent = ' '.join(top_hyp.value) f.write(hyp_sent + '\n') if args['--plot-attention']: plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = [ 'Arial Unicode MS', 'Arial', 'sans-serif' ] from matplotlib.font_manager import _rebuild _rebuild() output_dir = os.path.dirname(args['OUTPUT_FILE']) for idx, (src_sent, hyps) in tqdm(enumerate(zip(test_data_src, hypotheses)), desc='Plot attention', file=sys.stdout): top_hyp = hyps[0] hyp_sent = top_hyp.value hyp_att = top_hyp.attention filename = output_dir + '/att_%d.jpg' % idx plot_attention(hyp_att, src_sent, hyp_sent, filename)
def draw_flight_graph(weighted_edges_list, node_num, weighted_nodes_list=None): G = nx.Graph() G.add_weighted_edges_from(weighted_edges_list, alpha=0.5, edge_color='g') mfm._rebuild() mpb.rcParams['font.family'] = ['Heiti TC'] fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(121) pos = nx.spring_layout(G, k=0.25) node_list, node_sizes = [], [] if weighted_nodes_list != None: for (node, weight) in weighted_nodes_list: node_list.append(node) node_sizes.append(weight) print(len(node_list), len(node_sizes)) #color codes: #FFD94A (bright yellow); #5BC2E7 (water blue), #4364A2 (darker blue), #D59439 (dark yellow) ##E3C25B (dirt yellow) nx.draw_networkx_nodes(G, pos, nodelist=node_list, node_size=node_sizes, node_color='#D59439', alpha=1) nx.draw_networkx_nodes(G, pos, nodelist=['中国'], node_size=500, node_color='#8C1515', alpha=1) else: nx.draw_networkx_nodes(G, pos, node_size=500, node_color=range(1, node_num + 1), alpha=1, cmap=plt.cm.Blues) nx.draw_networkx_nodes(G, pos, nodelist=['中国'], node_size=300, node_color='r', alpha=1) for edge in weighted_edges_list: nx.draw_networkx_edges(G, pos, edgelist=[edge], width=edge[2] * 100, alpha=1, edge_color='#4364A2') nx.draw_networkx_labels(G, pos, font_size=7, font_family='Heiti TC', font_weight='bold') plt.axis('off') plt.show()
def resolve_chinese_problem(): # 中文乱码处理 # https://www.zhihu.com/question/25404709 from matplotlib.font_manager import _rebuild _rebuild() # reload一下 # 指定默认字体 plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['font.family'] = 'sans-serif' # 用来正常显示负号 plt.rcParams['axes.unicode_minus'] = False
def plot_bar(prop, cat_var, freq): mfm._rebuild() # n = len(mfm.fontManager.ttflist) mpb.rcParams['font.family'] = ['Heiti TC'] width = 0.35 fig = plt.figure() ax = fig.add_subplot(111) rect = ax.bar(cat_var, freq, width, color='#4364A2', label=freq) ax.set_ylabel('数量') ax.set_xlabel(prop) ax.set_title(prop + "数量柱状图") autolabel(ax, rect) plt.show()
def getFontStyleList(self, ): font_manager._rebuild() # 리스트의 원소(폰트파일의 경로)만큼 반복 fontDict = dict() for v in font_manager.findSystemFonts(fontpaths=None, fontext='ttf'): try: # 각 폰트파일의 경로를 사용하여 폰트 속성 객체 얻기 fprop = font_manager.FontProperties(fname=v) # 폰트 속성중 이름과 파일 경로를 딕셔러리로 구성하여 리스트에 추가. fontDict[fprop.get_name()] = fprop.get_file() except: continue fontDict["NotoSans"] = "./font/NotoSansKR-Regular.otf" return fontDict
def test_find_ttc(): fp = FontProperties(family=["WenQuanYi Zen Hei"]) if Path(findfont(fp)).name != "wqy-zenhei.ttc": if not os.environ.get("TRAVIS") or sys.platform != "linux": pytest.skip("Font may be missing") # Travis appears to fail to pick up the ttc file sometimes. Try to # rebuild the cache and try again. fm._rebuild() assert Path(findfont(fp)).name == "wqy-zenhei.ttc" fig, ax = plt.subplots() ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp) for fmt in ["raw", "svg", "pdf", "ps"]: fig.savefig(BytesIO(), format=fmt)
def test_find_ttc(): fp = FontProperties(family=["WenQuanYi Zen Hei"]) if Path(findfont(fp)).name != "wqy-zenhei.ttc": # Travis appears to fail to pick up the ttc file sometimes. Try to # rebuild the cache and try again. fm._rebuild() assert Path(findfont(fp)).name == "wqy-zenhei.ttc" fig, ax = plt.subplots() ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp) fig.savefig(BytesIO(), format="raw") fig.savefig(BytesIO(), format="svg") with pytest.raises(RuntimeError): fig.savefig(BytesIO(), format="pdf") with pytest.raises(RuntimeError): fig.savefig(BytesIO(), format="ps")
def test_find_ttc(): fp = FontProperties(family=["WenQuanYi Zen Hei"]) if Path(findfont(fp)).name != "wqy-zenhei.ttc": # Travis appears to fail to pick up the ttc file sometimes. Try to # rebuild the cache and try again. fm._rebuild() assert Path(findfont(fp)).name == "wqy-zenhei.ttc" fig, ax = plt.subplots() ax.text(.5, .5, "\N{KANGXI RADICAL DRAGON}", fontproperties=fp) fig.savefig(BytesIO(), format="raw") fig.savefig(BytesIO(), format="svg") with pytest.raises(RuntimeError): fig.savefig(BytesIO(), format="pdf") with pytest.raises(RuntimeError): fig.savefig(BytesIO(), format="ps")
def plot_with_labels(low_dim_embs, labels, filename): assert low_dim_embs.shape[0] >= len( labels), 'More labels than embeddings' _rebuild() # reload一下 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 plt.figure(figsize=(18, 18)) # in inches for i, label in enumerate(labels): x, y = low_dim_embs[i, :] plt.scatter(x, y) plt.annotate(label, xy=(x, y), xytext=(5, 2), textcoords='offset points', ha='right', va='bottom') plt.savefig(filename)
def setup(): ''' Set up. ''' plt.close('all') original_units_registry = matplotlib.units.registry.copy() original_settings = matplotlib.rcParams.copy() ver = easypyplot.util.matplotlib_version_tuple() # Setup routine introduced from 1.5. if ver >= (1, 5): matplotlib.testing.setup() # Style name has changed over matplotlib versions. # See changes from <matplotlib repo>/lib/matplotlib/testing/conftest.py:mpl_test_settings() if ver >= (3, 2): matplotlib.style.use(['classic', '_classic_test_patch']) elif ver >= (2, 0): matplotlib.style.use('_classic_test') elif ver >= (1, 5): matplotlib.style.use('classic') # Times bold bug. # https://stackoverflow.com/questions/33955900 # https://github.com/matplotlib/matplotlib/issues/5574 # # The proposed solution simply removes `'roman'` from `weight_dict`, which # may cause `KeyError` when `weight_dict['roman']` is used in other places. # # Instead we order `'roman'` after `'bold'`, so when matching weights, # "Times New Roman Bold" will first match `'bold'` and get its weight. # # Also we need to update the shortcut module method `findfont`. It may # already be imported in other modules, so instead of update its value, we # also need to update the font lists of the previous font manager, so that # those already imported can also use the new font lists. if ver >= (2, 0): # Order `'bold'` before `'roman'` in the weight list. roman_weight = mlpfm.weight_dict.pop('roman', None) mlpfm.weight_dict = OrderedDict(mlpfm.weight_dict) if roman_weight is not None: mlpfm.weight_dict['roman'] = roman_weight # Rebuild font manager and update existing font lists. fm = mlpfm.fontManager mlpfm._rebuild() # pylint: disable=protected-access mlpfm.findfont = mlpfm.fontManager.findfont fm.ttflist = mlpfm.fontManager.ttflist fm.afmlist = mlpfm.fontManager.afmlist return original_units_registry, original_settings
def visualize2(directory, n): tag_list = [] track_list = [] for category in ['genre', 'instrument', 'mood_theme']: tsv_file = os.path.join(directory, category + '.tsv') data = pd.read_csv(tsv_file, delimiter='\t') data = data.sort_values(by=['tracks'], ascending=False) data = data[:n] tag_list += list(data['tag']) track_list += list(data['tracks']) fig = plt.figure(figsize=(12, 2.5)) plt.style.use('seaborn-whitegrid') font_manager._rebuild() rcParams['font.family'] = 'serif' rcParams['font.serif'] = 'Times' plt.grid(False) plt.ylabel('# of tracks') plt.xlim([-1, 60]) plt.ylim([0, 18000]) for i, color in enumerate(['c', 'm', 'y']): indices = np.arange(n * i, n * (i + 1)) plt.bar(indices, np.array(track_list)[indices], align='center') for i in [0, 20, 40, 59]: plt.text(i, track_list[i] + 300, track_list[i], fontsize=8, horizontalalignment='center') plt.xticks(np.arange(len(tag_list)), tag_list, rotation='vertical') ylabels = np.arange(4, 20, 4) plt.yticks(ylabels * 1000, ['{}k'.format(ylabel) for ylabel in ylabels]) plt.subplots_adjust(bottom=0.4) output_file = os.path.join(directory, 'top{}.pdf'.format(n)) plt.savefig(output_file, bbox_inches='tight') # plt.show() plt.close()
def set_KRFONT(KRFONT: str): system = platform.system() if system == 'Windows' or 'Linux': if system == 'Windows': datapath = os.getcwd() + '\\' imagepath = datapath + 'images\\' # ttf 폰트 전체개수 font_list[:10] f = [f.name for f in fm.fontManager.ttflist] f[:10] [(f.name, f.fname) for f in fm.fontManager.ttflist if 'Nanum' in f.name] path = 'C:\\Windows\\Fonts\\NanumBarunGothic.ttf' elif system == 'Linux': datapath = os.getcwd() + '//' imagepath = datapath + 'images//' # !apt-get update -qq # !apt-get install fonts-nanum* -qq path = '/usr/share/fonts/truetype/nanum/NanumGothic.ttf' KRFONT = fm.FontProperties(fname=path, size=10).get_name() fm._rebuild() mpl.rcParams['axes.unicode_minus'] = False KRFONT = fm.FontProperties(fname=path, size=10).get_name() add_KRFONT(KRFONT) print(f"# matplotlib 한글 사용 가능: {KRFONT}") else: sys.exit('ERROR: Sorry, my code has compatibility with Windows and Linux only.') return KRFONT
def get_font_family(): """ 시스템 환경에 따른 기본 폰트명을 반환하는 함수 """ import platform system_name = platform.system() # colab 사용자는 system_name이 'Linux'로 확인 if system_name == "Darwin": font_family = "AppleGothic" elif system_name == "Windows": font_family = "Malgun Gothic" else: # Linux # colab에서는 runtime을 <꼭> 재시작 해야합니다. # 런타임을 재시작 하지 않고 폰트 설치를 하면 기본 설정 폰트가 로드되어 한글이 깨집니다. import matplotlib.font_manager as fm fontpath = '/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf' font = fm.FontProperties(fname=fontpath, size=9) fm._rebuild() font_family = "NanumBarunGothic" return font_family
def test(): _rebuild()
def printKickDistribution(sp: la_class.ServerParam, feat: la_class.Feature, path: str) -> None: import matplotlib #matplotlib.use('Agg') from mpl_toolkits.mplot3d import Axes3D from matplotlib.patches import Circle import mpl_toolkits.mplot3d.art3d as art3d from matplotlib import pyplot as plt import matplotlib.font_manager as fm xlim: cython.double = sp.pitch_length / 2 + 5.0 ylim: cython.double = sp.pitch_width / 2 + 5.0 fm._rebuild() #plt.rc('font', family='Times New Roman') #plt.rcParams['font.family'] = 'Times New Roman' fig = plt.figure(figsize=(10.5, 6.8)) plt.rcParams['font.family'] = 'Times New Roman' plt.rcParams['ps.useafm'] = True plt.rcParams['pdf.use14corefonts'] = True plt.rcParams['text.usetex'] = True ax = fig.add_subplot(111, projection='3d', azim=240) ax.set_xlabel("{\it x}", fontsize=32, labelpad=20) ax.set_ylabel("{\it y}", fontsize=32, labelpad=20) ax.set_zlabel("{\it distance}", fontsize=32, labelpad=20) ax.set_xlim(-xlim, xlim) ax.set_ylim(ylim, -ylim) ax.tick_params(labelsize=32, pad=10) # plot soccer fields ax.plot3D([sp.goal_line_l, -sp.penalty_area_x], [-sp.penalty_area_y, -sp.penalty_area_y], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_l, -sp.penalty_area_x], [sp.penalty_area_y, sp.penalty_area_y], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_r, sp.penalty_area_x], [-sp.penalty_area_y, -sp.penalty_area_y], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_r, sp.penalty_area_x], [sp.penalty_area_y, sp.penalty_area_y], [0, 0], color='g', linewidth=4) ax.plot3D([-sp.penalty_area_x, -sp.penalty_area_x], [sp.penalty_area_y, -sp.penalty_area_y], [0, 0], color='g', linewidth=4) ax.plot3D([sp.penalty_area_x, sp.penalty_area_x], [sp.penalty_area_y, -sp.penalty_area_y], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_l, sp.goal_line_r], [-sp.pitch_width / 2, -sp.pitch_width / 2], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_l, sp.goal_line_r], [sp.pitch_width / 2, sp.pitch_width / 2], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_l, sp.goal_line_l], [-sp.pitch_width / 2, sp.pitch_width / 2], [0, 0], color='g', linewidth=4) ax.plot3D([sp.goal_line_r, sp.goal_line_r], [-sp.pitch_width / 2, sp.pitch_width / 2], [0, 0], color='g', linewidth=4) plt.plot([0, 0], [-sp.pitch_width / 2, sp.pitch_width / 2], color='g', linewidth=4) p = Circle((0.5, 0.5), 9, ec="g", fc="None", linewidth=4) ax.add_patch(p) art3d.pathpatch_2d_to_3d(p, z=0.0, zdir="z") # plot kick distribution for i in range(len(feat.kick_sequence) - 1): # if not tarminal condition if ( feat.kick_sequence[i][4] != -1 \ and feat.kick_sequence[i+1][4] != 0 ): ax.plot3D([feat.kick_sequence[i][1], feat.kick_sequence[i][1]], [feat.kick_sequence[i][2], feat.kick_sequence[i][2]], [ 0, lib.calcDistC(feat.kick_sequence[i][1], feat.kick_sequence[i][2], feat.kick_sequence[i + 1][1], feat.kick_sequence[i + 1][2]) ], "red") filename: cython.str = path + feat.team_point[0] + "-kick_distribution" extension: list = [".eps", ".pdf", ".png", ".svg"] for e in extension: plt.savefig(filename + e, dpi=300, bbox_inches="tight", transparent=True) plt.show()
def plot_graph(self, title=None, file='graph.png'): from matplotlib.font_manager import _rebuild _rebuild() font_path = '' if platform.system() is 'Windows': # Window의 경우 폰트 경로 font_path = 'C:/Windows/Fonts/malgun.ttf' elif platform.system() is 'Darwin': # for Mac font_path = '/Library/Fonts/AppleGothic.ttf' font_name = fm.FontProperties(fname=font_path).get_name() plt.rc('font', family=font_name) plt.rc('axes', unicode_minus=False) # 그래프에서 마이너스 폰트 깨지는 문제에 대한 대처 mpl.rcParams['axes.unicode_minus'] = False #print('버전: ', mpl.__version__) #print('설치 위치: ', mpl.__file__) #print('설정 위치: ', mpl.get_configdir()) #print('캐시 위치: ', mpl.get_cachedir()) # size, family print('# 설정 되어있는 폰트 사이즈') print(plt.rcParams['font.size']) print('# 설정 되어있는 폰트 글꼴') print(plt.rcParams['font.family']) fig = plt.figure(figsize=(8, 8)) pos = self.centrality_layout() """Conveniently summarize graph visually""" # config parameters edge_min_width = 3 edge_max_width = 12 label_font = 18 node_font = 22 node_alpha = 0.4 edge_alpha = 0.55 edge_cmap = plt.cm.Spectral # Create figure if fig is None: fig, ax = plt.subplots() else: ax = fig.add_subplot(111) fig.subplots_adjust(0, 0, 1) font = FT2Font(font_path) # Plot nodes with size according to count sizes = [] degrees = [] for n, d in self.G.nodes(data=True): sizes.append(d['count']) degrees.append(self.G.degree(n)) sizes = self.rescale_arr(np.array(sizes, dtype=float), 100, 1000) # Compute layout and label edges according to weight pos = nx.spring_layout(self.G) if pos is None else pos labels = {} width = [] for n1, n2, d in self.G.edges(data=True): w = d['weight'] labels[n1, n2] = w width.append(w) width = self.rescale_arr(np.array(width, dtype=float), edge_min_width, edge_max_width) # Draw nx.draw_networkx_nodes(self.G, pos, node_size=sizes, node_color=degrees, alpha=node_alpha) nx.draw_networkx_edges(self.G, pos, width=width, edge_color=width, edge_cmap=edge_cmap, alpha=edge_alpha) nx.draw_networkx_edge_labels(self.G, pos, edge_labels=labels, font_size=label_font) nx.draw_networkx_labels(self.G, pos, font_size=node_font, font_family=font_name, font_weight='bold') if title is not None: ax.set_title(title, fontsize=label_font) ax.set_xticks([]) ax.set_yticks([]) # Mark centrality axes kw = dict(color='k', linestyle='-') cross = [ax.axhline(0, **kw), ax.axvline(self.rad0, **kw)] [l.set_zorder(0) for l in cross] #plt.show() plt.savefig(file)
def main(argv): config = read_parser(argv, Inputs, InputsOpt_Defaults) if config['mode'] == '1x4_two_scale_plot': from matplotlib import font_manager del font_manager.weight_dict['roman'] font_manager._rebuild() plt.rcParams['font.family'] = 'Times New Roman' print('Select Features files') root = Tk() root.withdraw() root.update() Filepaths = filedialog.askopenfilenames() root.destroy() print('Select Feature Condition...') root = Tk() root.withdraw() root.update() filepath2 = filedialog.askopenfilename() root.destroy() mydict2 = pd.read_excel(filepath2, sheetname=config['sheet']) mydict2 = mydict2.to_dict(orient='list') Feature2 = np.array(mydict2[config['feature_cond']]) index_nonan = [] count = 0 for element in Feature2: if not isnan(element): index_nonan.append(count) count +=1 Feature2 = Feature2[index_nonan] if config['feature_cond'] == 'n': Feature2 = Feature2/117.18 fig, ax = plt.subplots(nrows=1, ncols=2) # ax2 = ax.twinx() # count = 0 titles = ['AE-1', 'AE-2', 'AE-3', 'AE-4'] features = ['MAX', 'RMS'] fontsize_big = 18 fontsize_little = 17 fontsize_legend = 14 for k in range(2): count = 0 for filepath in Filepaths: mydict = pd.read_excel(filepath, sheetname=config['sheet']) mydict = mydict.to_dict(orient='list') Feature = mydict[features[k]] Feature = 1000*np.array(Feature)/141.3 if config['n_mov_avg'] != 0: Feature = movil_avg(Feature, config['n_mov_avg']) time = np.array([i*10. for i in range(len(Feature))])/60. time2 = time[index_nonan] ax[k].plot(time, Feature, label=titles[count]) ax[k].set_xlabel('Time [min]', fontsize=fontsize_big) if features[k] == 'RMS': ax[k].set_ylabel('RMS value [mV]', color='k', fontsize=fontsize_big) ax[k].set_ylim(bottom=0, top=0.8) else: ax[k].set_ylabel('Maximum value [mV]', color='k', fontsize=fontsize_big) ax[k].set_ylim(bottom=0, top=40) ax[k].tick_params('y', colors='k') ax[k].legend(fontsize=fontsize_legend, loc='best') ax[k].tick_params(axis='both', labelsize=fontsize_little) ax[k].set_xlim(left=0, right=60) count += 1 ax2 = ax[k].twinx() ax2.set_xlim(left=0, right=60) # ax2.set_xticks([0, 2, 4, 6, 8, 10, 12, 14]) if config['feature_cond'] == 'T': ax2.set_ylim(bottom=25, top=55) ax2.set_ylabel('Temperature [°C]', color='m', fontsize=fontsize_big) elif config['feature_cond'] == 'n': ax2.set_ylabel('Rotational speed [CPM]', color='m', fontsize=fontsize_big) # Feature2 = Feature2/117.18 ax2.set_ylim(bottom=0, top=15) ax2.set_yticks([0, 3, 6, 9, 12, 15]) elif config['feature_cond'] == 'M': ax2.set_ylabel('Torque [Nm]', color='m', fontsize=fontsize_big) # Feature2 = Feature2/117.18 ax2.set_ylim(bottom=0, top=18) ax2.set_yticks([0, 3, 6, 9, 12, 15, 18]) # ax2.set_yticks([0, 3, 6, 9, 12, 15]) # ax2.set_ylabel('Rotational speed [CPM]', color='r', fontsize=15) ax2.plot(time2, Feature2, 'sm') ax2.tick_params('y', colors='m') ax2.tick_params(axis='both', labelsize=fontsize_little) fig.set_size_inches(12, 4.5) plt.subplots_adjust(wspace=0.4, left=0.065, right=0.935, bottom=0.15, top=0.94) # plt.tight_layout() plt.show() elif config['mode'] == 'calculate_correlation': print('Select Features files') root = Tk() root.withdraw() root.update() Filepaths = filedialog.askopenfilenames() root.destroy() print('Select Feature Condition...') root = Tk() root.withdraw() root.update() filepath2 = filedialog.askopenfilename() root.destroy() mydict2 = pd.read_excel(filepath2, sheetname=config['sheet']) mydict2 = mydict2.to_dict(orient='list') Feature2 = np.array(mydict2[config['feature_cond']]) index_nonan = [] count = 0 for element in Feature2: if not isnan(element): index_nonan.append(count) count +=1 Feature2 = Feature2[index_nonan] if config['feature_cond'] == 'n': Feature2 = Feature2/117.18 # ax2 = ax.twinx() mydict2 = {} count = 0 titles = ['AE-1', 'AE-2', 'AE-3', 'AE-4'] features = ['MAX', 'RMS'] for k in range(2): vec = [] for filepath in Filepaths: mydict = pd.read_excel(filepath, sheetname=config['sheet']) mydict = mydict.to_dict(orient='list') Feature = mydict[features[k]] Feature = 1000*np.array(Feature)/141.3 if config['n_mov_avg'] != 0: Feature = movil_avg(Feature, config['n_mov_avg']) time = np.array([i*10. for i in range(len(Feature))])/60. time2 = time[index_nonan] Feature2int = np.interp(time, time2, Feature2) # plt.plot(time, Feature, 'b', time, Feature2int, 'or') # plt.show() print(titles[count], ' = ' , np.corrcoef(Feature, Feature2int)[0][1]) vec.append(np.corrcoef(Feature, Feature2int)[0][1]) mydict2[features[k]] = vec print(mydict2) DataFr = pd.DataFrame(data=mydict2, index=titles) writer = pd.ExcelWriter(config['name'] + '.xlsx') DataFr.to_excel(writer, sheet_name='Correlations') print('Result in Excel table') else: print('unknown mode') sys.exit() return
from pandas import DataFrame from pandas import Series from pandas.core.index import MultiIndex from pandas.core.indexing import convert_to_index_sliceable from performanceanalytics.charts.performance_summary import create_performance_summary from .columns import * from .outcomes import * from ..io.downloader import download_latest_data from ..util.checker import not_empty import dropbox import io # Hangul font setting # noinspection PyProtectedMember font_manager._rebuild() if platform.system() == 'Windows': font_name = font_manager.FontProperties(fname='c:/Windows/Fonts/malgun.ttf').get_name() elif platform.system() == 'Darwin': # OS X font_name = font_manager.FontProperties(fname='/Library/Fonts/AppleGothic.ttf').get_name() else: # Linux fname = '/usr/share/fonts/truetype/nanum/NanumGothicOTF.ttf' if not os.path.isfile(fname): raise ResourceWarning("Please install NanumGothicOTF.ttf for plotting Hangul.") font_name = font_manager.FontProperties(fname=fname).get_name() rc('font', family=font_name) # for fix broken Minus sign matplotlib.rcParams['axes.unicode_minus'] = False PERCENTAGE = 'percentage'
def main(argv): config = read_parser(argv, Inputs, InputsOpt_Defaults) if config['mode'] == 'plot_from_mat': print('Select MAT from DATA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() data = load_signal(filepath, channel=config['channel']) # data = data*(1000/70.8)*(1000/70.8)*1000*1000 # data = data*(1000/141.25)*(1000/141.25)*1000*1000 print('Select MAT from ALPHA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() alpha = load_signal(filepath, channel='alpha') print('Select MAT from FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = load_signal(filepath, channel='f') data = np.reshape(data, (int(len(f) * 2), len(alpha))) data = data[0:int(data.shape[0] / 2), :] data = np.absolute(data) data[:, 0] = 0. fig = plt.figure() if config['projection'] == 'surface': ax = fig.add_subplot(111, projection='3d') alpha, f = np.meshgrid(alpha, f) ax.plot_surface(alpha, f / 1000, data, cmap='plasma') # ax.plot_wireframe(alpha, f/1000, data, color='black') # ax.plot_trisurf(alpha, f/1000, data, cmap='viridis') # elif config['projection'] == 'mesh': # ax = fig.add_subplot(111) # dog = ax.pcolormesh(alpha, f/1000., np.log(data), cmap='inferno', vmax=None) # cbar = fig.colorbar(dog, ax=ax) # cbar.set_label('SCoh [-]', fontsize=13) # cbar.ax.tick_params(axis='both', labelsize=12) # # cbar.ax.set_ylim(bottom=0.000001, top=20) elif config['projection'] == 'mesh': ax = fig.add_subplot(111) # vmax = max_cspectrum(data) # dog = ax.pcolormesh(alpha, f/1000, 10*np.log10(data), cmap='inferno', vmax=None) dog = ax.pcolormesh(alpha, f / 1000, data, cmap='inferno', vmax=None) cbar = fig.colorbar(dog, ax=ax) # cbar.set_label('SCD [mV$^{2}$]', fontsize=13) cbar.set_label('SCoh [-]', fontsize=13) cbar.ax.tick_params(axis='both', labelsize=12) # cbar.ax.set_ylim(bottom=0.000001, top=20) elif config['projection'] == 'save_map': extent_ = [0, np.max(alpha), 0, 500] # ax.contourf(cwtmatr, extent=extent_) # ax.set_ylim(bottom=0 , top=500000) # plt.show() mydict = {'map': data, 'extent': extent_} save_pickle('CycloDensity_simulated_AE_mod_fault_frec.pkl', mydict) sys.exit() ax.set_xlabel('Cyclic Frequency [Hz]', fontsize=13) ax.set_ylabel('Frequency [kHz]', fontsize=13) ax.tick_params(axis='both', labelsize=12) ax.set_title('S-1: AE-3', fontsize=12) # ax.set_xlim(left=0, right=50) # ax.set_ylim(bottom=0, top=500) plt.show() elif config['mode'] == 'plot_from_mat_cyclic': print('Select MAT from DATA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() data = load_signal(filepath, channel=config['channel']) # data = data*(1000/70.8)*(1000/70.8)*1000*1000 # data = data*(1000/141.25)*(1000/141.25)*1000*1000 print('Select MAT from ALPHA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() alpha = load_signal(filepath, channel='alpha') print('Select MAT from FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = load_signal(filepath, channel='f') data = np.reshape(data, (int(len(f) * 2), len(alpha))) data = data[0:int(data.shape[0] / 2), :] data = np.absolute(data) sum = np.zeros(len(data[:, 0])) #sum len 100 for i in range(len(data[0, :])): #i itera de 1 a 100 sum += data[:, i] #data suma a len 100 plt.plot(f / 1000, sum) plt.show() sum = np.zeros(len(data[0, :])) #sum len 100 for i in range(len(data[:, 0])): #i itera de 1 a 100 sum += data[i, :] #data suma a len 100 plt.plot(alpha, sum) plt.show() sys.exit() elif config['mode'] == 'avg_plot_from_mat_cyclic': for i in range(5): print('Select MAT from DATA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() data = load_signal(filepath, channel=config['channel']) # data = data*(1000/70.8)*(1000/70.8)*1000*1000 # data = data*(1000/141.25)*(1000/141.25)*1000*1000 print('Select MAT from ALPHA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() alpha = load_signal(filepath, channel='alpha') print('Select MAT from FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = load_signal(filepath, channel='f') data = np.reshape(data, (int(len(f) * 2), len(alpha))) data = data[0:int(data.shape[0] / 2), :] data = np.absolute(data) sum = np.zeros(len(data[:, 0])) #sum len 100 for i in range(len(data[0, :])): #i itera de 1 a 100 sum += data[:, i] #data suma a len 100 plt.plot(f / 1000, sum) plt.show() sum = np.zeros(len(data[0, :])) #sum len 100 for i in range(len(data[:, 0])): #i itera de 1 a 100 sum += data[i, :] #data suma a len 100 plt.plot(alpha, sum) plt.show() sys.exit() elif config['mode'] == 'comp_plot_from_mat': print('Select MAT from DATA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() data = load_signal(filepath, channel=config['channel']) print('Select MAT from ALPHA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() alpha = load_signal(filepath, channel='alpha') print('Select MAT from FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = load_signal(filepath, channel='f') data = np.reshape(data, (int(len(f) * 2), len(alpha))) data = data[0:int(data.shape[0] / 2), :] data = np.absolute(data) select_alpha = 4.3 - 2 da = alpha[1] - alpha[0] result_alpha_idx = int(round(select_alpha / da)) result_alpha = alpha[result_alpha_idx] plt.plot(f, data[:, result_alpha_idx]) plt.show() print(result_alpha) print(alpha) sys.exit() fig = plt.figure() ax = fig.add_subplot(111, projection='3d') alpha, f = np.meshgrid(alpha, f) ax.plot_surface(alpha, f, data, cmap='plasma') plt.show() elif config['mode'] == 'multi_comp_plot_from_mat': prom = 1 for i in range(prom): print('Select MAT from DATA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() data = load_signal(filepath, channel=config['channel']) # data = data*(1000/70.8)*(1000/70.8)*1000*1000 # data = data*(1000/141.25)*(1000/141.25)*1000*1000 # data = 10*np.log(data) print('Select MAT from ALPHA') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() alpha = load_signal(filepath, channel='alpha') print('Select MAT from FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = load_signal(filepath, channel='f') data = np.reshape(data, (int(len(f) * 2), len(alpha))) data = data[0:int(data.shape[0] / 2), :] data = np.absolute(data) #++++++++TEST RIG # select_alphas = np.array([4.333, 4.333*2, 4.333*3, 4.333*4, 4.333*5, 4.333*6, 4.333*7, 4.333*8, 4.333*9, 4.333*10]) # select_alphas = np.array([312., 624., 936., 1248., 1560.]) # select_alphas = np.array([307.67, 316.33, 303.34, 320.66, 294.68, 329.32, 290.35, 333.65] + [619.67, 628.33, 615.34, 632.66, 606.68, 641.32, 602.35, 645.65] + [931.67, 940.33, 927.34, 944.66, 918.68, 953.32, 914.35, 957.65] + [1243.67, 1252.33, 1239.34, 1256.66, 1230.68, 1265.32, 1226.35, 1269.35] + [1555.67, 1564.33, 1551.34, 1568.66, 1542.68, 1577.32, 1538.35, 1581.65])*1.023077 # select_alphas = [4.333, 4.333*2, 4.333*3, 4.333*4, 4.333*5, 4.333*6, 4.333*7, 4.333*8, 4.333*9, 4.333*10] # select_alphas += [312., 624., 936., 1248., 1560.] # select_alphas += [307.67, 316.33, 303.34, 320.66, 294.68, 329.32, 290.35, 333.65] + [619.67, 628.33, 615.34, 632.66, 606.68, 641.32, 602.35, 645.65] + [931.67, 940.33, 927.34, 944.66, 918.68, 953.32, 914.35, 957.65] + [1243.67, 1252.33, 1239.34, 1256.66, 1230.68, 1265.32, 1226.35, 1269.35] + [1555.67, 1564.33, 1551.34, 1568.66, 1542.68, 1577.32, 1538.35, 1581.65] # select_alphas = np.array(select_alphas) # *1.023077 # #++++++++CWD DAMAGE # # select_alphas = [35.8875, 71.775, 107.6625, 143.55, 179.4375] #fg # select_alphas = [0.3625, 0.725, 1.0875, 1.45, 1.8125, 2.175, 2.5375, 2.9, 3.2625, 3.625] #ffr # # select_alphas = [0.3625] #ffr # #++++++++TURM Schottland DAMAGE # select_alphas = [3.872, 7.744, 11.615, 15.487, 19.359, 23.231, 27.103, 30.974, 34.847, 38.718] #ffr #++++++++BOCHUM NO DAMAGE select_alphas = [ 3.481, 6.961, 10.442, 13.923, 17.403, 20.884, 24.365, 27.485, 31.326, 34.806 ] #ffr da = alpha[1] - alpha[0] result_alpha_idxes = [] if i == 0: PromsComps = np.zeros(len(data[:, 0])) Comps = np.zeros(len(data[:, 0])) for select_alpha in select_alphas: result_alpha_idx = int(round(select_alpha / da)) result_alpha_idxes.append(result_alpha_idx) print(alpha[result_alpha_idx]) Comps = Comps + data[:, result_alpha_idx] PromsComps = PromsComps + Comps PromsComps = PromsComps / prom fig, ax = plt.subplots() ax.plot(f / 1000., PromsComps) ax.set_xlabel('Frequency [kHz]', fontsize=13) # name_ylabel = r'$\Sigma_{i=1}^{10}$ SCD $i f_{c}$ [$\mu$V$^{2}$]' name_ylabel = r'$\Sigma_{i=1}^{10}$ SCoh $i f_{c}$ [-]' ax.set_ylabel(name_ylabel, fontsize=13) ax.tick_params(axis='both', labelsize=12) ax.set_title('B-3: AE-3', fontsize=12) plt.show() if config['save'] == 'ON': # #TEST-RIG # save_pickle('MessQ_MeanDensity_5h_sb_f_g_c.pkl', PromsComps) # save_pickle('MessQ_fDensity_5h_sb_f_g_c.pkl', f) # #CWD-Damage # save_pickle('CWD_Damage_MeanCoherence_10fft.pkl', PromsComps) # save_pickle('CWD_Damage_fCoherence_10ffr.pkl', f) #Schottland-Damage save_pickle('Schottland_Damage_MeanCoherence_10ffr.pkl', PromsComps) save_pickle('Schottland_Damage_fCoherence_10ffr.pkl', f) elif config['mode'] == 'plot_mean': print('Select FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = read_pickle(filepath) print('Select MEAN DATA') root = Tk() root.withdraw() root.update() Filepaths = filedialog.askopenfilenames() root.destroy() data = read_pickle(filepath) names = ['MessA', 'MessG', 'MessI', 'MessL', 'MessN', 'MessQ'] signals = {} for name in names: for filepath in Filepaths: if filepath.find(name) != -1: if name not in signals.keys(): signals[name] = read_pickle(filepath) font_big = 17 font_little = 15 font_label = 13 from matplotlib import font_manager del font_manager.weight_dict['roman'] font_manager._rebuild() plt.rcParams['font.family'] = 'Times New Roman' fig, ax = plt.subplots(ncols=3, nrows=2, sharex=True, sharey=True) plt.subplots_adjust(wspace=0.275, left=0.065, right=0.98, bottom=0.125, top=0.92, hspace=0.55) fig.set_size_inches(14.2, 6.2) ax[0][0].plot(f / 1000., signals['MessA']) ax[0][1].plot(f / 1000., signals['MessG']) ax[0][2].plot(f / 1000., signals['MessI']) ax[1][0].plot(f / 1000., signals['MessL']) ax[1][1].plot(f / 1000., signals['MessN']) ax[1][2].plot(f / 1000., signals['MessQ']) name_ylabel = r'$\Sigma$ SCD [-]' ax[0][0].set_ylabel(name_ylabel, fontsize=font_big) ax[0][1].set_ylabel(name_ylabel, fontsize=font_big) ax[0][2].set_ylabel(name_ylabel, fontsize=font_big) ax[1][0].set_ylabel(name_ylabel, fontsize=font_big) ax[1][1].set_ylabel(name_ylabel, fontsize=font_big) ax[1][2].set_ylabel(name_ylabel, fontsize=font_big) name_xlabel = 'Frequency [kHz]' ax[0][0].set_xlabel(name_xlabel, fontsize=font_big) ax[0][1].set_xlabel(name_xlabel, fontsize=font_big) ax[0][2].set_xlabel(name_xlabel, fontsize=font_big) ax[1][0].set_xlabel(name_xlabel, fontsize=font_big) ax[1][1].set_xlabel(name_xlabel, fontsize=font_big) ax[1][2].set_xlabel(name_xlabel, fontsize=font_big) # ax[0][0].legend(fontsize=font_label, ncol=2, loc='best') # ax[0][1].legend(fontsize=font_label, ncol=2, loc='best') # ax[0][2].legend(fontsize=font_label, ncol=2, loc='best') # ax[1][0].legend(fontsize=font_label, ncol=2, loc='best') # ax[1][1].legend(fontsize=font_label, ncol=2, loc='best') # ax[1][2].legend(fontsize=font_label, ncol=2, loc='best') plt.rcParams['mathtext.fontset'] = 'cm' ax[0][0].set_title('$1^{st}$ MC', fontsize=font_big) ax[0][1].set_title('$2^{nd}$ MC', fontsize=font_big) ax[0][2].set_title('$4^{th}$ MC', fontsize=font_big) ax[1][0].set_title('$7^{th}$ MC', fontsize=font_big) ax[1][1].set_title('$9^{th}$ MC', fontsize=font_big) ax[1][2].set_title('$10^{th}$ MC', fontsize=font_big) ax[0][0].set_xlim(left=0, right=500) ax[0][1].set_xlim(left=0, right=500) ax[0][2].set_xlim(left=0, right=500) ax[1][0].set_xlim(left=0, right=500) ax[1][1].set_xlim(left=0, right=500) ax[1][2].set_xlim(left=0, right=500) valtop = 0.4 ax[0][0].set_ylim(bottom=0, top=valtop) ax[0][1].set_ylim(bottom=0, top=valtop) ax[0][2].set_ylim(bottom=0, top=valtop) ax[1][0].set_ylim(bottom=0, top=valtop) ax[1][1].set_ylim(bottom=0, top=valtop) ax[1][2].set_ylim(bottom=0, top=valtop) # ax[0][0].set_yticks([0, 100, 200, 300, 400, 500]) # ax[0][1].set_yticks([0, 100, 200, 300, 400, 500]) # ax[0][2].set_yticks([0, 100, 200, 300, 400, 500]) # ax[1][0].set_yticks([0, 100, 200, 300, 400, 500]) # ax[1][1].set_yticks([0, 100, 200, 300, 400, 500]) # ax[1][2].set_yticks([0, 100, 200, 300, 400, 500]) ax[0][0].tick_params(axis='both', labelsize=font_little) ax[0][1].tick_params(axis='both', labelsize=font_little) ax[0][2].tick_params(axis='both', labelsize=font_little) ax[1][0].tick_params(axis='both', labelsize=font_little) ax[1][1].tick_params(axis='both', labelsize=font_little) ax[1][2].tick_params(axis='both', labelsize=font_little) for ax_it in ax.flatten(): for tk in ax_it.get_yticklabels(): tk.set_visible(True) for tk in ax_it.get_xticklabels(): tk.set_visible(True) ax_it.yaxis.offsetText.set_visible(True) plt.show() elif config['mode'] == 'plot_mean_multi': print('Select FREQ') root = Tk() root.withdraw() root.update() filepath = filedialog.askopenfilename() root.destroy() f = read_pickle(filepath) print('Select MEAN DATA') root = Tk() root.withdraw() root.update() Filepaths = filedialog.askopenfilenames() root.destroy() data = read_pickle(filepath) names = ['MessA', 'MessG', 'MessI', 'MessL', 'MessN', 'MessQ'] harmonics = ['10h_f_f_r', '5h_f_g', '5h_sb_f_g_c'] signals = {} for name in names: for harmonic in harmonics: for filepath in Filepaths: if filepath.find(name) != -1 and filepath.find( harmonic) != -1: if name not in signals.keys(): signals[name + '_' + harmonic] = read_pickle(filepath) font_big = 17 + 3 font_little = 15 + 3 font_label = 13 + 3 font_caption = 23 + 3 from matplotlib import font_manager del font_manager.weight_dict['roman'] font_manager._rebuild() plt.rcParams['font.family'] = 'Times New Roman' fig, ax = plt.subplots(ncols=3, nrows=2, sharex=True, sharey=True) # plt.subplots_adjust(wspace=0.275, left=0.065, right=0.98, bottom=0.125, top=0.92, hspace=0.55) plt.subplots_adjust(wspace=0.32, left=0.07, right=0.985, bottom=0.135, top=0.94, hspace=0.52) # fig.set_size_inches(14.2, 6.2) fig.set_size_inches(14.2, 7) fig.text(0.053 - 0.015, 0.04, '(d)', fontsize=font_caption) fig.text(0.385 - 0.015, 0.04, '(e)', fontsize=font_caption) fig.text(0.717 - 0.015, 0.04, '(f)', fontsize=font_caption) fig.text(0.053 - 0.015, 0.528, '(a)', fontsize=font_caption) fig.text(0.385 - 0.015, 0.528, '(b)', fontsize=font_caption) fig.text(0.717 - 0.015, 0.528, '(c)', fontsize=font_caption) ax[0][0].plot(f / 1000., signals['MessA_10h_f_f_r'], label='$n f_{c}$') ax[0][0].plot(f / 1000., signals['MessA_5h_f_g'], label='$p f_{m}$') ax[0][0].plot(f / 1000., signals['MessA_5h_sb_f_g_c'], label='$p f_{m} \pm s f_{c}$') ax[0][1].plot(f / 1000., signals['MessG_10h_f_f_r'], label='$n f_{c}$') ax[0][1].plot(f / 1000., signals['MessG_5h_f_g'], label='$p f_{m}$') ax[0][1].plot(f / 1000., signals['MessG_5h_sb_f_g_c'], label='$p f_{m} \pm s f_{c}$') ax[0][2].plot(f / 1000., signals['MessI_10h_f_f_r'], label='$n f_{c}$') ax[0][2].plot(f / 1000., signals['MessI_5h_f_g'], label='$p f_{m}$') ax[0][2].plot(f / 1000., signals['MessI_5h_sb_f_g_c'], label='$p f_{m} \pm s f_{c}$') ax[1][0].plot(f / 1000., signals['MessL_10h_f_f_r'], label='$n f_{c}$') ax[1][0].plot(f / 1000., signals['MessL_5h_f_g'], label='$p f_{m}$') ax[1][0].plot(f / 1000., signals['MessL_5h_sb_f_g_c'], label='$p f_{m} \pm s f_{c}$') ax[1][1].plot(f / 1000., signals['MessN_10h_f_f_r'], label='$n f_{c}$') ax[1][1].plot(f / 1000., signals['MessN_5h_f_g'], label='$p f_{m}$') ax[1][1].plot(f / 1000., signals['MessN_5h_sb_f_g_c'], label='$p f_{m} \pm s f_{c}$') ax[1][2].plot(f / 1000., signals['MessQ_10h_f_f_r'], label='$n f_{c}$') ax[1][2].plot(f / 1000., signals['MessQ_5h_f_g'], label='$p f_{m}$') ax[1][2].plot(f / 1000., signals['MessQ_5h_sb_f_g_c'], label='$p f_{m} \pm s f_{c}$') # name_ylabel = r'$\Sigma$ SCD [mV$^{2}$]' name_ylabel = r'$\Sigma$ SC [-]' ax[0][0].set_ylabel(name_ylabel, fontsize=font_big) ax[0][1].set_ylabel(name_ylabel, fontsize=font_big) ax[0][2].set_ylabel(name_ylabel, fontsize=font_big) ax[1][0].set_ylabel(name_ylabel, fontsize=font_big) ax[1][1].set_ylabel(name_ylabel, fontsize=font_big) ax[1][2].set_ylabel(name_ylabel, fontsize=font_big) name_xlabel = 'Frequency [kHz]' ax[0][0].set_xlabel(name_xlabel, fontsize=font_big) ax[0][1].set_xlabel(name_xlabel, fontsize=font_big) ax[0][2].set_xlabel(name_xlabel, fontsize=font_big) ax[1][0].set_xlabel(name_xlabel, fontsize=font_big) ax[1][1].set_xlabel(name_xlabel, fontsize=font_big) ax[1][2].set_xlabel(name_xlabel, fontsize=font_big) ax[0][0].legend(fontsize=font_label, ncol=1, loc='best', labelspacing=0.2, handletextpad=0.2) ax[0][1].legend(fontsize=font_label, ncol=1, loc='best', labelspacing=0.2, handletextpad=0.2) ax[0][2].legend(fontsize=font_label, ncol=1, loc='best', labelspacing=0.2, handletextpad=0.2) ax[1][0].legend(fontsize=font_label, ncol=1, loc='best', labelspacing=0.2, handletextpad=0.2) ax[1][1].legend(fontsize=font_label, ncol=1, loc='best', labelspacing=0.2, handletextpad=0.2) ax[1][2].legend(fontsize=font_label, ncol=1, loc='best', labelspacing=0.2, handletextpad=0.2) plt.rcParams['mathtext.fontset'] = 'cm' # ax[0][0].set_title('$1^{st}$ MC', fontsize=font_big) # ax[0][1].set_title('$2^{nd}$ MC', fontsize=font_big) # ax[0][2].set_title('$4^{th}$ MC', fontsize=font_big) # ax[1][0].set_title('$7^{th}$ MC', fontsize=font_big) # ax[1][1].set_title('$9^{th}$ MC', fontsize=font_big) # ax[1][2].set_title('$10^{th}$ MC', fontsize=font_big) ax[0][0].set_title('MC 1', fontsize=font_big) ax[0][1].set_title('MC 2', fontsize=font_big) ax[0][2].set_title('MC 4', fontsize=font_big) ax[1][0].set_title('MC 7', fontsize=font_big) ax[1][1].set_title('MC 9', fontsize=font_big) ax[1][2].set_title('MC 10', fontsize=font_big) ax[0][0].set_xlim(left=0, right=500) ax[0][1].set_xlim(left=0, right=500) ax[0][2].set_xlim(left=0, right=500) ax[1][0].set_xlim(left=0, right=500) ax[1][1].set_xlim(left=0, right=500) ax[1][2].set_xlim(left=0, right=500) # valtop = 0.15 valtop = 1. ax[0][0].set_ylim(bottom=0, top=valtop) ax[0][1].set_ylim(bottom=0, top=valtop) ax[0][2].set_ylim(bottom=0, top=valtop) ax[1][0].set_ylim(bottom=0, top=valtop) ax[1][1].set_ylim(bottom=0, top=valtop) ax[1][2].set_ylim(bottom=0, top=valtop) ax[0][0].set_xticks([0, 100, 200, 300, 400, 500]) ax[0][1].set_xticks([0, 100, 200, 300, 400, 500]) ax[0][2].set_xticks([0, 100, 200, 300, 400, 500]) ax[1][0].set_xticks([0, 100, 200, 300, 400, 500]) ax[1][1].set_xticks([0, 100, 200, 300, 400, 500]) ax[1][2].set_xticks([0, 100, 200, 300, 400, 500]) ax[0][0].tick_params(axis='both', labelsize=font_little) ax[0][1].tick_params(axis='both', labelsize=font_little) ax[0][2].tick_params(axis='both', labelsize=font_little) ax[1][0].tick_params(axis='both', labelsize=font_little) ax[1][1].tick_params(axis='both', labelsize=font_little) ax[1][2].tick_params(axis='both', labelsize=font_little) ax[0][0].grid(axis='both') ax[0][1].grid(axis='both') ax[0][2].grid(axis='both') ax[1][0].grid(axis='both') ax[1][1].grid(axis='both') ax[1][2].grid(axis='both') for ax_it in ax.flatten(): for tk in ax_it.get_yticklabels(): tk.set_visible(True) for tk in ax_it.get_xticklabels(): tk.set_visible(True) ax_it.yaxis.offsetText.set_visible(True) # plt.show() name = 'TestBench_Cyclo_SC_Each_AE_EnvFftComps' path_1 = 'C:\\Felix\\29_THESIS\\MODEL_A\\Chapter3_Test_Bench\\03_Figures\\' path_2 = 'C:\\Felix\\29_THESIS\\MODEL_A\\LATEX_Diss_FLn\\bilder\\Figures_Test_Bench\\' path_1b = path_1 + name + '.svg' path_2b = path_2 + name + '.pdf' plt.savefig(path_1b) plt.savefig(path_2b) else: print('wrong mode') return
def printSequence(sp: la_class.ServerParam, feat: la_class.Feature, path: str) -> None: import matplotlib # smatplotlib.use('Agg') from matplotlib import pyplot as plt import matplotlib.font_manager as fm fig = plt.figure(figsize=(10.5, 6.8)) plt.rcParams['font.family'] = 'Times New Roman' plt.rcParams['ps.useafm'] = True plt.rcParams['pdf.use14corefonts'] = True plt.rcParams['text.usetex'] = True xlim = sp.pitch_length / 2 + 5.0 ylim = sp.pitch_width / 2 + 5.0 fm._rebuild() plt.xlim([-xlim, xlim]) plt.ylim([ylim, -ylim]) plt.tick_params(labelsize=32) # plot soccer fields plt.plot([sp.goal_line_l, -sp.penalty_area_x], [-sp.penalty_area_y, -sp.penalty_area_y], color='g', linewidth=4) plt.plot([sp.goal_line_l, -sp.penalty_area_x], [sp.penalty_area_y, sp.penalty_area_y], color='g', linewidth=4) plt.plot([sp.goal_line_r, sp.penalty_area_x], [-sp.penalty_area_y, -sp.penalty_area_y], color='g', linewidth=4) plt.plot([sp.goal_line_r, sp.penalty_area_x], [sp.penalty_area_y, sp.penalty_area_y], color='g', linewidth=4) plt.plot([-sp.penalty_area_x, -sp.penalty_area_x], [sp.penalty_area_y, -sp.penalty_area_y], color='g', linewidth=4) plt.plot([sp.penalty_area_x, sp.penalty_area_x], [sp.penalty_area_y, -sp.penalty_area_y], color='g', linewidth=4) plt.plot([sp.goal_line_l, sp.goal_line_r], [-sp.pitch_width / 2, -sp.pitch_width / 2], color='g', linewidth=4) plt.plot([sp.goal_line_l, sp.goal_line_r], [sp.pitch_width / 2, sp.pitch_width / 2], color='g', linewidth=4) plt.plot([sp.goal_line_l, sp.goal_line_l], [-sp.pitch_width / 2, sp.pitch_width / 2], color='g', linewidth=4) plt.plot([sp.goal_line_r, sp.goal_line_r], [-sp.pitch_width / 2, sp.pitch_width / 2], color='g', linewidth=4) plt.plot([0, 0], [-sp.pitch_width / 2, sp.pitch_width / 2], color='g', linewidth=4) p = plt.Circle((0.0, 0.0), 9.0, color='g', linewidth=4, fill=False) ax = plt.gca() ax.add_patch(p) for x, y, color in zip(feat.all_kick_path_x, feat.all_kick_path_y, feat.color4plt_ks): plt.plot(x, y, color) filename = path + feat.team_point[0] + "-kick_sequence" extension = [".eps", ".pdf", ".png", ".svg"] for e in extension: plt.savefig(filename + e, dpi=300, bbox_inches="tight", transparent=True) plt.show()
#!/usr/bin/python #coding:utf-8 from skimage import io, transform import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manager import _rebuild import tensorflow as tf from pylab import mpl _rebuild() # 重新创建字体索引列表 mpl.rcParams['font.sans-serif']=[u'SimHei'] # 正常显示中文标签 mpl.rcParams['axes.unicode_minus']=False # 正常显示正负号 def load_image(path): fig = plt.figure("Centre and Resize") img = io.imread(path) img = img / 255.0 ax0 = fig.add_subplot(131) ax0.set_xlabel(u'Original Picture') ax0.imshow(img) short_edge = min(img.shape[:2]) y = (img.shape[0] - short_edge) // 2 x = (img.shape[1] - short_edge) // 2 crop_img = img[y:y+short_edge, x:x+short_edge] ax1 = fig.add_subplot(132) ax1.set_xlabel(u"Centre Picture") ax1.imshow(crop_img)
# 在terminal里画图会报错 _tkinter.TclError: no display name and no $DISPLAY environment variable # 必须加下面来防止出错 import matplotlib matplotlib.use('Agg') # plt.clf() # 清理之前的内容。好像plt.savefig和plt.show()不一样,似乎有时候不会清理之前的内容 plt.savefig('filename.png', bbox_inches='tight') # use this instead of plt.show() # bbox_inches='tight' 的效果非常赞,它可以让两边边变窄, 周围的文字留全!!!! # 中文显示: 来自Dong Zhou, 还未自己尝试 # 需要自己下载字体 anaconda3/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf plt.rcParams['font.sans-serif'] = 'SimHei' plt.rcParams['axes.unicode_minus'] = False # 还得删除缓存:https://www.zhihu.com/question/25404709/answer/309784195 from matplotlib.font_manager import _rebuild _rebuild() #reload一下 # use seaborn import seaborn as sns; sns.set(color_codes=True) # 如果pandas希望在jupyter里display所有的列,那么用下面的代码 # https://stackoverflow.com/questions/11361985/output-data-from-all-columns-in-a-dataframe-in-pandas pd.set_option('display.max_columns', None) # 如果希望显示pandas cell内部的所有内容: # https://stackoverflow.com/questions/25351968/how-to-display-full-non-truncated-dataframe-information-in-html-when-convertin pd.set_option('display.max_colwidth', -1)
from matplotlib.colors import LinearSegmentedColormap from multiprocessing import Pool, sharedctypes, RawArray, RawValue from CMIP import * from MS.IBIS import * from MS.LPJ import * from MS.Biome_BGC import * from MS.MOD17A2 import * import pymongo client = pymongo.MongoClient(host=['223.2.41.104:27017']) db = client.Comparison metricCollection = db.Metric myfont = fm.FontProperties(fname='/home/scr/.config/matplotlib/msyh.ttf') plt.switch_backend('Agg') _rebuild() sns.set() # annual/model-avg.nc def plotSpatial(argv): fig, axes = plt.subplots(nrows=int(len(argv['models'])), ncols=1, figsize=(8, 16), dpi=100, sharex=True, sharey=True) axes = axes.flatten() for i, model in enumerate(argv['models']): fpath = '../data/annual/%s-avg.nc' % model dataset = Dataset(fpath, mode='r')
# %matplotlib inline import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.font_manager as fm !apt-get update -qq !apt-get install fonts-nanum* -qq path = '/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf' font_name = fm.FontProperties(fname=path, size=10).get_name() print(font_name) plt.rc('font', family=font_name) fm._rebuild() mpl.rcParams['axes.unicode_minus'] = False # drive mount from google.colab import drive drive.mount('/content/drive') # colab to pdf install # !apt-get install texlive texlive-xetex texlive-latex-extra pandoc # !pip install pypandoc # colab to pdf code # !jupyter nbconvert --to PDF '/content/drive/MyDrive/Colab Notebooks/bigdata_project_scv.ipynb'
import matplotlib.pyplot as plt import matplotlib as mpl import matplotlib.font_manager as fm import numpy as np %matplotlib inline %config InlineBackend.figure_format = 'retina‘ mpl.rcParams['axes.unicode_minus'] = False # 하이픈(-) 폰트 깨질 경우 대비 path = '/usr/share/fonts/truetype/nanum/NanumGothicBold.ttf' font_name = fm.FontProperties(fname=path, size=18).get_name() plt.rc('font', family=font_name) fm._rebuild() # 이걸 해줘야 plt.rc가 작동 plt.plot(np.random.randn(4, 8), np.random.randn(4,8), 'bo--') plt.title('타이틀') plt.xlabel('X 라벨') plt.ylabel('Y 라벨') plt.show()
from matplotlib.font_manager import _rebuild from IPython.display import display # plt.style.use("fivethirtyeight") # sns.set_style({'font.sans-serif':['simhei','Arial']}) plt.rcParams['font.sans-serif'] = ['simHei'] #用来正常显示中文标签 plt.rcParams['font.size'] = 6 # plt.rcParams['axes.unicode_minus']=False #用来正常显示负号 # 导入数据 sz_rent = pd.read_csv('merge_zc.csv') # print('data-row2====') display(sz_rent.head(n=2)) _rebuild() #reload一下 # # 检查缺失值情况 # print('info====') sz_rent.info() print('describe====') print(sz_rent.describe()) # 添加新特征均价 zf = sz_rent.copy() zf['perPrice'] = sz_rent['price'] / sz_rent['size'] #print('PerPrice====',sz_rent['PerPrice']) # # 重新摆放列位置 columns = [
def main_mini(lujing): print('jinrule ', 'main_mini') data = [i for i in open(lujing, encoding='utf-8').readlines()] out = [] for i in data: for j in i.split(';'): j = j.strip("\n") j = j.strip(' ') out.append(j) # print(out[:10]) # print(len(out)) print('jinrule ', 'main_mini2') from collections import defaultdict dict1 = defaultdict(int) print('jinrule ', 'main_mini3') for i in out: dict1[i] += 1 print('jinrule ', 'main_mini4') # print(len(dict1)) a = [] for i in dict1: a.append((i, dict1[i])) a = sorted(a, key=lambda x: x[1]) a = a[::-1] print('jinrule ', 'main_mini5') # print(a[:1000]) # 我们只处理最大前1千个词语 a = a[: 100] #################################################################################### print(778, a) # 计算文本相似度. from bert_serving.client import BertClient import numpy as np bc = BertClient() print(32432423473947, a) shuju = [i[0] for i in a] def cosine(a, b): return a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b)) print('jinrule ', shuju) emb = np.array(bc.encode(shuju)) # print('new',emb) # print(['First do it', 'then do it right'], ":", cosine(emb[0], emb[1])) # return 99999999999999 # 下面调用这个函数即可vec_route # b里面放的是词语,词频,词向量 b = [] for i in range(len(a)): b.append([a[i][0], a[i][1], emb[i]]) # print(b,333333333333333333333333) # 下面进行降维 from matplotlib.font_manager import _rebuild _rebuild() #reload一下 import numpy as np from sklearn.manifold import TSNE import matplotlib.pyplot as plt import matplotlib as mpl label = [] for i in b: label.append(i[0]) vectors = [] for i in b: vectors.append(i[2]) cipin = [] for i in b: cipin.append(i[1]) print(112) tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000, method='exact') low_dim_embs = tsne.fit_transform( vectors) # 需要显示的词向量,一般比原有词向量文件中的词向量个数少,不然点太多,显示效果不好 print(113) for i in range(len(b)): b[i][2] = low_dim_embs[i] uuuuu = str(b) print(11233333333) import json #chuanshu22288.json 这个数据就是给前端用的!!!!!!!!!!!!!!!!!1 bb = b for i in range(len(bb)): bb[i][2] = list(bb[i][2]) print(11255555555) import json print(1127777777777) # define A.class class node: def __init__(self, id, label, x, y, size, color): self.id = id self.label = label self.x = x self.y = y self.size = size self.cluster = color # cluster ## import matplotlib.pyplot as plt from sklearn.datasets.samples_generator import make_blobs from sklearn.cluster import KMeans from sklearn import metrics print(11299999999) #选择聚类数K=2 聚类小于=8,因为颜色就写了8个 final2 = [] for i4 in range(3, 11): n_clusters = i4 y_pred = KMeans(n_clusters=n_clusters).fit_predict(low_dim_embs) colorlist = [ 'red', 'black', 'yellow', 'greenyellow', 'blue', 'brown', 'coral', 'cyan', 'deeppink', 'orange' ] list1 = [] for i, j in enumerate(bb): list1.append( node(i, j[0], float((float(j[2][0]))), float((float(j[2][1]))), float(j[1]**0.5), colorlist[y_pred[i]]).__dict__) nodes = {} # 接口:https://github.com/xukuanzhuo/xukuanzhuo.github.io/issues/8 #下面把每一个聚类里面的距离算一下. #similarity class edge: def __init__(self, sourceID, targetID, size): self.source = sourceID self.target = targetID self.size = size print(112000000000) print(bb, 'lookupu234') list3 = [] for i in range(n_clusters): dexlist = [] for j in range(len(y_pred)): if y_pred[j] == i: dexlist.append(j) for i1 in range(len(dexlist)): for i2 in range(i1 + 1, len(dexlist)): left = dexlist[i1] # 得到索引. right = dexlist[i2] similar = cosine(np.array(bb[left][2]), np.array(bb[right][2])) + 1 list3.append( edge(bb[left][0], bb[right][0], similar).__dict__) print(list3, "list3") all3 = {"clusterNum": n_clusters, "nodes": list1, "edges": list3} final2.append(all3) print('final2', final2) tmp2 = json.dumps(final2, ensure_ascii=False) with open("chuanshuFinal.json", mode='w', encoding='utf-8') as f: print("最终json放在chuanshuFinal.json") f.write(tmp2) return tmp2