I'm trying to make a plot where the various subplots all share a colorbar similar to this answer. The problem that I'm running into is that in my script, I'm calling a function which creates the QuadMesh
(generated from ax.pcolormesh
) instance and returns the Figure
and Axes
instances associated with it. Is there any way to get a handle on the QuadMesh instance from the Axes instance (or Figure instance)?
import matplotlib.pyplot as plt
import numpy as np
def foo(subplot):
data = np.random.random((100,100))
x,y = np.meshgrid(np.arange(101),np.arange(101))
fig = plt.gcf()
ax = fig.add_subplot(subplot)
quadmesh = ax.pcolormesh(x,y,data)
return fig,ax
fig = plt.figure()
f,a = foo(221)
f,a = foo(222)
f,a = foo(223)
f,a = foo(224)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85,0.15,0.05,0.7])
#fig.colorbar(magic_get_quadmesh,cax=cbar_ax)
plt.show()