简体   繁体   English

使用 matplotlib 中的 contourf 将某个值映射到颜色

[英]Mapping certain value to a color using contourf in matplotlib

I've been trying to solve this problem for days, but none of the solutions I found online seem to work very well for me, despite how simple the problem seems.几天来我一直在尝试解决这个问题,但是我在网上找到的解决方案似乎对我来说都不是很好,尽管问题看起来很简单。

import numpy as np
from matplotlib import pyplot as plt

grid_x = np.linspace(-5, 5, 500)
grid_y = np.linspace(-5, 5, 500)
xx, yy = np.meshgrid(grid_x, grid_y)

fig, ax = plt.subplots(2)
rand1 = np.ones((500,500))
rand1[:, 250:] = 2
ax[0].contourf(xx, yy, rand1, alpha=.3, cmap = 'jet')

rand2 = np.zeros((500,500))
rand2[:, 200:250] = 1
rand2[:,250:] = 2
ax[1].contourf(xx, yy, rand2, alpha=.3, cmap = 'jet')

This is the resulting plot by running the above:这是通过运行上述代码生成的 plot :

在此处输入图像描述

On the first axis, the 1s are mapped to blue, and 2s are mapped to red.在第一个轴上,1 映射到蓝色,2 映射到红色。 On the second axis, the 0s are mapped to blue, 1s are mapped to green, and 2s are mapped to red.在第二个轴上,0 映射到蓝色,1 映射到绿色,2 映射到红色。

I would like it to be consistent, so that for example, 1s are always mapped to blue, 2s always mapped to red, and 0s always mapped to green.我希望它保持一致,例如,1 总是映射到蓝色,2 总是映射到红色,0 总是映射到绿色。

As far as I am aware, this issue would be fixed by ensuring that both plots have values in (0,1,2) all show up at some point, however I cannot ensure that for my project, so that is not a valid solution.据我所知,可以通过确保两个图的值都在 (0,1,2) 中的某个点都显示来解决这个问题,但是我不能确保对于我的项目,所以这不是一个有效的解决方案.

Another thought I had was to map each region separately.我的另一个想法是分别对每个区域进行 map。 However, I am not sure how to do this, and though I imagine it would be relatively easy for the simple example above, I need it to generalise to any arbitrarily shaped subregions.但是,我不确定如何做到这一点,尽管我认为上面的简单示例相对容易,但我需要将其推广到任何任意形状的子区域。

Does anyone have suggestions on how to fix this issue?有没有人有关于如何解决这个问题的建议? Thank you very much!非常感谢!

To better see what's happening, it helps to add a colorbar.为了更好地了解正在发生的事情,添加颜色条会有所帮助。 It shows the levels used and the corresponding colors.它显示了使用的级别和相应的 colors。

To have the same colors for corresponding levels, the levels should be the same between the plots:要使相应级别的 colors 相同,地块之间的级别应该相同:

import numpy as np
from matplotlib import pyplot as plt

grid_x = np.linspace(-5, 5, 500)
grid_y = np.linspace(-5, 5, 500)
xx, yy = np.meshgrid(grid_x, grid_y)

rand1 = np.ones((500, 500))
rand1[:, 250:] = 2

rand2 = np.zeros((500, 500))
rand2[:, 200:250] = 1
rand2[:, 250:] = 2

# common_levels = np.linspace(0, 2, 11)
common_levels = np.linspace(min(rand1.min(), rand2.min()), max(rand1.max(), rand2.max()), 11)

fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(12, 6))
contour00 = axs[0, 0].contourf(xx, yy, rand1, alpha=.3, cmap='jet')
axs[0, 0].set_title('values 1 and 2, default levels')
plt.colorbar(contour00, ax=axs[0, 0])

contour01 = axs[0, 1].contourf(xx, yy, rand1, levels=common_levels, alpha=.3, cmap='jet')
plt.colorbar(contour01, ax=axs[0, 1])
axs[0, 1].set_title('values 1 and 2, common levels')

contour10 = axs[1, 0].contourf(xx, yy, rand2, alpha=.3, cmap='jet')
axs[1, 0].set_title('values 0, 1 and 2, default levels')
plt.colorbar(contour10, ax=axs[1, 0])

contour11 = axs[1, 1].contourf(xx, yy, rand2, levels=common_levels, alpha=.3, cmap='jet')
axs[1, 1].set_title('values 0, 1 and 2, common levels')
plt.colorbar(contour11, ax=axs[1, 1])

plt.tight_layout()
plt.show()

比较有和没有共同水平的轮廓图

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

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