简体   繁体   中英

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.

When I do this on my dataset, the matplotlib does not have a bar color for each date.

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?

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

在此处输入图像描述

The default width of a bar is, according to 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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