Reputation: 530
If I do this
import numpy as np
import matplotlib.pyplot as plt
a=[1,2,3]
b=[3,4,5]
plt.figure(1)
plt.xlim(0,3)
plt.plot(b)
plt.figure(2)
plt.plot(a)
plt.show()
the choice of the x axes will be applied only to figure 1. What can I use to discriminate between the options that I want to be valid for only figure 1 or 2 and the ones that I want to be applied to both figures?
Clarification: I know that it is possible to call plt.xlim
several times. I was rather looking for some command of a form like
plt.apply_options_to(1,2)
and from that moment on every time I call plt.xlim
the option is applied to both figures and not only one of the two.
Upvotes: 0
Views: 52
Reputation: 339705
With pyplot, each command applies to the currently active figure or axes. This means you can easily loop over the figures and apply each command like
for i in (1,2):
plt.figure(i)
plt.xlim(0,3)
Now those are three lines of code. If the requirement is to use a single line of code, the following would be a solution
[plt.setp(plt.figure(i).axes[0], xlim=(0,3)) for i in plt.get_fignums() if i in (1,2)]
This is neither elegant nor easy to type, so when using pyplot I would recommend the first solution.
In general however I would recommend using the object oriented approach, where creating two figures would look like:
import matplotlib.pyplot as plt
a=[1,2,3]
b=[3,4,5]
fig, ax = plt.subplots()
ax.plot(b)
fig2, ax2 = plt.subplots()
ax2.plot(a)
plt.show()
Then the single line solution is also a bit more compact
plt.setp([ax,ax2], xlim=(0,3))
Upvotes: 1