1

Looking at matplotlib documentation, I found this example:

http://matplotlib.org/users/tight_layout_guide.html

import matplotlib.pyplot as plt

def example_plot(ax,pid, fontsize=12):
    ax.plot([1, 2])
    ax.locator_params(nbins=3)
    ax.set_xlabel('x-label', fontsize=fontsize)
    ax.set_ylabel('y-label', fontsize=fontsize)
    ax.set_title('Title'+str(pid), fontsize=fontsize)

plt.close('all')
fig = plt.figure()

ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax4 = plt.subplot(122)

example_plot(ax1,1)
example_plot(ax2,2)
example_plot(ax4,4)

plt.tight_layout()

plt.show()

which produce a 2 columns layout, on the left a column with two rows, and on the right a column with 1 row. This seems to match what the API of subplot is saying: http://matplotlib.org/api/pyplot_api.html

subplot(211) produces a subaxes in a figure which represents the top plot (i.e. the first) in a 2 row by 1 column notional grid (no grid actually exists, but conceptually this is how the returned subplot has been positioned).

I am now trying to add a row to the column of the left (for a total of 3 rows). From my understanding, that should do it:

import matplotlib.pyplot as plt

def example_plot(ax,pid, fontsize=12):
    ax.plot([1, 2])
    ax.locator_params(nbins=3)
    ax.set_xlabel('x-label', fontsize=fontsize)
    ax.set_ylabel('y-label', fontsize=fontsize)
    ax.set_title('Title'+str(pid), fontsize=fontsize)

plt.close('all')
fig = plt.figure()

ax1 = plt.subplot(321) # changed "2" by "3"
ax2 = plt.subplot(323) # changed "2" by "3"
ax3 = plt.subplot(324) # line added
ax4 = plt.subplot(122)

example_plot(ax1,1)
example_plot(ax2,2)
example_plot(ax3,3) # line added
example_plot(ax4,4)

plt.tight_layout()

plt.show()

There is something I must be doing wrong, because this display the right layout, but the third plot of the first column does not show up ....

1 Answer 1

2

When you do a 3 by 2 subplot grid, the plots in the left column are numbered 1, 3 and 5. Change the line to ax3 = plt.subplot(325) and it should work.

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

2 Comments

Thanks ... just to make sure I understand why, it's because the number 4 is for the "imaginary" (rowspanned) plot on the right column ?
Correct. You number the row first, in this case 1, 2; 3, 4; and 5, 6.

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.