gpt4 book ai didi

python-3.x - 日期数组到轴单位的 Matplotlib 转换失败

转载 作者:行者123 更新时间:2023-12-04 15:36:44 25 4
gpt4 key购买 nike

我正在关注以下情节教程:

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

但我受以下错误的影响:

f'units: {x!r}') 来自 e
matplotlib.units.ConversionError:无法将值转换为轴单位:['11-2019', '12-2019', 0]
【7.199s完成】

我相信这与将我的日期值数组转换为 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]

关于python-3.x - 日期数组到轴单位的 Matplotlib 转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com