python - Matplotlib使刻度标签字体大小为sm
在matplotlib图中,如何使用ax1.set_xticklabels()缩小刻度标签的字体大小?
此外,如何将其从水平旋转到垂直?
10个解决方案
359 votes
实际上有一种更简单的方法。 我刚发现:
import matplotlib.pyplot as plt
# We prepare the plot
fig = plt.figure(1)
# We define a fake subplot that is in fact only the plot.
plot = fig.add_subplot(111)
# We change the fontsize of minor ticks label
plot.tick_params(axis='both', which='major', labelsize=10)
plot.tick_params(axis='both', which='minor', labelsize=8)
这只回答了您的问题的一部分label。
Autiwa answered 2019-03-07T21:05:07Z
115 votes
要同时指定字体大小和旋转,请尝试以下操作:
plt.xticks(fontsize=14, rotation=90)
scottlittle answered 2019-03-07T21:05:31Z
93 votes
请注意,较新版本的MPL具有此任务的快捷方式。 这个问题的另一个答案显示了一个例子:[https://stackoverflow.com/a/11386056/42346]
以下代码仅用于说明目的,可能不一定经过优化。
import matplotlib.pyplot as plt
import numpy as np
def xticklabels_example():
fig = plt.figure()
x = np.arange(20)
y1 = np.cos(x)
y2 = (x**2)
y3 = (x**3)
yn = (y1,y2,y3)
COLORS = ('b','g','k')
for i,y in enumerate(yn):
ax = fig.add_subplot(len(yn),1,i+1)
ax.plot(x, y, ls='solid', color=COLORS[i])
if i != len(yn) - 1:
# all but last
ax.set_xticklabels( () )
else:
for tick in ax.xaxis.get_major_ticks():
tick.label.set_fontsize(14)
# specify integer or one of preset strings, e.g.
#tick.label.set_fontsize('x-small')
tick.label.set_rotation('vertical')
fig.suptitle('Matplotlib xticklabels Example')
plt.show()
if __name__ == '__main__':
xticklabels_example()
bernie answered 2019-03-07T21:04:37Z
43 votes
或者,您可以这样做:
import matplotlib as mpl
label_size = 8
mpl.rcParams['xtick.labelsize'] = label_size
user2468932 answered 2019-03-07T21:05:54Z
19 votes
在Matplotlib的当前版本中,您可以执行axis.set_xticklabels(labels, fontsize='small')。
Christoph answered 2019-03-07T21:06:18Z
13 votes
对于较小的字体,我使用
ax1.set_xticklabels(xticklabels, fontsize=7)
它的工作原理!
Pengyao answered 2019-03-07T21:06:50Z
12 votes
您还可以使用如下行更改标签显示参数,例如fontsize:
zed = [tick.label.set_fontsize(14) for tick in ax.yaxis.get_major_ticks()]
ATX_Bigfoot answered 2019-03-07T21:07:14Z
12 votes
另一种选择
我有两个并排的地块,想分别调整刻度标签。
上面的解决方案很接近,但它们并没有为我提供帮助。 我从这个matplotlib页面找到了我的解决方案。
ax.xaxis.set_tick_params(labelsize=20)
这样做的伎俩直截了当。 对于我的用例,右边的情节需要调整。 对于我创建新刻度标签后左侧的绘图,我能够在与设置标签相同的过程中调整字体。
即
ax1.set_xticklabels(ax1_x, fontsize=15)
ax1.set_yticklabels(ax1_y, fontsize=15)
因此,我用于正确的情节,
ax2.xaxis.set_tick_params(labelsize=24)
ax2.yaxis.set_tick_params(labelsize=24)
一个小巧的......我知道......但我希望这有助于某人:)
如果有人知道如何调整数量级标签的字体大小,则奖励积分。
Kyle Swanson answered 2019-03-07T21:08:14Z
6 votes
plt.tick_params(axis ='both',which ='minor',labelsize = 12)
jingweimo answered 2019-03-07T21:08:38Z
4 votes
以下对我有用:
ax2.xaxis.set_tick_params(labelsize=7)
ax2.yaxis.set_tick_params(labelsize=7)
以上的优点是您不需要提供array的labels并使用axes上的任何数据。
Cryptoman answered 2019-03-07T21:09:06Z