Currently, I make a subplot (using matplotlib
) with
import matplotlib.pyplot as plt
fig, (ax1, ax2, ax3) = plt.subplots(3, sharex=True)
I want a variable number (n) of subplots in my plot named ax1, ax2, ... axn So it should be something like
mylist = []
for num in xrange(n):
mylist.append('ax'+str(num))
fig, (mylist) = plt.subplots(n, sharex=True)
However, this doesn't work because it just writes over "mylist" instead of using the names in the list.
Another solution would be if I didn't create the list and just used the names it creates for the axes, however, I do not know how to access these names. If I try to access the names with
mylist[0]
The console says
<matplotlib.axes._subplots.AxesSubplot object at 0x09BFAEF0>
How can I use this number to plot with a line such as
ax1.plot(data[:,0], data[:,1]
When you create multiple axes:
fig, axes = plt.subplots(3)
axes
will be a numpy array containing the 3 axes objects.
In order to use them to plot you can index the array to access the axes objects and plot as you would normally (ax.plot(...)
):
axes[0].plot(x, y)
If you have many axes then you can also loop through these axes and plot:
for ax in axes.flatten():
ax.plot(x, y)