[英]Matplotlib: set label font without setting label
I would like to write a function that changes the font of all of the labels in a figure that I pass to it without actually changing the labels themselves. 我想编写一个函数来更改传递给它的图形中所有标签的字体,而无需实际更改标签本身。
This is useful for figures that I have already created and need to have a uniform font style. 这对于已经创建并且需要具有统一字体样式的图形很有用。
Currently I am using multiple font dictionaries like so: 目前,我正在使用多个字体字典,例如:
titleFont = {'family' : 'Arial',
'weight' : 'bold',
'size' : 20}
axesFont = {'family' : 'Arial',
'weight' : 'bold',
'size' : 18}
axesTickFont = {'family' : 'Arial',
'weight' : 'bold',
'size' : 16}`
And then setting the font sizes by using commands along the lines of: 然后通过以下命令使用命令来设置字体大小:
ax.set_title('My Plot',**titleFont)
The issue is that with the command above, I need to specify a plot title, when all I want to do is set the font style of an existing title. 问题是,使用上面的命令,当我要做的只是设置现有标题的字体样式时,我需要指定打印标题。
Something like this would be ideal: 这样的事情将是理想的:
def set_fonts(axis):
axis.set_title(**titleFont)
axis.set_ylabel(**axesFont)
axis.set_xlabel(**axesFont)
return axis
This is probably not the cleanest solution, but it should do what you want: 这可能不是最干净的解决方案,但它应该做您想要的:
def set_fonts(axis):
axis.set_title(axis.get_title(), **titleFont)
axis.set_ylabel(axis.get_ylabel(), **axesFont)
axis.set_xlabel(axis.get_xlabel(), **axesFont)
return axis
Otherwise you can directly access the relevant artist instances, which avoids reassigning the text: 否则,您可以直接访问相关的演出者实例,从而避免了重新分配文本:
def set_fonts(axis):
plt.setp(axis.title, **titleFont)
plt.setp(axis.xaxis.label, **axesFont)
plt.setp(axis.yaxis.label, **axesFont)
return axis
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.