How to assign some matplotlib.axes._subplots.AxesSubplot side by side in another pyplot.subplots array

Viewed 1029

I have a function that returns a matplotlib.axes._subplots object, and I would like put some of these outputs side by side in another subplots, but I get the plots of these objects separately, and one empty plot for the new subplot. What I am looking for is just a plot including 4 plots. I know how to plot side by side by adding items one by one to subplots, but I would like to see if I can assign an axes object to another like following codes:

import matplotlib.pyplot as plt
import numpy as np
def plot_sub(x , y, title):
    fig, ax=plt.subplots(figsize=(5, 4))
    ax.plot(x,y)
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_title(title)
    return ax 
x=range(0,10)
y=[np.random.uniform(0,1,10) for i in range(4)]
fig1, ax1=plt.subplots(nrows=2,ncols=2)
for i in range(2):
    for j in range(2):
        ax1[i,j]=plot_sub(x,y[j+2*i], f'{i},{j}')
plt.show()

0 Answers
Related