【问题标题】:Matplotlib Rotate xticklabels using ax.set()Matplotlib 使用 ax.set() 旋转 xticklabels
【发布时间】:2018-08-07 11:54:50
【问题描述】:

这是我一直感兴趣的东西。我们显然可以创建一个 matplotlib 图并像这样旋转 xticklabels。

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)

我没有指定 xticklabels 需要在 props 字典中旋转。有没有办法将这些信息包含在字典中?

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

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

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

    【讨论】:

    • 我喜欢plt.setp
    猜你喜欢
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2018-06-27
    • 2019-02-01
    • 2020-05-07
    相关资源
    最近更新 更多