简体   繁体   English

Python 3.5 Matplotlib等高线图例

[英]python 3.5 matplotlib contour plot legend

Trying to add a legend to my contour plot: 尝试在图例中添加图例:

Here is the relevant code part i am having problem with: 这是我遇到问题的相关代码部分:

plt.figure()
CS = plt.contourf(gg, cc, zz_miss)
CS.clabel()
lbl = CS.cl_cvalues
plt.xlabel('gamma')
plt.ylabel('C = 1 / lambda')
plt.legend((lbl), loc= 'upper right')

plt.show()

The legend labels of the legend are correct, but why are the pertineent colors all smeared and out of place? 图例的图例标签是正确的,但是为什么常绿的颜色全部被涂抹并不合适?

It's putting the literal polygons of your contour plot as they were markers. 它将轮廓图的文字多边形作为标记放置。 It's a problem. 这是一个问题。

I advise you to create a manual replacement for the color legend. 我建议您为颜色图例创建手动替换。 Here is the modification you need to make into your code (generated synthetic data): 这是您需要对代码(生成的合成数据)进行的修改:

import matplotlib.pyplot as plt

plt.figure()
xx,yy = np.meshgrid(range(100),range(100))
gg = np.sqrt(xx*2+yy*2)
CS = plt.contourf(gg) #, cc, zz_miss)
proxy = [plt.Rectangle((0,0),1,1,fc = pc.get_facecolor()[0]) for pc in CS.collections]

plt.legend(proxy, [str(i) for i in range(8)])
plt.xlabel('gamma')
plt.ylabel('C = 1 / lambda')

plt.show()

, the result is this: ,结果是这样的:

手动构建轮廓线的颜色图例

Thanks. 谢谢。
Also, found a slightly easier one: see result 另外,发现了一个稍微容易一些的: 查看结果

CS = plt.contourf(gg, cc, zz_miss, alpha= 1)
nm, lbl = CS.legend_elements()
plt.legend(nm, lbl, title= 'MyTitle', fontsize= 8) 
plt.xlabel('gamma')
plt.ylabel('C = 1 / lambda')

here is the result: 结果如下:

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

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