1

I have created a plot with 4 subplots and each subplot will show a different type of analyses on some infrasound data. This the code I have used to create the subplots:

gs = gridspec.GridSpec(2, 2, width_ratios=[1,1], height_ratios=[1,1])

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])

So far I have been able to input what I have wanted into the subplots, but I want to be able to input a pandas DataFrame plot into ax3 and I can't seem to do it. I have already written the pandas program and was just going to insert it into the larger script so it was shown in the subplot.

This is the line of code that is used to plot the pandas DataFrame plot:

df.plot(subplots=True, sharey=True, ylim=(0,(y_max*1.5)))

1 Answer 1

4

When plotting using pandas.Dataframe.plot you can choose the Axes object you would like to plot to with the keyword argument ax as shown below:

gs = gridspec.GridSpec(2, 2, width_ratios=[1,1], height_ratios=[1,1])

ax1 = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax3 = plt.subplot(gs[2])
ax4 = plt.subplot(gs[3])

# ...some other code that defines df...

df.plot(ax=ax3)

This will add your data to the ax3 object. Note that this will plot all of your columns into that one subplot, if you want one particular column then you could do df['my_col_name'].plot(ax=ax3).

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

4 Comments

That is really helpful, thank you. In my original DataFrame plot it has 4 subplots of the data and I can't seem to get it to plot it in the same into ax3. Is there a way of doing that?
So you wish to have 4 subplots inside ax3? Or ax3 is one of the 4 subplots and you wish to automatically plot each of your columns to an ax?
I want 4 subplots in ax3. The original likne of code that plots df divides it into 4 subplots but I can't seem to ad the (ax=ax3) part to this.
Unfortunately it looks like selecting the subplots option overrides your selection of ax and I don't know of a way to overcome this simply. If you want non-simply then you could create a more convoluted GridSpec object which instead has 7 ax: three will be ax1, ax2, and ax4 and the remaining four will be where ax3 currently is.

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.