简体   繁体   English

Matplotlib添加轮廓图颜色条

[英]Matplotlib adding a contour plot colorbar

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. 我试过cbar = plt.colorbar()我收到错误:找不到可映射用于颜色条创建。 First define a mappable such as an image (with imshow) or a contour set (with contourf). 首先定义一个可映射的对象,例如图像(带有imshow)或轮廓集(带有轮廓f)。

If you have write access to the source, then you can change the line in scatter_contour to return the contour set you need: 如果您具有对源代码的写访问权,那么可以更改scatter_contour中的行以返回所需的轮廓集:

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. 如果不能,则必须尝试跟踪轴中保存的集合的引用-不立即确定如何执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM