imshow的彩条和轮廓重叠

问题描述 投票:0回答:1

我正在尝试获得一个颜色条,其中包含imshow的值,并且轮廓线的3条线被过度绘制。 matplotlib示例之一显示的内容很接近,但是它们只有彩色的轮廓。该图像具有我想要的类型的颜色条。

这是我的代码和图像,它只是忽略了imshow的cmap的颜色条。

plt.imshow(Bho, origin='l')
plt.contour(Bho, [300,400,500],origin='lower', colors=['white', 'yellow', 'red'])
plt.colorbar()
plt.show()

enter image description here

python contour colorbar
1个回答
0
投票

缺少的是,您还需要将返回的对象从imshow传递到颜色栏。我准备了一个最小的工作示例,该示例演示了如何在颜色栏中获得图像值和定义的级别。

import numpy as np
import matplotlib.pyplot as plt

Bho = np.random.random(size=10000).reshape(100,100)

fig, ax = plt.subplots()
im = ax.imshow(Bho, origin='l')
_cs2 = ax.contour(Bho, levels=[0.2,0.4] ,origin='lower', colors=['white','red'])

cbar = fig.colorbar(im, ax=ax)
cbar.add_lines(_cs2)

plt.show()

结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.