def __init__(self, font_name='', font_dirs='', x_label='', y_label=''): self.fig = plt.figure(figsize=(3.3, 3.3)) self.ax = self.fig.add_subplot(111) # Change font properties or add custom fonts if font_name != '': ''' Solution found at: https://stackoverflow.com/questions/35668219/\ how-to-set-up-a-custom-font-with-custom-path-to-matplotlib-global-font ''' self.font = font_name font_files = fm.findSystemFonts(fontpaths=font_dirs) font_list = fm.createFontList(font_files) fm.fontManager.ttflist.extend(font_list) plt.rcParams['font.family'] = self.font self.title_font_sz = 8 self.ticks_font_size = 7 plt.rcParams.update({'font.size': self.ticks_font_size}) # Define axes labels self.ax.set_xlabel(x_label, labelpad=5, fontsize=self.title_font_sz) self.ax.set_ylabel(y_label, labelpad=5, fontsize=self.title_font_sz) # Define markers and colours self.markers = [ '8', '>', 'D', '^', 'H', '*', 'd', 'h', 'v', 's', '<', 'o', 'p' ] colormap = plt.cm.get_cmap('brg') self.colours = [ colormap(i) for i in np.linspace(0, 0.9, len(self.ax.collections)) ] return
def japanize(): font_dir_path = get_font_path() font_dirs = [font_dir_path] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) matplotlib.rc('font', family=FONT_NAME)
def radar(self):#绘制雷达图,涉及变量为11个中排除调式剩下的10个,标准化后的极径范围为0-1 # 导入中文 font_dirs = ['./font'] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) plt.rcParams['font.family'] = 'SimHei' feature=['danceability','energy','valence','tempo','loudness','key','acousticness','instrumentalness','liveness','speechiness','danceability'] # 启用主题 plt.style.use('ggplot') #等分图 angles = np.linspace(0, 2 * np.pi, 10, endpoint=False) #角度首尾封闭 angles = np.append(angles, angles[0]) #数据首尾封闭 values = np.append(np.array(self.figure), self.data[2]) fig=plt.figure() # 设置为极坐标格式 ax = fig.add_subplot(111, polar=True) ax.plot(angles, values, 'o-', linewidth=2) # 填充颜色 ax.fill(angles, values, alpha=0.25) ax.set_thetagrids(angles * 180 / np.pi, feature) plt.title('radar') ax.set_ylim(0, 1.1) # 添加网格线 ax.grid(True) plt.show()
def set_style(style_filename_list): # graph_font_filename = resource_filename(__name__, os.path.join("fonts", "Roboto-Medium.ttf")) # WORKAROUND: createFontList deprecation in mpl >=3.2 if hasattr(fm.fontManager, "addfont"): font_dirs = [ resource_filename(__name__, "fonts"), ] font_files = fm.findSystemFonts(fontpaths=font_dirs) for font_found in font_files: fm.fontManager.addfont(font_found) else: font_dirs = [ resource_filename(__name__, "fonts"), ] font_files = fm.findSystemFonts(fontpaths=font_dirs) font_list = fm.createFontList(font_files) fm.fontManager.ttflist.extend(font_list) fm.fontManager.ttflist = font_list styles_in_final_location = list() for source_name in style_filename_list: styles_in_final_location.append( resource_filename(__name__, os.path.join("mpl_styles", source_name))) # fm.FontProperties(fname=graph_font_filename) matplotlib.style.use(styles_in_final_location)
def set_font(font): """set graph text font """ if font not in plt.rcParams["font.family"]: font_manager.fontManager.ttflist.extend( font_manager.createFontList(font_manager.findSystemFonts())) plt.rcParams["font.family"] = font
def plot_similarity(): # region Setting font for matplotlib font_dirs = [ 'C:\\Users\\NSL\\Downloads\\Nanum_Gothic\\', ] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) mpl.rcParams['font.family'] = 'NanumGothic' # endregion words = [] embeddings = [] similarities = Embeddings.objects.all() for similarity in similarities: words.append(similarity.text) np_bytes = base64.b64decode(similarity.embedding) embeddings.append(pickle.loads(np_bytes)) pca = PCA(n_components=2) result = pca.fit_transform(embeddings) plt.scatter(result[:, 0], result[:, 1]) for i, word in enumerate(words): plt.annotate(word, xy=(result[i, 0], result[i, 1])) plt.savefig("plot.png", dpi=1000)
def set_style(style='white', palette='deep', context='poster', font='FreeSans', font_scale=1.00): """Set plot preference in a way that looks good to me. """ import matplotlib.font_manager as font_manager import cycler # Update font list. mpl_data_dir = os.path.dirname(mpl.matplotlib_fname()) font_files = font_manager.findSystemFonts( os.path.join(mpl_data_dir, 'fonts', 'ttf')) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) sns.set( style=style, palette=palette, context=context, font=font, font_scale=font_scale, ) scale_dict = {'paper': 1.3, 'notebook': 1.3, 'talk': 1.4, 'poster': 1.4} scale = scale_dict[context] plt.rc('axes', linewidth=1.33, labelsize=14) plt.rc('xtick', labelsize=10 * scale) plt.rc('ytick', labelsize=10 * scale) plt.rc('xtick', bottom=True) plt.rc('xtick.major', size=5 * scale, width=1.33) plt.rc('xtick.minor', size=5 * scale, width=1.33) plt.rc('ytick', left=True) plt.rc('ytick.major', size=5 * scale, width=1.33) plt.rc('ytick.minor', size=5 * scale, width=1.33) plt.rc('legend', fontsize=7 * scale) plt.rc('grid', color='grey', linewidth=0.5, alpha=0.33) plt.rc('font', family=font) color_palette = [ '#005AC8', '#AA0A3C', '#0AB45A', '#FA7850', '#8214A0', '#FA78FA', '#A0FA82', '#006E82', '#00A0FA', '#14D2DC', '#F0F032', '#FAE6BE', ] mpl.rcParams['axes.prop_cycle'] = cycler.cycler(color=color_palette)
def set_style(style_filename_list, can_use_cjk=True): # graph_font_filename = resource_filename(__name__, os.path.join("fonts", "Roboto-Medium.ttf")) # WORKAROUND: createFontList deprecation in mpl >=3.2 if hasattr(fm.fontManager, "addfont"): font_dirs = [ resource_filename(__name__, "fonts"), ] font_files = fm.findSystemFonts(fontpaths=font_dirs) for font_found in font_files: fm.fontManager.addfont(font_found) else: font_dirs = [ resource_filename(__name__, "fonts"), ] font_files = fm.findSystemFonts(fontpaths=font_dirs) font_list = fm.createFontList(font_files) fm.fontManager.ttflist.extend(font_list) fm.fontManager.ttflist = font_list styles_in_final_location = list() for source_name in style_filename_list: styles_in_final_location.append( resource_filename(__name__, os.path.join("mpl_styles", source_name))) # fm.FontProperties(fname=graph_font_filename) # Apply style matplotlib.style.use(styles_in_final_location) # NEW: support for CJK characters, apply override after setting the style if config["General"].getint("use_cjk_font") != 0 and can_use_cjk: plt.rcParams['font.family'] = 'Noto Sans CJK JP'
def extend_matplotlib_fonts(font_directory): """Facilitates the use of external fonts for use in matplotlib. Currently dependent on a deprecated function. Suggested matplotlib.font_manager.FontManager.addfont method does not appear to give expected results.""" font_dirs = [font_directory] font_files = fm.findSystemFonts(fontpaths=font_dirs) font_list = fm.createFontList(font_files) fm.fontManager.ttflist.extend(font_list)
def add_custom_fonts(): ''' Allow us to use fonts from our fonts folder ''' font_dirs = ['fonts'] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list)
def install_fonts_to_matplotlib(): fonts_folder = os.path.join(home, '.fonts') font_dirs = [fonts_folder] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) matplotlib.rcParams['font.family'] = 'Roboto'
def graph2fig(g,x:int=800,y:int=600): """ 用matplotlib对有向图进行可视化 :param g: nx.DiGraph :param x: 像素 :param y: 像素 :return: """ import pkg_resources import matplotlib.pyplot as plt import matplotlib.font_manager as font_manager font_files = [pkg_resources.resource_filename('smoothnlp', 'resources/simhei/simhei.ttf')] font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) plt.rcParams['font.family'] = "SimHei" if len(g)<=0: ## 处理空的Graph return if len(g)==2: pos = nx.drawing.planar_layout(g) else: dists = shortest_path_length(g) pos = nx.drawing.kamada_kawai_layout(g,dist=dists) fig = plt.figure(figsize = (x/100,y/100),dpi=100) plt.title('SmoothNLP开源工具生成的知识图谱', fontdict={"fontsize": 14}) def label_modification(label): length = len(label) if 0 < length <= 6: return label elif length <= 12: return label[:length // 2] + "\n" + label[length // 2:] else: return label[:length // 3] + "\n" + label[length // 3:2 * length // 3] + "\n" + label[2 * length // 3:] node_labels = {k: label_modification(k) for k in g.nodes} edge_labels = {k:label for k,label in nx.get_edge_attributes(g, "label").items()} nx.draw(g, pos, labels=node_labels, with_labels=True, node_color="lightblue", edge_color="grey", node_size=[min(len(n), 5) * 1500 for n in g.nodes], alpha=1.0, font_color="white", font_size=14, width=3.0, font_family="SimHei") nx.draw_networkx_edge_labels(g, pos, edge_labels=edge_labels, font_color='Black', font_size=16, font_family="SimHei") return fig
def japanize(): font_dir_path = get_font_path() font_dirs = [font_dir_path] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) is_support_createFontList = LooseVersion(matplotlib.__version__) < '3.2' if is_support_createFontList: font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) else: for fpath in font_files: font_manager.fontManager.addfont(fpath) matplotlib.rc('font', family=FONT_NAME)
def __init__(self, **kwargs): self.parameters = kwargs self.nrows = None self.ncols = None self.figsize = None font_dirs = [ '/home/marco/.fonts', ] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list)
def verify_plotting_packages(): """Verifies that all the plotting packages are installed. Otherwise, install the missing one. """ import matplotlib from matplotlib import font_manager # Import the fonts font_dirs = [os.path.dirname(os.path.realpath(__file__)) + '/fonts/'] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list)
def visualize_video_subtitle(video_frames: np.ndarray, subtitle: str, swap_axes: bool = True): font_list = fm.createFontList(['THSarabunNew.ttf']) fm.fontManager.ttflist.extend(font_list) plt.rcParams['font.family'] = 'TH Sarabun New' if swap_axes: video_frames = np.swapaxes(video_frames, 1, 2) ax = plt.subplot(111) ax.spines["top"].set_visible(False) ax.spines["bottom"].set_visible(False) ax.spines["right"].set_visible(False) ax.spines["left"].set_visible(False) plt.axis('off') text = plt.text(0.5, 0.1, "", ha='center', va='center', transform=ax.transAxes, fontdict={ 'fontsize': 14, 'color': 'yellow', 'fontweight': 500 }) text.set_path_effects([ path_effects.Stroke(linewidth=3, foreground='black'), path_effects.Normal() ]) subs = subtitle.split() inc = max(len(video_frames) / (len(subs) + 1), 0.01) img = None for i, frame in enumerate(video_frames): sub = " ".join(subs[:int(i / inc)]) text.set_text(sub) if img is None: img = plt.imshow(frame) else: img.set_data(frame) plt.pause(FRAME_RATE) plt.show()
def ps_graph(search_term, tweets_dataframe, save_path): # Sort out fonts font_files = font_manager.findSystemFonts( fontpaths= "/Users/jamesashford/Documents/Projects/Hackathons/Oxford Hack 2020/OxHack-2020/TCARS/rsc" ) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) from matplotlib import rcParams rcParams['font.family'] = "Swiss911 UCm BT" rcParams['font.size'] = 18 # Extract data from tweets_dataframe pols = tweets_dataframe["Polarity"].values subjs = tweets_dataframe["Subjectivity"].values times = tweets_dataframe["Time"].values # Convert times to mdates datetimes tweet_datetimes = [ mdates.date2num(datetime.datetime.strptime(t, "%Y-%m-%d")) for t in times ] # Scatterplot coloured by datetime fig = plt.figure(figsize=(5, 3), dpi=200) ax = fig.add_subplot(111) sc = ax.scatter(pols, subjs, c=tweet_datetimes) # Create colourbar with autoformatted mdates loc = mdates.AutoDateLocator() cbar = fig.colorbar(sc, ticks=loc, format=mdates.AutoDateFormatter(loc)) # Make all the axes white for viewing ax.tick_params(axis='x', colors="white") ax.tick_params(axis='y', colors="white") ax.set_xlabel("Polarity", color='white') ax.set_ylabel("Subjectivity", color='white') ax.spines['top'].set_edgecolor('white') ax.spines['bottom'].set_edgecolor('white') ax.spines['left'].set_edgecolor('white') ax.spines['right'].set_edgecolor('white') cbar.ax.tick_params(axis='y', colors='white') [t.set_color('white') for t in cbar.ax.xaxis.get_ticklabels()] [t.set_color('white') for t in cbar.ax.yaxis.get_ticklabels()] # Save file_name = f"{save_path}/{search_term}_psplot.png" plt.savefig(file_name, transparent=True, pad_inches=0, bbox_inches='tight') return True
def japanize(): FONTS_DIR = 'fonts' FONT_NAME = "IPAexGothic" font_dir_path = pathlib.Path( os.path.abspath(__file__)).parent / pathlib.Path(FONTS_DIR) font_dirs = [ font_dir_path, ] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) matplotlib.rc('font', family=FONT_NAME)
def __init__(self, font_name='', font_dirs='', x_label=''): self.fig = plt.figure(figsize=(8.27, 11.69), dpi=100) # A4 Size # self.fig = plt.figure(figsize=(11.69, 16.53), dpi=100) # A3 Size self.axG = self.fig.add_subplot(111) # Change fonts if other is provided if font_name != '': ''' Solution found at: https://stackoverflow.com/questions/35668219/\ how-to-set-up-a-custom-font-with-custom-path-to-matplotlib-global-font ''' self.font = font_name font_files = fm.findSystemFonts(fontpaths=font_dirs) font_list = fm.createFontList(font_files) fm.fontManager.ttflist.extend(font_list) plt.rcParams['font.family'] = self.font self.title_font_sz = 8 self.ticks_font_size = 7 plt.rcParams.update({'font.size': self.ticks_font_size}) # Turn off axis lines and ticks of the big subplot self.axG.spines['top'].set_color('none') self.axG.spines['bottom'].set_color('none') self.axG.spines['left'].set_color('none') self.axG.spines['right'].set_color('none') self.axG.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') self.axG.set_xticklabels([]) self.axG.set_yticklabels([]) self.axG.set_xlabel(x_label, labelpad=15, fontsize=self.title_font_sz) plt.subplots_adjust(wspace=0.3, hspace=0.3) self.colours = ['#1b9e77', '#d95f02', '#7770b3'] ''' '#1b9e77' # Green '#d95f02' # Red '#7770b3' # Purple ''' self.linestyles = ['-', '--', ':'] self.position_calls = [[0, 0]] self.first_row_axes = [] self.first_column_axes = [] return
def PlotTestResults(y, pred): # Font Stuff font_files = font_manager.findSystemFonts(fontpaths=os.getcwd() + '../../../poster_figures/fonts') font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) mpl.rcParams['font.family'] = 'Product Sans' # Figure aspect ratio fig, ax = plt.subplots(figsize=(14, 9)) x = list(range(len(y))) # Line Labels ax.plot(x, y, label='True', color="#00c8fc", linewidth=2) ax.plot(x, pred, '--', label='Predicted', color="#DB4437", linewidth=2) # Axis value Ranges plt.xlabel('Hours (since start of test)') plt.ylabel('Rides per hour') # Add title with bold font and padding title = "Prediction of Citi Bike use from Weather" plt.title(title, fontweight="bold", pad=40, size=45) fig.canvas.set_window_title(title) # Position legend and make it horizontal leg = plt.legend(fontsize=30, ncol=2, bbox_to_anchor=(0.5, 1.01), loc='center') leg.get_frame().set_linewidth(0.0) # Remove actual tick marks plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=True, labelbottom=True) for axis in [ax.xaxis, ax.yaxis]: axis.label.set_weight("bold") axis.label.set_size(38) axis.labelpad = 15 # Change axis label font size plt.tick_params(axis='both', which='major', labelsize=30) # Remove lines plt.gca().spines["top"].set_alpha(0) plt.gca().spines["bottom"].set_alpha(0) plt.gca().spines["right"].set_alpha(0) plt.gca().spines["left"].set_alpha(0) # Set layout fig.tight_layout() fig.savefig("predict_ridecount.svg", dpi=1200) plt.show()
def applyATLASstyle(mtp): font_dir = os.path.abspath(__file__).replace("PyATLASstyle.py", "fonts/") font_dirs = [ font_dir, ] import matplotlib.font_manager as font_manager font_files = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) #mtp.rcParams['font.family'] = 'Arial' mtp.rcParams['font.family'] = 'TeX Gyre Heros' mtp.rcParams['font.size'] = 15 mtp.rcParams['legend.frameon'] = False mtp.rcParams['legend.fontsize'] = 14 mtp.rcParams['lines.antialiased'] = False # mtp.rcParams['lines.linewidth'] = 2. mtp.rcParams['xtick.direction'] = 'in' mtp.rcParams['xtick.top'] = True mtp.rcParams['xtick.minor.visible'] = True mtp.rcParams['xtick.major.size'] = 10 mtp.rcParams['xtick.minor.size'] = 5 mtp.rcParams['ytick.direction'] = 'in' mtp.rcParams['ytick.right'] = True mtp.rcParams['ytick.minor.visible'] = True mtp.rcParams['ytick.major.size'] = 10 mtp.rcParams['ytick.minor.size'] = 5 mtp.rcParams['mathtext.fontset'] = 'custom' #mtp.rcParams['mathtext.it'] = 'Arial:italic' #mtp.rcParams['mathtext.bf'] = 'Arial:bold' #mtp.rcParams['mathtext.rm'] = 'Arial' #mtp.rcParams['mathtext.sf'] = 'Arial' #mtp.rcParams['mathtext.cal'] = 'Arial:italic' #mtp.rcParams['mathtext.tt'] = 'Arial' mtp.rcParams['mathtext.it'] = 'TeX Gyre Heros:italic' mtp.rcParams['mathtext.bf'] = 'TeX Gyre Heros:bold' mtp.rcParams['mathtext.rm'] = 'TeX Gyre Heros' mtp.rcParams['mathtext.sf'] = 'TeX Gyre Heros' mtp.rcParams['mathtext.cal'] = 'TeX Gyre Heros:italic' mtp.rcParams['mathtext.tt'] = 'TeX Gyre Heros' mtp.rcParams['axes.unicode_minus'] = False mtp.rcParams['pdf.fonttype'] = 42
def verify_plotting_packages(): """Verifies that all the plotting packages are installed. Otherwise, install the missing one. """ # Installing dependencies try: import numpy except ImportError: print('Package *numpy* is not installed. Installing it.') pip_install_wheel(package_name='numpy') try: import matplotlib except ImportError: print('Package *matplotlib* is not installed. Installing it.') pip_install_wheel(package_name='matplotlib') try: import seaborn except ImportError: print('Package *seaborn* is not installed. Installing it.') pip_install_wheel(package_name='seaborn') try: import pandas except ImportError: print('Package *pandas* is not installed. Installing it.') pip_install_wheel(package_name='pandas') import matplotlib matplotlib.use('agg') # To resolve the tkinter issue from matplotlib import font_manager # Import the fonts font_dirs = [nmv.consts.Paths.FONTS_DIRECTORY] font_files = font_manager.findSystemFonts(fontpaths=font_dirs) if nmv.utilities.is_blender_280(): for font_file in font_files: font_manager.fontManager.addfont(font_file) else: font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list)
def set_mat_font(): import platform #from matplotlib import font_manager, rc from matplotlib import font_manager as fm # 폰트 변경시 마이너스 깨지는 문제 수정 plt.rcParams['axes.unicode_minus'] = False font_size = 12 # font size 입력 win_path = "c:/Windows/Fonts/malgun.ttf" # 시스템에 저장되어있는 고딕폰트 목록 가져오기 gothic_fonts = [(f.name, f.fname) for f in fm.fontManager.ttflist if 'Gothic' in f.name] print(gothic_fonts) # plt전역에 폰트 지정 if platform.system() == 'Darwin': plt.rcParams['font.family'] = "AppleGothic" elif platform.system() == 'Windows': plt.rcParams['font.family'] = "Malgun Gothic" elif platform.system() == 'Linux': font_dirs = [ '/home/nbuser/fonts', ] # System font 찾는 경로를 설정 font_files = fm.findSystemFonts(fontpaths=font_dirs) # 폰트 파일로 부터 폰트 리스트를 생성 font_list = fm.createFontList(font_files) print(font_list) # matplotlib fontManager의 ttflist에 생성한 폰트리스트를 추가 fm.fontManager.ttflist.extend(font_list) plt.rcParams['font.family'] = "NanumGothic" else: print('Unknown') plt.rcParams["font.size"] = font_size
def set_plot_style(dpi: int = DPI): """Set plot style. :param dpi: DPI for saving and displaying figures, defaults to microdf.DPI (200). :type dpi: int, optional """ sns.set_style("white") # Set up Roboto. Must be downloaded in the current directory. # See https://stackoverflow.com/a/51844978/1840471. fm.fontManager.ttflist += fm.createFontList(["Roboto-Regular.ttf"]) STYLE = { "savefig.dpi": dpi, "figure.dpi": dpi, "figure.figsize": (6.4, 4.8), # Default. "font.sans-serif": "Roboto", "font.family": "sans-serif", # Set title text color to dark gray (https://material.io/color) not # black. "text.color": TITLE_COLOR, # Axis titles and tick marks are medium gray. "axes.labelcolor": AXIS_COLOR, "xtick.color": AXIS_COLOR, "ytick.color": AXIS_COLOR, # Grid is light gray. "axes.grid": True, "grid.color": GRID_COLOR, # Equivalent to seaborn.despine(left=True, bottom=True). "axes.spines.left": False, "axes.spines.right": False, "axes.spines.top": False, "axes.spines.bottom": False, } mpl.rcParams.update(STYLE)
def set_plot_style(dpi=DPI): """ Set plot style. Args: None. Returns: Nothing. Sets style. """ sns.set_style('white') # Set up Roboto. Must be downloaded in the current directory. # See https://stackoverflow.com/a/51844978/1840471. fm.fontManager.ttflist += fm.createFontList(['Roboto-Regular.ttf']) STYLE = { 'savefig.dpi': dpi, 'figure.dpi': dpi, 'figure.figsize': (6.4, 4.8), # Default. 'font.sans-serif': 'Roboto', 'font.family': 'sans-serif', # Set title text color to dark gray (https://material.io/color) not black. 'text.color': TITLE_COLOR, # Axis titles and tick marks are medium gray. 'axes.labelcolor': AXIS_COLOR, 'xtick.color': AXIS_COLOR, 'ytick.color': AXIS_COLOR, # Grid is light gray. 'axes.grid' : True, 'grid.color': GRID_COLOR, # Equivalent to seaborn.despine(left=True, bottom=True). 'axes.spines.left': False, 'axes.spines.right': False, 'axes.spines.top': False, 'axes.spines.bottom': False } mpl.rcParams.update(STYLE)
def __init__(self, *args): QPushButton.__init__(self, *args) self._app = QApplication.instance().window self.textOptions = Property.TextOptions() # Set initial textOptions values based on application preferences if 'preferences' in self._app.__dict__.keys(): preferenceTextOptions = self._app.preferences.getInternal('textOptions') if preferenceTextOptions is not None: self.setTextOptions(preferenceTextOptions) # Create dialog self._dialog = QTextOptionsWidget(self) self._ui = Ui_TextOptionsWidget() self._ui.setupUi(self._dialog) self._dialog.setUiObject(self._ui) self._window = SubWindow(self._app.ui.workspace) self._window.setWidget(self._dialog) self._dialog.setParent(self._window) self.hideTextOptionsWidget() # Create font list from matplotlib fonts fontPropList = fm.createFontList(fm.findSystemFonts()) # Get just the names fontList = map(lambda x:x.name, fontPropList) # Unique and sort the names fontList = list(set(fontList)) fontList.sort() # Enter fonts into the ui widget self._ui.name.addItems(fontList)
def cooc_graph(search_term, tweets_dataframe, save_path, NUM_OF_COOCS=5): def dict_value_sort(dictionary): return (dict( sorted(dictionary.items(), key=lambda item: item[1], reverse=True))) def dice_significance(cooc_array, word, word_list): """Dice statistical significance for "word" against all other words in "word_list" using corresponding 2d array of cooccurrance. """ word_index = word_list.index(word.upper()) k = len(cooc_array) ki = sum(cooc_array[:, word_index]) kj = np.sum(cooc_array, axis=0) kij = cooc_array[word_index, :] dice_stat = 2 * kij / (ki + kj) stat_dict = {word: d for word, d in zip(word_list, dice_stat)} stat_dict.pop(word.upper()) return (stat_dict) ALL_SEARCH_TERMS = TextBlob(search_term).words tweets = tweets_dataframe["Tweet"].dropna().values # Sort out fonts font_files = font_manager.findSystemFonts( fontpaths= "/Users/jamesashford/Documents/Projects/Hackathons/Oxford Hack 2020/OxHack-2020/TCARS/rsc" ) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list) # Extract and clean words all_words = TextBlob(" ".join(tweets).upper()).words.lemmatize() # Get stop-words stop_words = list(set(stopwords.words('english'))) + ['thi'] # Remove Stop and Short Words words = [ w for w in all_words if len(w) > 3 and w.lower() not in stop_words ] # Remove words that only occur once counts = dict(Counter(words)) key_words = [word for word in counts if counts[word] > 1] # Create dtm dtm = np.array( [[1 if (tweet.upper().count(word) > 0) else 0 for word in key_words] for tweet in tweets]) # Co-occurrances cooc = np.dot(dtm.T, dtm) graph_data = [] layer1_names = [] layer2_names = [] for term in ALL_SEARCH_TERMS: # Statistical significance of search_term term_dice_stats = dice_significance(cooc, term, key_words) term_dice_stats = dict_value_sort(term_dice_stats) # Get NUM_OF_COOCS most similar words most_similar = list(term_dice_stats.keys())[0:NUM_OF_COOCS] layer1_names += most_similar # Create a structure to hold the node links graph_data += [{ "from": term.upper(), "to": set_name, "stat": term_dice_stats[set_name] } for set_name in most_similar] # Iterate over each of the chosen coocs, and find their closest for word in most_similar: # Find stats for this word word_dice_stats = dice_significance(cooc, word, key_words) word_dice_stats = dict_value_sort(word_dice_stats) # Choose top nearby matches top_neighbours = list(word_dice_stats.keys())[0:10] layer2_names += top_neighbours new_graph_data = [{ "from": word.upper(), "to": set_name, "stat": word_dice_stats[set_name] } for set_name in top_neighbours] # Add to existing graph data graph_data += new_graph_data # Convert graph data to pandas dataframe gd = pd.DataFrame.from_dict(graph_data) # Create co-occurance graph # G = nx.from_numpy_matrix(cooc) G = nx.from_pandas_edgelist(gd, "from", "to", "stat") # Generate colours colours, sizes = [], [] l0, l1, l2 = {}, {}, {} for node in G: if node in ALL_SEARCH_TERMS.upper(): col = 'darkblue' #'red' size = min(counts[node] * 100, 5000) #5000 l0[node] = node elif node in layer1_names: col = 'lightblue' #'orange' size = min(counts[node] * 100, 3000) #2500 l1[node] = node else: col = 'cyan' #'blue' size = counts[node] * 10 #1000 l2[node] = node colours.append(col) sizes.append(size) # Visualisation fig = plt.figure(figsize=(5, 3), dpi=200) pos = nx.spring_layout(G) if len(ALL_SEARCH_TERMS) == 1: pos = nx.nx_agraph.graphviz_layout(G, prog='twopi') # Draw edges edges = nx.draw_networkx_edges( G, pos, alpha=1, width=1, edge_color='white') #width=gd["stat"].values*10) # Draw nodes, once white for background then again with colour+alpha nodes = nx.draw_networkx_nodes(G, pos, alpha=1, node_color='white', node_size=sizes) nodes = nx.draw_networkx_nodes(G, pos, alpha=0.8, node_color=colours, node_size=sizes) nodes.set_edgecolor('black') # Draw labels for each layer, with proportional sizes labels0 = nx.draw_networkx_labels(G, pos, labels=l0, font_family="Swiss911 UCm BT", font_size=30) labels1 = nx.draw_networkx_labels(G, pos, labels=l1, font_family="Swiss911 UCm BT", font_size=15) labels2 = nx.draw_networkx_labels(G, pos, labels=l2, font_family="Swiss911 UCm BT", font_size=11, font_color="white") labels2 = nx.draw_networkx_labels(G, pos, labels=l2, font_family="Swiss911 UCm BT", font_size=10, font_color="black") # Save file_name = f"{save_path}/{search_term}_coocgraph.png" plt.savefig(file_name, transparent=True)
'axes.labelsize': 'xx-large', 'axes.titlesize': 24, 'xtick.labelsize': 'xx-large', 'ytick.labelsize': 'xx-large', 'figure.titleweight': 'normal', 'axes.titleweight': 'normal', 'axes.xmargin': 0.05, 'axes.ymargin': 0.05, 'axes.titlepad': 16.0, 'legend.frameon': True, 'legend.edgecolor': '#AC8AF5', 'legend.framealpha': 0.8, 'legend.fancybox': False, 'axes.prop_cycle': cycler('color', colors), 'axes.edgecolor': '#360C90', 'xtick.color': '#360C90', 'axes.labelcolor': '#360C90', 'ytick.color': '#360C90', 'text.color': '#360C90', 'grid.linestyle': '--', 'grid.linewidth': 1, 'grid.color': '#360C90', 'grid.alpha': 0.1 } plt.rcParams.update(params) font_files = font_manager.findSystemFonts(fontpaths=['assets/']) font_list = font_manager.createFontList(font_files) font_manager.fontManager.ttflist.extend(font_list)
try: _base_dir = os.path.dirname(os.path.abspath(__file__)) except NameError: _base_dir = None with open(os.path.join(_base_dir, '.VERSION')) as version_file: __version__ = version_file.read().strip() # Make package fonts available to matplotlib import os import matplotlib.font_manager as fm path = os.path.abspath(__file__) font_path = "/" + "/".join(path.split("/")[:-1]) + "/fonts/" font_files = fm.findSystemFonts(fontpaths=font_path) font_list = fm.createFontList(font_files) fm.fontManager.ttflist.extend(font_list) # Log submodules __all__ = [ cms, atlas, lhcb, plot, style, tools, label, # Log plot functions 'histplot', 'hist2dplot', 'mpl_magic',
from matplotlib import use, rcParams, cm, colors, font_manager use('Agg') import numpy, pylab import ctypes from sys import path from pathlib import Path home_dir = str(Path.home()) font_dirs = [ '{:s}/.local/etc/fonts'.format(home_dir), ] font_recs = font_manager.findSystemFonts(fontpaths=font_dirs) font_list = font_manager.createFontList(font_recs) font_manager.fontManager.ttflist.extend(font_list) rcParams.update({ 'figure.autolayout': False, 'figure.figsize': (8, 6), 'figure.facecolor': [1, 1, 1, 0], 'figure.edgecolor': [0, 0, 0, 1], 'figure.dpi': 100, 'text.usetex': True, 'text.latex.preamble': [ r'\usepackage{amsmath,amssymb,bm}', # r'\usepackage{siunitx}', ], # Plot boundary properties 'axes.linewidth':
import os import matplotlib.font_manager as font_manager import matplotlib.pyplot as plt import seaborn as sns font_paths = font_manager.findSystemFonts() font_objects = font_manager.createFontList(font_paths) font_names = [f.name for f in font_objects] print(font_names) from bayes_cv_prune.StanModel import BayesStanPruner """Plot the first 3 graphs in section 2.3: The prior. """ def plot(): CURRENT_PATH = os.path.dirname(os.path.realpath(__file__)) # Final model with exponential hyperprior MODEL_PATHS = os.path.join(CURRENT_PATH, "..", "bayes_cv_prune", "stan_models") model0 = BayesStanPruner(os.path.join(MODEL_PATHS, "exp_new.stan"), seed=0).load() # Simulated sets of accuracy values prior_pred0 = model0.fit_predict([]) model1 = BayesStanPruner(os.path.join(MODEL_PATHS, "exp_new_no_one.stan"), seed=0).load()