python - Matplotlib 使用 ax.set() 旋转 xticklabels
问题描述
这是我一段时间以来一直感兴趣的事情。我们显然可以创建一个 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 字典中旋转。有没有办法将这些信息包含在字典中?
解决方案
旋转是刻度标签的属性,而不是轴。您可以使用plt.setp
来设置任何对象的属性。
props = {"rotation" : 30}
plt.setp(ax.get_xticklabels(), **props)
推荐阅读
- python - Read_Csv() 没有给出预期的输出,如果我更新了 csv 中的任何单元格
- javascript - 取消选择并选择事件
- node.js - Next.js 的内存问题
- javascript - 为什么我的 Ajax 无法将数据保存到数据库中?Laravel 与 Ajax
- sql - 从其他表列检索记录
- javascript - 空白屏幕 - Matter.js
- druid - Druid Concat String 和 Measure 聚合
- javascript - MongoDB如何按索引替换数组项
- c - 使用 ARM Neon 内在函数实现 64 位变量的乘法时面临的问题
- rest-assured - 使用 RestArrured 触发时 API 返回 text/html 而不是 JSON