0

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
NotAName
  • 3,821
  • 2
  • 29
  • 44
  • Could you show us some data from the dataframe? To be able to reproduce your problem. Thank you – powerPixie Oct 16 '19 at 07:46
  • Sorry about that. I've edited the original post with the data example. – NotAName Oct 16 '19 at 08:16
  • Can you show a reproducible example? See [mcve] or [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples), i.e. one single piece of runnable code from which the datatypes are clear; and also state the versions of matplotlib and pandas in use? – ImportanceOfBeingErnest Oct 16 '19 at 08:33

1 Answers1

1

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.

enter image description here

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.

powerPixie
  • 718
  • 9
  • 20
  • Thanks! Indexing worked! Now I have all three plots working well. I use ```plt.setp(plt.xticks()[1], rotation=30, ha='right')``` to get date ticks diagonally so that it's not such a huge mess. – NotAName Oct 16 '19 at 09:36