2

Suppose I have:

import matplotlib.pyplot as plt

def generate_plot(data):
    plt.plot(...)
    return plt

we assume that _data is an iterable with length 4.

if "__name__" = "__main__":
   _data = ....
   plt.figure(1)
   for data_i, index in _data, range(4):
       plt.subplot(2,2,index+1)
       ??????? <---- what goes here?

   plt.savefig(...)

Can I call generate_plot such that the graph plotted is placed in position index? If so, how?

To be honest, I don't quite understand how the plt object works in Matplotlib.

1 Answer 1

2

You don't want to return plt. You can think of plt just as a way to access all of the plotting things in matplotlib, not as a "plot object". Generate your data first, then plot it. Say for example I have

import numpy as np
import matplotlib.pyplot as plt

def generate_data():
    return np.random.randint(10, size=10)

plt.figure(1)
for i in range(4):
    plt.subplot(2, 2, i + 1)
    plt.plot(generate_data())

plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

i currently have a very complicated function some else wrote which returns the plt. Let me try removing this and see.

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.