I have the following dataframe I want to plot. I'm trying to have 2 subplots, one of them with two plots on it (with different y axes and sharing the x)

size = (20,10)
fig = plt.figure(figsize=size)
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax3 = ax2.twinx()

# the dataframe is obtained by other means, and has the following format:

Date        col1        col2    col3    col4        
2017-06-30  7813.9291   0.0000  0.0000  0.000000
2017-07-31  8222.1428   0.0000  0.0000  4.964809
2017-08-31  7010.1959   0.0000  0.0000  -17.288346
2017-09-30  5878.8063   0.0000  0.0000  -19.245227

df.iloc[:,0:3].plot.bar(figsize = size, ax = ax3)
df.iloc[:,-1].plot(figsize = size, ax = ax2)

The other plot with only one plot in the figure(in ax1) does not give any problems, so I won't post it here.

The problem is, when I run this code, only one the plots remains in the figure, (the last line), and the other just disappears. If I switch the order of the last two lines, then the other plot remains.

Any ideas why is this happening?

EDIT: I'm using a jupyter notebook, with pandas 0.24 and python 3.7

1 Answers

Quang Hoang On Best Solutions

The problem is that

df.iloc[:,0:3].plot.bar(figsize = size, ax = ax3)

sets ax3.xticks to 0, 1, ..., len(df)-1 while

df.iloc[:,-1].plot(figsize = size, ax = ax2)

uses df.index, in this case (I guess) the timestamp, which are very very large integers. That's why you don't get to see one of the two plots. Suggestion is to re-do the line plot:

df.iloc[:,0:3].plot.bar(figsize = size, ax = ax3)
ax2.plot(range(len(df)), df.iloc[:,-1])

You can try some other approaches in my answer here.