我正在尝试在matplotlib
中将分类信息绘制成一系列datetime
值。如果将分类数据表示为字符串,则可以使图形工作。然而,我希望Y轴是分类的,以便可以按正确顺序对其进行排序。
以下代码段展示了我目前的情况。在图中,将y
替换为y_cat
,matplotlib
会抛出错误:
import pandas as pd
import numpy as np
import calendar, datetime
import matplotlib as mpl
import matplotlib.pyplot as plt
# %matplotlib inline #for Jupyter notebooks
x = pd.date_range('2015/08/01', freq='4M', periods=9)
y = pd.Series(['Good', 'Very Good', 'Very Good', 'Average', 'Average', 'Good', 'Excellent', 'Excellent', 'Excellent'])
y_cat = pd.Categorical(y, categories=['Poor', 'Average', 'Good', 'Very Good', 'Excellent'], ordered=True)
fig, currAX = plt.subplots(figsize=(10, 4))
label_format = {'fontsize':12, 'fontweight':'bold'}
title_format = {'fontsize':15, 'fontweight':'bold'}
currAX.plot(x, y, color='crimson', linestyle='-')
#uncomment for error
#currAX.plot(x, y_cat, color='crimson', linestyle='-')
currAX.xaxis.set_major_formatter(mpl.dates.DateFormatter('%Y %b'))
currAX.spines['top'].set_visible(False)
currAX.spines['right'].set_visible(False)
currAX.spines['left'].set_visible(False)
currAX.set_xlabel('Review Period', **label_format)
currAX.set_ylabel('Review Rating', **label_format)
fig.tight_layout()
plt.show();
### ERROR:
IndexError: tuple index out of range
我希望看到一个图表,其中评论类别在Y轴上排序,从最好到最差,从上到下。