0

I'd like to add labels to a grid of plots, only at the most bottom axes. I don't get the originally created axes, so I have to query them by fig.get_axes() (this means for the example below: I can not access the value ax). Nevertheless all of these axes have been created by subplots with a shared x-axis.

As an example use this simplified example:

# This is just for illustration purpose I have no 
# knowledge how the original plot was created

import numpy as np
import pylab as plt
fig, ax = plt.subplots(2, 3, sharex=True)
x = np.arange(0, 5, 0.1)
n = 0
for i in xrange(2):
    for j in xrange(3):
        y = x ** n
        ax[i, j].plot(x, y)
        n += 1

# My real work starts here
axes = fig.get_axes()
print axes
plt.show()

I'd now like to use the data retrieved in axes to get the three bottom axes and label them. Note: The number of columns may vary and is unknown.

MichaelA
  • 1,866
  • 2
  • 23
  • 38

1 Answers1

0

Don't permanently flatten the array of axes. It's shape will tell you what to label. In fact, use ravel instead of flatten to get a view of the original ax array instead of making an unncessary copy: What is the difference between flatten and ravel functions in numpy?

import numpy as np
import pylab as plt
fig, ax = plt.subplots(2, 3, sharex=True)
x = np.arange(0, 5, 0.1)
fax = ax.ravel()
for i in xrange(6):
    y = x ** i
    fax[i].plot(x, y)
for i in range(ax.shape[1]):
    ax[-1, i].set_xlabel("Some junk {}".format(i))
plt.show()
Community
  • 1
  • 1
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Yes, this would work if I could access the original `ax` value, but this is not possible, I'll have to rely on the `fig.get_axes()` method. I tried to clarify the question above and deleted the misleading `ax.flatten()` statement (None the less: thanks for the information about ravel, didn't know that before) – MichaelA Jan 28 '16 at 07:51