matplotlib x轴刻度数调整
时间: 2023-08-24 10:07:13 浏览: 94
Python绘图Matplotlib之坐标轴及刻度总结
可以使用xticks()函数来调整x轴刻度数。以下是一些示例代码:
1. 设置固定数量的刻度数:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
plt.plot(x, y)
plt.xticks(range(1, 11, 2)) # 设置x轴刻度为1、3、5、7、9
plt.show()
```
2. 自动调整刻度数:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
plt.plot(x, y)
plt.locator_params(axis='x', nbins=5) # 自动调整x轴刻度数为5
plt.show()
```
3. 设置刻度标签:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
plt.plot(x, y)
plt.xticks([1, 3, 5, 7, 9], ['Jan', 'Mar', 'May', 'Jul', 'Sep']) # 设置x轴刻度标签
plt.show()
```
阅读全文