1

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

2
  • 2
    It might be worth noting that it is not necessary to have calls to plt.subplot() and fig, ax = plt.subplots(nrows=2, ncols=2) Commented Feb 16, 2018 at 12:59
  • 1
    Indeed that plt.subplots(nrows=2, ncols=2) is unused. Commented Feb 16, 2018 at 13:01

1 Answer 1

1

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) :-)

Sign up to request clarification or add additional context in comments.

2 Comments

haha it works for me...Actually, in my real function, I have many calculations so I have defined function to plot..anyways thanks a lot :)
Heheh yes sure, if you have additional stuffs to do functions is a good habit.

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.