简体   繁体   English

Matplotlib 未在条形图中绘制所有数据点

[英]Matplotlib not plotting all data points in bar graph

I am working on a timeseries dataset.我正在研究时间序列数据集。 I want to have a line graph of sales & a horizontal line showing the average sales over the time period.我想要一个销售折线图和一条显示该时间段内平均销售额的水平线。 This part is working correctly.这部分工作正常。

I want to add a bargraph on top of the line graph showing when sales are above or below the average sales.我想在折线图顶部添加一个条形图,显示销售额何时高于或低于平均销售额。 Every date in the data is flagged as either Over or Under the average with a boolean T/F.使用 boolean T/F 将数据中的每个日期标记为高于或低于平均值。

When I do this on my dataset, the matplotlib does not have a bar color for each date.当我在我的数据集上执行此操作时,matplotlib 没有每个日期的条形颜色。

I went to make a toy set for this example, and when I run the code on the toy set, it works correctly.我为这个例子制作了一个玩具套装,当我在玩具套装上运行代码时,它可以正常工作。 I have included a snip of the actual data, the data type & the incorrect graphs.我已经包含了实际数据、数据类型和不正确图表的片段。

My questions is: what should I troubleshoot or look at to see why matplotlib is not graphing the bars correctly?我的问题是:我应该解决什么问题或查看为什么 matplotlib 没有正确绘制条形图?

Actual Dataset实际数据集

在此处输入图像描述

Actual Dataset Type实际数据集类型

在此处输入图像描述

Actual Dataset Graph实际数据集图

Note, each week should be either green or red based on the data请注意,每周应根据数据显示为绿色或红色

在此处输入图像描述

Code on Actual Dataset实际数据集上的代码

fig = plt.figure()
ax1 = fig.add_subplot()

x = test['FSCL_WK_BGN_DT']
y = test[['total_sls','avg_sales']]
off_season = test['Off_season']
on_season = test['On_season']

ax1.plot(x,y)


ax2 = ax1.twinx()

ax2.bar(x,on_season, color = 'green')
ax2.bar(x,off_season, color = 'red')

Toy Example玩具示例

dict_sample = {'date': [1,2,3,4,5],
              'sales':[100,200,300,100,200],
               'avg_sls': 180,
               'over':[False,True,True,False,True],
               'under':[True,False,False,True,False]
              }
df_sample = pd.DataFrame(dict_sample)
df_sample

Graph Toy Example Code图形玩具示例代码

ax1 = fig.add_subplot()

x = df_sample['date']
y = df_sample[['sales','avg_sls']]
over = df_sample['over']
under = df_sample['under']

ax1.plot(x,y)


ax2 = ax1.twinx()

ax2.bar(x,over, color = 'green')
ax2.bar(x,under, color = 'red')

Code Output Albeit ugly, the bars are correct代码 Output虽然很难看,但条形图是正确的

在此处输入图像描述

The default width of a bar is, according to https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html , 0.8.根据https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html ,条的默认宽度为 0.8。 As you use a time series x axis, this would become 0.8 seconds, which may be too small to display anything at times.当您使用时间序列 x 轴时,这将变为 0.8 秒,有时可能太小而无法显示任何内容。

Try尝试

ax2.bar(x,on_season, color = 'green', width=86400*0.8)
ax2.bar(x,off_season, color = 'red', width=86400*0.8)

The toy example uses a different, integer scale, thats why it works so nicely.玩具示例使用不同的 integer 比例尺,这就是它工作得如此出色的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM