简体   繁体   English

Matplotlib使用ax.set()旋转xticklabel

[英]Matplotlib Rotate xticklabels using ax.set()

Here's something I've been interested in for a while. 这是我一段时间以来一直感兴趣的东西。 We can obviously create a matplotlib plot and rotate the xticklabels like such. 显然,我们可以创建一个matplotlib图并旋转xticklabel这样的。

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())

ax.set_title('Random Cumulative Sum')
ax.set_xlabel('Stages')
ax.set_xticks([0, 250, 500, 750, 1000])
ax.set_xticklabels(['one','two','three','four','five'],
                   rotation=30, fontsize='small')

Suppose I'd rather use a dictionary to accomplish this. 假设我宁愿使用字典来完成此操作。 I can make a (nearly) identical plot using 我可以使用(几乎)相同的情节

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.random.randn(1000).cumsum())

props = {'title': 'Random Cumulative Sum',
        'xlabel': 'stages',
        'xticks': [0,250,500,750,1000],
        'xticklabels': ['one','two','three','four','five']}

ax.set(**props)

I have not specified that the xticklabels need to be rotated in the props dictionary. 我没有指定需要在props字典中旋转xticklabels。 Is there a way to include this information in the dictionary? 有没有办法在字典中包含此信息?

Rotation is a property of the ticklabels, not the axes. 旋转是刻度标签的属性,而不是轴的属性。 You may use plt.setp to set the properties of any object(s). 您可以使用plt.setp设置任何对象的属性。

props = {"rotation" : 30}
plt.setp(ax.get_xticklabels(), **props)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM