Matplotlib: any way to get existing color panels?

In matplotlib object oriented style, you can get the current axes, lines and images in an existing drawing:

fig.axes
fig.axes[0].lines
fig.axes[0].images

      

But I haven't found a way to get the existing colorbars, I need to assign a name to the colorbar when I first create it:

cbar = fig.colorbar(image)

      

Is there a way to get the colorbar objects in a given shape if I haven't given them names?

+3


source to share


1 answer


The problem is that the color bar is added as "just another" axis, so it will be specified with "normal" axes.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(6,6)
fig = plt.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
cax = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
print "Before adding the colorbar:"
print fig.axes
fig.colorbar(cax)
print "After adding the colorbar:"
print fig.axes

      

For me this gives the result:



Before adding the colorbar:
[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000080D1D68>]
After adding the colorbar:
[<matplotlib.axes._subplots.AxesSubplot object at 0x00000000080D1D68>,
<matplotl ib.axes._subplots.AxesSubplot object at 0x0000000008268390>]

      

That is, your shape has two axes, the second is a new color bar.

Edit: The code is based on the answer given here: fooobar.com/questions/222377 / ...

+1


source







All Articles