日期数组到轴单位的 matplotlib 转换失败



我正在遵循以下情节教程:

https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781849513265/1/ch01lvl1sec16/plotting-multiple-bar-charts

但是我受到错误的影响:

f'units: {x!r}'( from e matplotlib.units.ConversionError: 无法将值转换为轴单位: ['11-2019', '12-2019', 0] [7.199秒完成]

我相信这与将我的日期值数组转换为 x 刻度有关

#Arrays below represent the number of sales in each month from queries above in my code which i have not 
#included in this post. e.g. soya1 = [4,2,6].
#All arrays are the same length and are the same length as the date array
soya1, wholeMilk1, coconut1, semiSkimmed1, decaf1, skimmed1, dates = ([] for i in range(7))
dates = ['11-2019', '12-2019', '01-2020']
data = []
data.extend((soya1,wholeMilk1,coconut1,semiSkimmed1,decaf1,skimmed1))
gap = .8 / len(data)
for i, row in enumerate(data):
X = np.arange(len(row))
plt.bar(X + i * gap, row,
width = gap)
plt.xticks(np.arange(len(dates)),dates)
print('dates',dates)
plt.ylabel('Volume')
plt.xlabel('Dates')
plt.title('Milk Options')
plt.show()

首先尝试将日期从字符串转换为日期对象:

from datetime import datetime
dates = ['11-2019', '12-2019', '01-2020']
dates = [datetime.strptime(x, '%m-%Y') for x in dates]

最新更新