gpt4 book ai didi

python - 使用 Matplotlib 将字体属性设置为刻度标签

转载 作者:太空狗 更新时间:2023-10-29 21:40:54 25 4
gpt4 key购买 nike

我正在尝试使用 matplotlib 将刻度标签的字体从标准字体更改为 Times New Roman。我认为这应该像更改标题和轴标签的字体一样简单,但事实证明这有点棘手。目前,我只是想为 x-tick 标签设置字体,这些标签是自动格式化的日期(这可能是我的问题之一,但我不确定)。

当运行下面的相关代码片段时,我收到错误“Axesssubplot 没有属性‘set_fontproperties’”。

ticks_font = matplotlib.font_manager.FontProperties(family='times new roman', style='normal', size=12, weight='normal', stretch='normal')

fig.autofmt_xdate()
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
for label in ax.get_xticklabels():
ax.set_fontproperties(ticks_font)

非常感谢任何帮助。

谢谢。

更新/编辑:啊,我觉得自己像个傻瓜。刚刚弄明白了,一旦我意识到这一点,它就很明显了。在上面代码片段的上下文中,答案是:

label.set_fontproperties(ticks_font)

最佳答案

如果您正在运行脚本,您也可以在运行带有参数列表的命令之前设置它。如下摘 self 的脚本:

fig_size = [fig_width, fig_height] 
tick_size = 9
fontlabel_size = 10.5
params = {
'backend': 'wxAgg',
'lines.markersize' : 2,
'axes.labelsize': fontlabel_size,
'text.fontsize': fontlabel_size,
'legend.fontsize': fontlabel_size,
'xtick.labelsize': tick_size,
'ytick.labelsize': tick_size,
'text.usetex': True,
'figure.figsize': fig_size
}
plt.rcParams.update(params)

或者如果你想像以前那样做,那么运行:

for label in ax.get_xticklabels() :
label.set_fontproperties(ticks_font)

它正在获取每个标签,其中包含您可以为其设置的所有文本属性。

关于python - 使用 Matplotlib 将字体属性设置为刻度标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7257372/

25 4 0