bikuser
bikuser

Reputation: 2093

subplot in matplotlib.pyplot from function

I tried to create subplot by using some defined function, which returns directly plot. But I can't figure out why this is not working. for example:

my plot function is something like:

def plot_data(data):
    plt.plot(data)
    return plt.show()

suppose my data are:

data1 = np.random.rand(50)
data2 = np.random.rand(50)
data3 = np.random.rand(50)
data4 = np.random.rand(50)

and I am trying to create subplot with:

fig, ax = plt.subplots(nrows=2, ncols=2)

plt.subplot(2, 2, 1)
plot_data(data)

plt.subplot(2, 2, 2)
plot_data(data)

plt.subplot(2, 2, 3)
plot_data(data)

plt.subplot(2, 2, 4)
plot_data(data)

plt.tight_layout()
plt.show()

it returns : enter image description here

Upvotes: 1

Views: 2198

Answers (1)

arnaud
arnaud

Reputation: 3473

Just remove the return plt.show() in your plot_data() function. That'll work.

In that case, it might be useless to even use that extra function plot_data() and you can directly use plt.plot(data) :-)

Upvotes: 1

Related Questions