xxxxxxxxxx
fig,ax = plt.subplots(3,2,figsize=(25,10),)
i,j = 0,0
for each in list_of_images:
img = cv.imread(each.name)
ax[i,j].imshow(img)
if j == 1:
j = 0
if i != 2:
i += 1
else:
j += 1
xxxxxxxxxx
fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 0].set_title("main")
axs[1, 0].plot(x, y**2)
axs[1, 0].set_title("shares x with main")
axs[1, 0].sharex(axs[0, 0])
axs[0, 1].plot(x + 1, y + 1)
axs[0, 1].set_title("unrelated")
axs[1, 1].plot(x + 2, y + 2)
axs[1, 1].set_title("also unrelated")
fig.tight_layout()
xxxxxxxxxx
# Create a facetted graph with 2 rows and 2 columns
ax = df.plot(subplots=True,
layout=(2,2),
sharex=False,
sharey=False,
linewidth=0.7,
fontsize=3,
legend=False)
# Alternative way
fig, ax = plt.subplots(2, 2)
ax[0, 0].plot( )
ax[0, 0].set_title("first row first column")
ax[1, 0].plot( )
ax[1, 0].sharex(ax[0, 0]) # Share same range with each other
ax[1, 0].set_xlabel("X label")
# ...and so on
fig.tight_layout()
plt.show()
xxxxxxxxxx
import matplotlib.pyplot as plt
# Create some sample data
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
# Create subplots with 2 rows and 1 column
fig, axs = plt.subplots(2, 1)
# Plot data on the first subplot
axs[0].plot(x, y1)
axs[0].set_title('Plot 1')
axs[0].set_xlabel('X-axis')
axs[0].set_ylabel('Y-axis')
# Plot data on the second subplot
axs[1].plot(x, y2)
axs[1].set_title('Plot 2')
axs[1].set_xlabel('X-axis')
axs[1].set_ylabel('Y-axis')
# Adjust the layout to avoid overlapping of labels and titles
fig.tight_layout()
# Show the plot
plt.show()
xxxxxxxxxx
# Example with 3 figures (more can be added)
fig, (fig1, fig2, fig3) = plt.subplots(1, 3) # subplots(row, columns)
fig1.plot(x,y)
fig2.plot(x,y)
fig3.plot(x,y)
plt.show()
xxxxxxxxxx
fig, axes = plt.subplots(1, 3, figsize=(15, 5), sharey=True)
fig.suptitle('Initial Pokemon - 1st Generation')
# Bulbasaur
sns.barplot(ax=axes[0], x=bulbasaur.index, y=bulbasaur.values)
axes[0].set_title(bulbasaur.name)
# Charmander
sns.barplot(ax=axes[1], x=charmander.index, y=charmander.values)
axes[1].set_title(charmander.name)
# Squirtle
sns.barplot(ax=axes[2], x=squirtle.index, y=squirtle.values)
axes[2].set_title(squirtle.name)
xxxxxxxxxx
# using the variable ax for single a Axes
fig, ax = plt.subplots()
# using the variable axs for multiple Axes
fig, axs = plt.subplots(2, 2)
# using tuple unpacking for multiple Axes
fig, (ax1, ax2) = plt.subplot(1, 2)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplot(2, 2)
xxxxxxxxxx
fig, (ax1, ax2) = plt.subplots(2)
fig.suptitle('Vertically stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)
#or
fig, (ax1, ax2) = plt.subplots(1,2) #in lines
xxxxxxxxxx
import matplotlib.pyplot as plt
ax1 = plt.subplot(131)
ax1.scatter([1, 2], [3, 4])
ax1.set_xlim([0, 5])
ax1.set_ylim([0, 5])
ax2 = plt.subplot(132)
ax2.scatter([1, 2],[3, 4])
ax2.set_xlim([0, 5])
ax2.set_ylim([0, 5])