Reputation: 4347
I am working on creating a plot featuring two line plots - planned and actual production, and a bar chart showing the difference between those.
I've created line plots:
ax.plot_date(df['Date'], df['Planned_x'], 'b-', c='red')
ax.plot_date(df['Date'], df['Actuals'], 'b-', c='blue')
Then later I saw in an old question on Stack Overflow that incorporating bar chart will be easier if I switched plot_date for normal plot and passed ax.xaxis_date()
separately since this is all plot_date does and so I've changed the code accordingly.
It all works fine so long as I don't try to add the bar chart, but as soon as I do it like so:
ax.plot(df['Date'], df['Planned_x'], 'b-', c='red')
ax.plot(df['Date'], df['Actuals'], 'b-', c='blue')
ax.bar(df['Date'], df['Delta'], c='black', width=1)
ax.xaxis_date()
...I start getting TypeErrors: TypeError: the dtypes of parameters x (datetime64[ns]) and width (int32) are incompatible
I looked around, but most of all I found were bug reports on matplotlib and Pandas github pages and there were no solutions that were of any help to me.
EDIT: Here's the example data from the Dataframe:
Date Planned_x Actuals ... C2P (%) Planned_y Delta
766 2019-09-19 284.000000 439.0 ... NaN NaN -155.000000
767 2019-09-20 284.000000 469.0 ... NaN NaN -185.000000
768 2019-09-21 260.000000 240.0 ... NaN NaN 20.000000
769 2019-09-22 305.000000 229.0 ... NaN NaN 76.000000
770 2019-09-23 351.000000 225.0 ... 0.533391 NaN 126.000000
771 2019-09-24 387.353430 1.0 ... NaN NaN 386.353430
772 2019-09-25 444.317519 152.0 ... NaN NaN 292.317519
773 2019-09-26 475.557830 300.0 ... NaN NaN 175.557830
774 2019-09-27 404.524517 150.0 ... NaN NaN 254.524517
775 2019-09-28 355.303705 550.0 ... NaN NaN -194.696295
Upvotes: 0
Views: 813
Reputation: 708
I used your data and indexed the date column, by tagging ".set_index('Date')"
df = pd.DataFrame(data,columns=['Date','Planned_x','Actuals','C2P','Planned_y','Delta']).set_index('Date')
I assume you already have some code to attach the plt board to your data, like:
ax = plt.subplot(111)
Then you trick the matplotlib, saying:
plt.bar(df.index, df.Delta)
Remember that your index is your dataframe column Date.
The only problem I see here is the messed up with the date labels, maybe you need to choose to show a reduced amount of data or so.
Upvotes: 1