0

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

4
  • Why not just use pandas's plotting methods, i.e. DataFrame.plot.bar. Commented Nov 16, 2020 at 21:53
  • because pandas is not as customizable as matplolib. Commented Nov 16, 2020 at 22:08
  • You can create the axes and 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. Commented Nov 16, 2020 at 22:08
  • it's quite difficult to do that unless you do without some important feature of matplolib. For example I have named a column as "ex_ax". Due to the fact that I would like to plot according to latex style (matplotlib.rc('text', usetex=True)), pandas was not able to handle it in plot.bar . Commented Nov 17, 2020 at 23:35

1 Answer 1

1

Let's try merge and plot:

ax = df1.merge(df2, on='dates', how='outer').plot.bar(x='dates')
# other format with `ax`
ax.xaxis_date()

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot. I will use this solution, although I prefer to plot with ax and matplolib features.
@diedro ax is a Matplotlib axis instance. You can do everything you would do with other axis instance. ax.xaxis_date() is an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.