Home > Enterprise >  Matplotlib not plotting all data points in bar graph
Matplotlib not plotting all data points in bar graph

Time:12-30

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

enter image description here

Actual Dataset Type

enter image description here

Actual Dataset Graph

Note, each week should be either green or red based on the data

enter image description here

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

enter image description here

CodePudding user response:

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.

  • Related