Reputation: 159
I want to plot two separate quantities while running through a loop. I want to create a separate figure window for each quantity, such that each iteration of the loop creates one subplot for each quantity.
Basically, I want my code to do something like this:
import numpy as np
import matplotlib.pyplot as plt
nr = [10, 15, 20, 25, 35, 50]
fig1 = plt.figure(1)
fig2 = plt.figure(2)
for y in range(len(nr)):
m = np.arange(y+1)
n = (y+1)*np.arange(y+1)
fig1.subplot(3,2,y+1)
fig1.plot(m,n, 'b')
fig1.title('y=%s'%y)
m1 = np.square(np.arange(y+1))
n1 = (y+1)*np.arange(y+1)
fig2.subplot(3,2,y+1)
fig2.plot(m1,n1, 'r')
fig2.title('y=%s'%y)
fig1.show()
fig2.show()
This code doesn't work; gives me the error message that 'Figure' object has no attribute 'subplot'. I've tried many variations on this link - http://matplotlib.org/api/pyplot_api.html, but I am unable to understand how to do it the right way. In the output, I want two figure windows, each with 6 subplots, such that the first one contains plots of m vs n, and the second one contains plots of m1 vs n1.
Upvotes: 2
Views: 6640
Reputation: 4926
Okay, long explanation because there are multiple issues here.
The biggest problem you are running into is that there are multiple ways to handle things in matplotlib. In fact, there are effectively multiple interfaces. The easiest and most commonly used method is to just create your plot using pyplot
and its methods like pyplot.subplot
and pyplot.plot
. This can work well for quick plots, but will not work well for your situation.
Since you want to create two figures and alternate plotting to those figures, you are going to want to use the more powerful objects in pyplot
. You have gotten part way there yourself, but I'll try to help you with the last part.
You are good up until here:
import numpy as np
import matplotlib.pyplot as plt
nr = [10, 15, 20, 25, 35, 50]
fig1 = plt.figure(1)
fig2 = plt.figure(2)
for y in range(len(nr)):
m = np.arange(y+1)
n = (y+1)*np.arange(y+1)
but when you try to use the methods of Figure
, you are getting confused and trying to use similar methods that belong to pyplot
. The next portion should be rewritten as:
ax1 = fig1.add_subplot(3,2,y)
ax1.plot(m,n, 'b')
ax1.set_title('y=%s'%y)
m1 = np.square(np.arange(y+1))
n1 = (y+1)*np.arange(y+1)
ax2 = fig2.add_subplot(3,2,y)
ax2.plot(m1,n1, 'r')
ax2.set_title('y=%s'%y)
Here, what you have done is capture the Axes
instance that is returned from add_subplot()
. Then you plot onto the Axes
instance. Also, when specifying where you want the subplot to be placed (the third input to Figure.add_subplot()
), you do not want to use y+1
because that would start at 1
and end at 6
which would go out of the available range of 0-5. Remember that Python indices start with zero.
Finally, to show the figures you just created, you can either call pyplot.show()
like this:
plt.show()
or you can save the figures to files like this:
fig1.savefig('fig1.png')
fig2.savefig('fig2.png')
The resulting figures look like this:
Upvotes: 4