I have this two dataframe:
x1=[{"dates":'2018-01-31',"rev":-2},
{"dates":'2018-02-28',"rev":-5},
{"dates":'2018-03-31',"rev":-7},
{"dates":'2018-04-30',"rev":-8},
{"dates":'2018-05-31',"rev":-9},
{"dates":'2018-06-30',"rev":-2},
{"dates":'2018-07-31',"rev":1},
{"dates":'2018-08-31',"rev":2},
{"dates":'2018-09-30',"rev":3},
{"dates":'2018-10-31',"rev":4},
{"dates":'2018-11-30',"rev":4},
{"dates":'2018-12-31',"rev":5}]
x2=[{"dates":'2018-01-31',"rev":-5},
{"dates":'2018-02-28',"rev":-9},
{"dates":'2018-03-31',"rev":-9},
{"dates":'2018-04-30',"rev":-6},
{"dates":'2018-05-31',"rev":-1},
{"dates":'2018-06-30',"rev":-2},
{"dates":'2018-07-31',"rev":-14},
{"dates":'2018-08-31',"rev":2},
{"dates":'2018-09-30',"rev":3},
{"dates":'2018-10-31',"rev":4},
{"dates":'2018-11-30',"rev":-4},
{"dates":'2018-12-31',"rev":5}]
df1=pd.DataFrame(x1)
df1["dates"]=pd.to_datetime(df1.dates)
df2=pd.DataFrame(x2)
df2["dates"]=pd.to_datetime(df2.dates)
(Thanks Flavio Moraes )
I would like to plot then as a bar plot. The matplotlib guide suggests to do something like the following:
width = 0.35
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, y1, width, label='y1')
rects2 = ax.bar(x + width/2, y2, width, label='y2')
However, this seems to not working with dates. Indeed I have tried:
ax = plt.axes()
widthb = 10
rects1 = ax.bar(df1.dates - widthb/2, df1.rev,color='blue', width=widthb)
rects2 = ax.bar(df2.dates + widthb/2, df2.rev,color='red', width=widthb)
ax.xaxis_date()
As expected, I get the following error:
unsupported operand type(s) for -: 'DatetimeIndex' and 'float'
Do you have any suggestion to overcome this problem and simultaneously to have a beautiful plot?
Thanks

DataFrame.plot.bar.axesand pass it to pandas though, let pandas do the lion's share of the plotting, then customize the output to your heart's content w/ matplotlib.