相关文章推荐
骑白马的领带  ·  DataTable.Load 方法 ...·  1 年前    · 
爱笑的蜡烛  ·  报错 index:1,size:1 或 ...·  1 年前    · 
憨厚的仙人掌  ·  java - Maven ...·  2 年前    · 
鬼畜的绿茶  ·  Android ...·  2 年前    · 

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()

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

Matplotlib作图中坐标轴字体的加粗斜体格式背景介绍科研作图中,坐标轴的字体、字号等参数有常用的选择,我常用的参数如下,坐标轴名称的字体是roman,字体需要加粗;坐标轴刻度的字体是roman,字体需要斜体(italic),坐标轴名称和刻度的字号相同。实现方法坐标轴名称通过ax.set_xlabel函数进行设置,典型的设置方法如下,ax.set_xlabel(“Distances / m”, font1)```,其中,第一个形式参数是坐标轴名称,第二个形式参数是名称的字体、字号等参数的设置。 from pylab import * from matplotlib.ticker import MultipleLocator, FormatStrFormatter xmajorLocator = MultipleLocator(20) #将x主刻度标签设置为20的倍数 xmajorFormatter = FormatStrFormatter('%1.1f') #设置x轴标签文本的格式 xminorLocator = MultipleLocator(5) #将x轴次刻度标签设置为5的倍数 ymajorLocato import matplotlib.dates as mdates # 配置横坐标为日期格式 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) from datetime import datetime import matplotlib.dates as mdates import matplot
# 修改标题及x,y坐标轴字体及大小 plt.title("$MFCC_0$参数值滤波", fontsize=15,fontweight='bold') plt.xlabel("时间/s", fontsize=15,fontweight='bold') plt.ylabel("数值", fontsize=15,fontweight='bold') # 修改坐标轴字体及大小 plt.yticks(fontproperties='Times New Roman', size=15) plt.xticks(f
legend1=legend('FontName','Times New Roman',tmpName,'Interpreter', 'none','fontsize',fontSizeLegend,'Location','SouthEast');%可修改图例标注的位置,和字体,耶 title(titleName,'fontsize',fontSize,'FontName','Times New Roman'); xlabel(xL