1

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

7
  • I don't understand the problem. It works fine for me. The last plot is correct: There is a bar chart and a line. What else do you expect? col2 is zero so you don't see any bars for col2. The only bars will come from col1 Commented May 16, 2019 at 14:55
  • My problem is that I either see the barplot or the line, but not both at the same time. I'm using a jupyter notebook, last version of pandas and python 3.7 Commented May 16, 2019 at 14:58
  • Strange. It works fine for me in 3.6.5 python, pandas 0.23.0 Commented May 16, 2019 at 15:02
  • Try the following: df.iloc[:,0:3].plot.bar(ax=ax2, secondary_y=True) and then df.iloc[:,-1].plot(ax=ax2) Commented May 16, 2019 at 15:05
  • Is your data frame indexed by Date? Commented May 16, 2019 at 15:09

1 Answer 1

1

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.

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

Comments

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.