Coolcrab
Coolcrab

Reputation: 2723

Python Plot two subplots

I have a set of 12 plots that I want to save as one. The first set is a 3x3 subplot of 9 segments and the 2nd part is a 2x2 subplot of 4 segments. I've tried to add a subplot(121) between these two and I have tried to use figure(1) and figure(2), but both don't met me save the two images as one big image. Is there an easy way of doing this?

plt.subplot(331)
plt.imshow(getpoly(seg1),origin="lower")
plt.subplot(332)
plt.imshow(getpoly(seg2),origin="lower")
plt.subplot(333)
plt.imshow(getpoly(seg3),origin="lower")
plt.subplot(334)
plt.imshow(getpoly(seg4),origin="lower")
plt.subplot(335)
plt.imshow(getpoly(seg5),origin="lower")
plt.subplot(336)
plt.imshow(getpoly(seg6),origin="lower")
plt.subplot(337)
plt.imshow(getpoly(seg7),origin="lower")
plt.subplot(338)
plt.imshow(getpoly(seg8),origin="lower")
plt.subplot(339)
plt.imshow(getpoly(seg9),origin="lower")


plt.subplot(221)
plt.imshow(h1,origin="lower")
plt.colorbar()
plt.subplot(222)
plt.imshow(h2,origin="lower")
plt.colorbar()
plt.subplot(223)
plt.imshow(getpoly(h2),origin="lower")
plt.colorbar()  
plt.subplot(224)
plt.imshow(h1-getpoly(h2),origin="lower")
plt.colorbar()

Upvotes: 0

Views: 464

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339230

You would probably want to use gridspec with GridSpecFromSubplotSpec as shown here.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

gs = gridspec.GridSpec(1, 2)
gs0 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs[0])
gs1 = gridspec.GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])

fig = plt.figure()

for i in range(9):
    ax = fig.add_subplot(gs0[i//3, i%3])
    ax.imshow(np.random.rand(4,4))
    ax.set_xticks([]); ax.set_yticks([])

for i in range(4):
    ax = fig.add_subplot(gs1[i//2, i%2])
    ax.imshow(np.random.rand(4,4))
    ax.set_xticks([]); ax.set_yticks([])

plt.show()

enter image description here

Upvotes: 2

Related Questions