如何在图的y轴上更改分类值的顺序

时间:2019-05-06 06:01:48

标签: python-3.x matplotlib

我只想更改此图中y轴上分类值的顺序:

Current plot

我要以下命令:

yax = ['empty',
       '1 to 2 range',
       '3 to 5 range',
       '6+ range',
       '1 person',
       '2 people',
       '3 people',
       '4 people',
       '5 people',
       '6 people',
       '7 people',
       '8 people',
       '9 people',
       '10 people']

我的代码是:

plt.figure(figsize=(11,6))
plt.plot(df.index.time, df['status'], marker='.', lw=0, ms=10, color = 'darkgreen')
plt.xlim(["00:00:00", "23:59:59"])
plt.show()

1 个答案:

答案 0 :(得分:1)

我建议添加一个分类列,然后按此列排序,例如:

import matplotlib.pyplot as plt
import pandas as pd

df=pd.DataFrame({'x':[0,1,2],'a':['high','low','middle']})

df['cat'] = pd.Categorical(df['a'], ['low', 'middle', 'high'])

df=df.sort_values(by='cat') ## try with and without this line

plt.figure(figsize=(11,6))
plt.plot(df['x'], df['a'].values, marker='.', lw=0, ms=10, color = 'darkgreen')

plt.show()