Matplotlib作图中坐标轴字体的加粗斜体格式

您所在的位置:网站首页 navicat设置字体大小没变化 Matplotlib作图中坐标轴字体的加粗斜体格式

Matplotlib作图中坐标轴字体的加粗斜体格式

2023-06-13 01:29| 来源: 网络整理| 查看: 265

Matplotlib作图中坐标轴字体的加粗斜体格式 背景介绍

科研作图中,坐标轴的字体、字号等参数有常用的选择,我常用的参数如下,坐标轴名称的字体是roman,字体需要加粗;坐标轴刻度的

字体是roman,字体需要斜体(italic),坐标轴名称和刻度的字号相同。

实现方法

坐标轴名称通过ax.set_xlabel函数进行设置,典型的设置方法如下,ax.set_xlabel(“Distances / m”, font1)```,其中,第一个形式参数是坐标轴名称,第二个形式参数是名称的字体、字号等参数的设置。

坐标轴刻度的设置通过ax.get_xticklabels()、ax.get_yticklabels()进行设置,上述两个函数分别返回X轴和Y轴的坐标轴刻度,在Python的Matplotlib中被称为"tick label"。

示例程序如下,

import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager def ReadFile(path, fileName): print("Data File Path : {}".format(path)) print("File Name : {}".format(fileName)) # read f = open(path+fileName) lines = f.readlines() distances = [] for line in lines: line = line.strip().split() distances.append(float(line[0])*1000.) distances = np.array(distances) return distances if __name__=='__main__': print('hello') for f in matplotlib.font_manager.fontManager.ttflist: print(f.name) path = '../step4-cylinderSegmentation-tiltedCamera/build/' fileName = 'data_distances.txt' distances = ReadFile(path, fileName) hist, bin_edges = np.histogram(distances) print(hist) print(bin_edges) # get width of each bin binWidth = (bin_edges[1] - bin_edges[0]) # get the center of each bin bin_centers = bin_edges[:-1] + binWidth # label of the bar plot label = str(round(distances.mean(),2)) + '$\pm$' + str(round(distances.std(),2)) + 'mm' # figure figsize = 8,6 figure, ax = plt.subplots(figsize=figsize) # draw a bar plot ax.bar(bin_centers, hist, width=binWidth*0.8, label=label) # set font for labels in two axis font1 = {'family': 'Nimbus Roman', 'weight': 'bold', 'style':'normal', 'size': 15, } font2 = {'family': 'Nimbus Roman', 'weight': 'normal', 'style':'italic', 'size': 16, } ax.set_xlabel("Distances / mm", font1) ax.set_ylabel("Counts", font1) #设置坐标刻度值的大小以及刻度值的字体 plt.tick_params(labelsize=15) labels = ax.get_xticklabels() + ax.get_yticklabels() [label.set_fontname('Nimbus Roman') for label in labels] [label.set_fontstyle('italic') for label in labels] # set title plt.title("Distribution of Distances from pionts in the cloud to Cylinder", fontsize=12) # draw legend plt.legend() # tight layout plt.tight_layout() # save figure plt.savefig('figure_step4_distances.png') # print figure on screen plt.show()

效果如图, 在这里插入图片描述



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3