色彩图( contour和许多其他绘图功能所需)和简单的颜色列表之间有什么区别? 如何将颜色列表(每种颜色应该代表相等大小的步骤)转换为颜色映射?
What's the difference between a colormap (as required by contour and many other plotting functions) and a simple list of colors? How do I convert a list of colors (where each color is supposed to be represent equal-sized step) into a colormap?
最满意答案
色彩图是一系列代表数量概念的颜色。 典型的色图(如喷射)为低值标记提供蓝色,为高值标记提供红色。 因此,您可以使用色彩图在图片中创建另一个维度。 对于matplotlib,如果你给出一种颜色,那么所有标记都会有这种颜色(所以它基本上是一种美学特征)。 如果给出色彩映射以及第三个变量,则该变量将被绘制为颜色而不是轴空间。
在轮廓的情况下,每种颜色可以表示更高或更低的高度(例如)。
您可以使用以下配方设置自己的离散色彩映射:
import matplotlib.pyplot as plt from matplotlib import colors import numpy as np def plot_matrix(rm, title='Robot World', cmap=plt.cm.Blues): plt.imshow(rm, interpolation='nearest', cmap=cmap) plt.title(title) plt.tight_layout() plt.show() cmap = colors.ListedColormap(['k','b','y','g','r']) bounds=[0,1,2,3,4] norm = colors.BoundaryNorm(bounds, cmap.N) rm = np.random.randint(0,4,(5,5)) plot_matrix(rm,cmap=cmap)我从另一个问题中给出了一个类似的答案,你可以在这里看到。
A colormap is a series of colors that represent a notion of quantity. A typical colormap such as jet gives a blue color to low valued markers and red color for high valued markers. So you can use a colormap make another dimension into your picture. For matplotlib if you give a color all marker will have that color (so it's basically an aesthetic feature). If you give a colormap, together with a third variable, that variable will be plotted as color instead for axis space.
In the case of contour each color might represent a higher or lower altitude (for example).
You can set your own discrete colormap using the following recipe:
import matplotlib.pyplot as plt from matplotlib import colors import numpy as np def plot_matrix(rm, title='Robot World', cmap=plt.cm.Blues): plt.imshow(rm, interpolation='nearest', cmap=cmap) plt.title(title) plt.tight_layout() plt.show() cmap = colors.ListedColormap(['k','b','y','g','r']) bounds=[0,1,2,3,4] norm = colors.BoundaryNorm(bounds, cmap.N) rm = np.random.randint(0,4,(5,5)) plot_matrix(rm,cmap=cmap)I brought this recipe from a similar answer I gave in another question you can see here.
更多推荐
发布评论