在matplotlib中从 pandas 系列绘制线图时显示分类的x轴值

问题描述:

如何显示[a,b,c]的x轴值?

How do I get the x-axis values of [a, b, c] to show up?

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series([1, 2, 10], index=['a', 'b', 'c'])
s.plot()
plt.show()

您可以使用plt.xticks来显示xtick标签:

You can get your xtick labels to show using plt.xticks:

import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series([1, 2, 10], index=['a', 'b', 'c'])
s.plot()
plt.xticks(np.arange(len(s.index)), s.index)
plt.show()

输出: