1
votes

Please let me know how I could include a contour plot color bar in the following figure:

from matplotlib import pyplot as plt
from astroML.plotting import scatter_contour
from astroML.datasets import fetch_sdss_S82standards

data = fetch_sdss_S82standards()

g = data['mmu_g']
r = data['mmu_r']
i = data['mmu_i']


fig, ax = plt.subplots(figsize=(5, 3.75))
scatter_contour(g - r, r - i, threshold=200, log_counts=True, ax=ax,
                histogram2d_args=dict(bins=40),
                plot_args=dict(marker=',', linestyle='none', color='black'),
                contour_args=dict(cmap=plt.cm.bone))

ax.set_xlabel(r'${\rm g - r}$')
ax.set_ylabel(r'${\rm r - i}$')

ax.set_xlim(-0.6, 2.5)
ax.set_ylim(-0.6, 2.5)

plt.show()

I tried cbar = plt.colorbar() I am getting the error: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

1
What does scatter_contour return? I'm unfamiliar with astroML, but if it returns something akin to what imshow returns, you can just assign it to a variable and pass it e.g. plt.colorbar(im)Ajean

1 Answers

2
votes

If you have write access to the source, then you can change the line in scatter_contour to return the contour set you need:

CS = ax.contourf(H.T, levels, extent=extent, **contour_args)

...

return CS

and then you can make your colorbar by calling

CS = scatter_contour(...)
colorbar(CS)

If you can't, then you'd have to try trace the references of the collections held in the axes - not immediately sure how to do this.