Matplotlib使用ax.set()旋转xticklabel

时间:2018-08-07 11:54:51

标签: python matplotlib plot

这是我一段时间以来一直感兴趣的东西。显然,我们可以创建一个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')

假设我宁愿使用字典来完成此操作。我可以使用

制作一个(几乎)相同的情节
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)

我还没有指定需要在props字典中旋转xticklabel。有没有办法在字典中包含此信息?

1 个答案:

答案 0 :(得分:1)

旋转是刻度标签的属性,而不是轴的属性。您可以使用plt.setp来设置任何对象的属性。

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