问题描述
我尝试以这种方式使用xy坐标和颜色的地图创建Color Mesh图:
I try to create a Color Mesh plot using a map of xy-coordinates and colors in this way:
from matplotlib.colors import ListedColormap
import numpy as np
%pylab inline
colors = ListedColormap(['red', 'blue', 'yellow'])
xx,yy = np.meshgrid(np.arange(1, 6, 1), np.arange(1, 6, 1))
zz = np.array([[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[2,2,0,0,0],
[2,2,0,0,0]])
pyplot.pcolormesh(xx, yy, zz, cmap = colors)
当存在三种颜色的列表并且我尝试将xy点映射到所有这些颜色时(如上面的代码中),效果很好:
It works ok when there is a list of three colors and I try to map xy-points into all of that colors (like in the code above):
但是当有三种颜色列表时,我尝试仅将点映射到其中的两种,则映射会出错:
But when there is a list of three colors, and I try to map points only into two of them, mapping goes wrong:
zz = np.array([[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,0,0,0]])
pyplot.pcolormesh(xx, yy, zz, cmap = colors)
它应该映射为颜色0(红色)和1(蓝色),但是我得到了0(红色)和 2(黄色)颜色的图.错误在哪里?
It should map into colors 0 (red) and 1 (blue), but I get plot with 0 (red) and 2 (yellow) colors. Where is the mistake?
推荐答案
色图在0到1之间进行归一化.在绘图中使用它们时,归一化将转换为数据的最小值和最大值.如果0和1是最小值和最大值,则0将是地图的第一个颜色(红色),而1将是地图的最后一个颜色(黄色).
Colormaps are normalized between 0 and 1. When they are used in a plot, the normalization is transfered to the minimum and maximum value of the data. If 0 and 1 are minimum and maximum, 0 will be the first color of the map (red) and 1 will the last (yellow).
您需要的是一种标准化,它考虑了所需的颜色图行为.最简单的选择是使用 vmin
和 vmax
What you need is a normalization which takes the desired colormap behaviour into account. The easiest option is to use vmin
and vmax
zz = np.array([[1,1,1,1,1],
[1,1,1,1,1],
[1,1,1,1,1],
[0,0,0,0,0],
[0,0,0,0,0]])
plt.pcolormesh(xx, yy, zz, cmap = colors, vmin=0,vmax=colors.N)
这篇关于PyPlot ColorMesh创建具有错误颜色映射的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!