3

I have the following Matplotlib figure, with 2 charts:

That i created with the following code:

fig = plt.figure(facecolor='#131722',dpi=155, figsize=(8, 4))
ax1 = plt.subplot2grid((1,2), (0,0), facecolor='#131722')
ax2 = plt.subplot2grid((1,2), (0,1), facecolor='#131722')

Now i would like to add two charts, so ax3 and ax4, each needs to be below the two charts, they should have the same width of the two charts but half the height of the two bigger charts. How can i do that? I tried various solutions from here here but i'm struggling to get the expected output

1 Answer 1

4

You can achieve this using the gridspec_kw argument of plt.subplots. This allows you to specify the dimensions of the grid (in this case 2x2) and the ratio of the heights:

f, ax = plt.subplots(2, 2 , gridspec_kw={"height_ratios": [2, 1]})
for cAx in ax.flatten():
    cAx.set_facecolor('#131722')
f.savefig("test.png", facecolor='#131722')

enter image description here

Alternatively, you can also create a 3x2 grid and specify that the first two subplots need to span two rows:

fig = plt.figure(facecolor='#131722',dpi=155, figsize=(8, 6))
ax1 = plt.subplot2grid((3,2), (0,0), facecolor='#131722', rowspan=2)
ax2 = plt.subplot2grid((3,2), (0,1), facecolor='#131722', rowspan=2)
ax3 = plt.subplot2grid((3,2), (2,0), facecolor='#131722')
ax4 = plt.subplot2grid((3,2), (2,1), facecolor='#131722')
Sign up to request clarification or add additional context in comments.

1 Comment

This is awesome! Thank you a lot! I should really start using gridspec, looks way easier to use

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.