2

I would like to define two figures objects and then in a loop I would like to contribute to each of them. The following code will not work but it demonstrate how it works.

import matplotlib.pyplot as plt

plt1.figure()
plt1.xticks(rotation=90)

plt2.figure()
plt2.xticks(rotation=90)

for i in range(5):

   plt1.plot(xs_a[i], ys_a[i], label='line' + str(i))
   plt2.plot(xs_b[i], ys_b[i], label='line' + str(i))

plt1.savefig(fname_1)
plt2.savefig(fname_2)

I always perceived plt as an image object for which I can set some parameters (for example xticks) or to which I can add some curves. However, plt is a library that I import. So, what I should do when I need to define two or more figure objects?

1
  • 1
    Check out this similar question: stackoverflow.com/questions/6916978/…. I think the gist is that you can use something like fig1=plt.figure() and fig2=plt.figure() then use fig1 and fig2. Commented Apr 21, 2015 at 12:37

1 Answer 1

5

This should work. You first create two figure objects (fig1and fig2). For each of the figures, you add axes using the add_subplot method. Then, you can use these axes objects to plot whatever you want and set the parameters to your need (like the ticks).

import matplotlib.pyplot as plt

fig1 = plt.figure()
ax1 = fig1.add_subplot(111)

fig2 = plt.figure()
ax2 = fig2.add_subplot(111)

for i in range(5):

    ax1.plot(xs_a[i], ys_a[i], label='line' + str(i))
    ax2.plot(xs_b[i], ys_b[i], label='line' + str(i))

plt.setp( ax1.xaxis.get_majorticklabels(), rotation=90)
plt.setp( ax2.xaxis.get_majorticklabels(), rotation=90)

fig1.savefig(fname_1)
fig2.savefig(fname_2)
Sign up to request clarification or add additional context in comments.

Comments

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.