python画图横坐标自动倾斜
时间: 2023-10-14 18:22:41 浏览: 175
如果横坐标标签文字过长,可以使用matplotlib中的`set_xticklabels`函数将标签进行旋转,使其更易于阅读。下面是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 生成示例数据
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
# 绘制图表
plt.plot(x, y)
# 获取当前坐标轴对象
ax = plt.gca()
# 将横坐标标签进行旋转,使其更易于阅读
ax.set_xticklabels(['Label 1', 'Label 2', 'Label 3', 'Label 4', 'Label 5'], rotation=30)
# 显示图表
plt.show()
```
在上面的示例代码中,`set_xticklabels`函数的第一个参数是横坐标标签的列表,第二个参数`rotation`表示旋转角度。通过调整`rotation`的值,可以使标签旋转到合适的角度。
阅读全文
相关推荐











