pandas xtick、ytick
时间: 2023-12-29 16:04:20 浏览: 68
pandas提供了一些方法来自定义绘图中的xtick和ytick。下面是两种常用的方法:
方法一:使用set_xticks和set_yticks方法来设置刻度位置
```python
import pandas as pd
import matplotlib.pyplot as plt
# 创建一个DataFrame
data = {'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
# 绘制柱状图
ax = df.plot(kind='bar')
# 设置x轴刻度位置和标签
ax.set_xticks([0, 1, 2, 3, 4])
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'])
# 设置y轴刻度位置和标签
ax.set_yticks([0, 10, 20, 30, 40, 50])
ax.set_yticklabels(['zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty'])
# 添加标题和标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_title('My Plot')
# 显示图形
plt.show()
```
方法二:使用set_xticklabels和set_yticklabels方法来设置刻度标签
```python
import pandas as pd
import matplotlib.pyplot as plt
# 创建一个DataFrame
data = {'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
# 绘制柱状图
ax = df.plot(kind='bar')
# 设置x轴刻度标签
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five'], rotation=30, fontsize='small')
# 设置y轴刻度标签
ax.set_yticklabels(['zero', 'ten', 'twenty', 'thirty', 'forty', 'fifty'])
# 添加标题和标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_title('My Plot')
# 显示图形
plt.show()
```
阅读全文