简体   繁体   English

Matplotlib自定义发散渐变忽略颜色

[英]Matplotlib custom diverging gradient ignoring colors

I've been working on some data plotting functioning for python and one such tool I need for my research is a set of difference charts to compare the changes between two sets of data. 我一直在研究一些适用于python的数据绘图功能,而我研究所需的一个此类工具是一组差异图,用于比较两组数据之间的变化。

I'm now at the point where I want to plot my data and I was looking into creating custom color maps to handle the diverging data, however all of my plots such far are either ignoring specific steps in my gradient, or the colors are being repeated for my low values. 我现在要绘制数据,正在寻找创建自定义颜色图以处理差异数据的方法,但是到目前为止,我所有的图都忽略了渐变中的特定步骤,或者正在绘制颜色重复我的低价值。

Here's an example plot that was generated: 这是生成的示例图: 在此处输入图片说明

And the code for both my custom color map, as well as the plotting: 还有我的自定义颜色图以及绘图的代码:

diffmap_17 = ["#FF0000", "#F81318", "#F12731", "#EB3B4A", "#EB5C66", "#EB7D82", "#EB9E9E", "#F1BEBE", "#F8DEDE", "#FFFFFF", "#DDDCFD", "#BCB9FB", "#9B96FA", "#6A6CFA", "#3A43FA", "#1D21FC", "#0000FF"]
diffmap_17_colormap = matplotlib.colors.ListedColormap(diffmap_17)

contour_levels = [-20, -10, -5, -2, -1, -0.75, -0.5, -0.25, -0.1, 0.0, 0.1, 0.25, 0.5, 0.75, 1, 2, 5, 10, 20]
cs = m.contourf(x,y,data,contour_levels,cmap=diffmap_17_colormap) #plot total

My goal is to have the color map have the zero point be white, and then diverge outward (Reds negative, blues positive). 我的目标是使颜色图的零点为白色,然后向外发散(红色为负,蓝色为正)。 For the time being, I'm employing a standard colormap, however using a custom one would be preferred moving forward. 目前,我正在使用标准的颜色表,但是最好使用自定义颜色表。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

The problem is that the colors are chosen from the colormap by dividing the range between the minimum and maximum values into equal parts. 问题在于,通过将最小值和最大值之间的范围分成相等的部分来从颜色图中选择颜色。 Since most levels lie very close to each other, they fall into the same range and thus have the same color. 由于大多数级别彼此非常接近,因此它们属于同一范围,因此具有相同的颜色。

The easiest solution is not to use a colormap, but a plot where each of the levels gets its color from the colorlist. 最简单的解决方案是不使用颜色图,而是使用每个色阶从颜色列表中获取其颜色的图。 In this case you may provide the list of colors directly to the contourf plot. 在这种情况下,您可以直接将颜色列表提供给contourf图。

plt.contourf(x,y,data,contour_levels,colors=diffmap_17)

Note, that because you have 19 levels your list would then need 18 colors (I therefore added one). 请注意,由于您有19个级别,因此列表将需要18种颜色(因此,我添加了一种)。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors

x, y= np.meshgrid(np.linspace(-3,3), np.linspace(-3,3))
r = np.sqrt(x**2+y**2)
data = np.tan((r*0.7-1.5))*1.3

diffmap_17 = ["#FF0000", "#F81318", "#F12731", "#EB3B4A", "#EB5C66", "#EB7D82", 
              "#EB9E9E", "#F1BEBE", "#F8DEDE", "#FFFFFF", "#DDDCFD", "#BCB9FB", 
              "#9B96FA", "#6A6CFA", "#3A43FA", "#1D21FC", "#0000FF", "#0000ce"]

contour_levels = [-20, -10, -5, -2, -1, -0.75, -0.5, -0.25, -0.1, 0.0, 
                  0.1, 0.25, 0.5, 0.75, 1, 2, 5, 10, 20]
cs = plt.contourf(x,y,data,contour_levels,colors=diffmap_17)

plt.colorbar(cs)

plt.show()

在此处输入图片说明

If you want to use a colormap instead, you would need to provide a normalization instance together with the colormap. 如果要改用色图,则需要与色图一起提供规范化实例。 A matplotlib.colors.BoundaryNorm would chose the colors according to the list of boundaries supplied to it, which would be the list of levels for the contour plot. matplotlib.colors.BoundaryNorm将根据提供给它的边界列表选择颜色,这将是等高线图的级别列表。

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors

x, y= np.meshgrid(np.linspace(-3,3), np.linspace(-3,3))
r = np.sqrt(x**2+y**2)
data = np.tan((r*0.7-1.5))*1.3

diffmap_17 = ["#FF0000", "#F81318", "#F12731", "#EB3B4A", "#EB5C66", "#EB7D82", 
              "#EB9E9E", "#F1BEBE", "#F8DEDE", "#FFFFFF", "#DDDCFD", "#BCB9FB", 
              "#9B96FA", "#6A6CFA", "#3A43FA", "#1D21FC", "#0000FF", "#0000ce"]
diffmap_17_colormap = matplotlib.colors.ListedColormap(diffmap_17)

contour_levels = [-20, -10, -5, -2, -1, -0.75, -0.5, -0.25, -0.1, 0.0, 
                  0.1, 0.25, 0.5, 0.75, 1, 2, 5, 10, 20]
norm = matplotlib.colors.BoundaryNorm(contour_levels, diffmap_17_colormap.N)
cs = plt.contourf(x,y,data,contour_levels,cmap=diffmap_17_colormap, norm=norm)

plt.colorbar(cs)

plt.show()

The output plot is the same as above. 输出图与上面相同。

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

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