我有一个简单的条形图,我正试图在matplotlib。它描绘了过去几个月的使用情况。我遇到的问题是,两个月的列表和月的用法的长度都是13。图中只显示了12个条
圈外
month_names = []
for i in range(13):
last_month = datetime.now() - relativedelta(months=i)
# Create string of month name and year...
text = format(last_month, '%b')
month_names.append(text)
month_names.reverse()
在循环内部(我知道像这样迭代df通常是否定的,但是我有一些特定的原因在这个问题的范围之外)
month_1 = df.at[i, 'cwat01'] if df.at[i, 'cwat01'] != '' else 0
month_2 = df.at[i, 'cwat02'] if df.at[i, 'cwat02'] != '' else 0
month_3 = df.at[i, 'cwat03'] if df.at[i, 'cwat03'] != '' else 0
month_4 = df.at[i, 'cwat04'] if df.at[i, 'cwat04'] != '' else 0
month_5 = df.at[i, 'cwat05'] if df.at[i, 'cwat05'] != '' else 0
month_6 = df.at[i, 'cwat06'] if df.at[i, 'cwat06'] != '' else 0
month_7 = df.at[i, 'cwat07'] if df.at[i, 'cwat07'] != '' else 0
month_8 = df.at[i, 'cwat08'] if df.at[i, 'cwat08'] != '' else 0
month_9 = df.at[i, 'cwat09'] if df.at[i, 'cwat09'] != '' else 0
month_10 = df.at[i, 'cwat10'] if df.at[i, 'cwat10'] != '' else 0
month_11 = df.at[i, 'cwat11'] if df.at[i, 'cwat11'] != '' else 0
month_12 = df.at[i, 'cwat12'] if df.at[i, 'cwat12'] != '' else 0
month_13 = df.at[i, 'cwat13'] if df.at[i, 'cwat13'] != '' else 0
months = [month_1,
month_2,
month_3,
month_4,
month_5,
month_6,
month_7,
month_8,
month_9,
month_10,
month_11,
month_12,
month_13]
绘图(也在循环内)
fig = plt.figure(figsize=(4, 1.2))
plt.rcParams.update({'font.size': 4})
plt.bar(month_names, months, color='grey', width = .5)
plt.ylabel('Gallons')
plt.xlabel('Month')
plt.rcParams["font.family"] = "Times New Roman"
ax = plt.axes()
ax.yaxis.grid()
imgdata = BytesIO()
fig.savefig(imgdata, format='svg')
imgdata.seek(0) # rewind the data
drawing = svg2rlg(imgdata)
renderPDF.draw(drawing, the_statement.canvas, 15, 210)
plt.close()
编辑:生成的图形
通过改变最后一个条形图的颜色,我发现它们相互重叠,因为月份名称是相同的。这不是我所期望的行为,但回想起来是有道理的
可能有一种奇特的方法来解决这个问题,但我只是把我的循环改成了这个
解决了问题!这让我很沮丧,所以我希望这能帮助一些人
相关问题 更多 >
编程相关推荐