从其他轴设置刻度标签字体属性

Set Tick Label Font Properties from other axis

我正在使用 twiny() 在图形上方创建第二个 x 轴,并明确设置刻度标签。这样做之后,第二个 x 轴的字体属性与图中的其余部分不匹配。我怎样才能让它们匹配?

这是我创建第二个轴的方法:

topXaxis = ax.twiny()
topXaxis.set_xticks(new_tick_locations)
topXaxis.set_xticklabels(new_tick_numbers)

结果如下:

我试过这样的事情,但没有任何反应:

labels = ax.get_xticklabels()
l = labels[0]
props = l.get_fontproperties()
for label in topXaxis.get_xticklabels():
    label.set_fontproperties(props)

如何设置顶部的 x 轴与底部的 x 轴具有相同的字体属性?

要在整个 graph/plot 中指定通用字体,您可以使用 rc params (You can find more documentation here) 并设置通用字体和字体大小。

示例:

plt.rc('font', family='serif', size='12.0')

一旦你在开始时设置了这个,在你的整个代码中,所有的字符都会有字体 serif 和一个共同的字体大小。

您可以找到更多可以使用 rc params 更改的字体和其他属性。