简体   繁体   English

Matplotlib + Cartopy:如何在自定义颜色表中使用contourf

[英]Matplotlib + Cartopy: How to use contourf with custom colormap

I have a set of points I'm using to plot a map using contourf . 我有一组要用于使用contourf绘制地图的contourf

I need to have a specific color pallet for this map, regarding specific data points, and a color to be set on data over the limit. 我需要针对该地图具有特定的颜色托盘,涉及特定的数据点,并且需要为超出限制的数据设置颜色。

So, I have levels=[0, 2, 20, 100] and I'm looking for to have a cmap like the following one: 因此,我的levels=[0, 2, 20, 100] cmap levels=[0, 2, 20, 100]并且我正在寻找一个像下面这样的cmap

cmap=LinearSegmentedColormap.from_list([
  (0,   color1),
  (2,   color2),
  (20,  color3),
  (100, color4),
])
cmap.set_over(color5)

Problem is that the points must be normalized, like so: 问题是必须对点进行归一化,如下所示:

cmap=LinearSegmentedColormap.from_list([
  (0 / max_value,   color1),
  (2 / max_value,   color2),
  (20 / max_value,  color3),
  (100 / max_value, color4),
])
cmap.set_over(color5)

My problem is, my data is variable, so I don't know what my max_value will be. 我的问题是,我的数据是可变的,所以我不知道我的max_value是什么。 I just want to "ignore" that the data is over 100, and paint it with color5 . 我只想“忽略”数据超过100,并用color5对其进行绘制。

I know I can manipulate my data beforehand and make everything over 100 to actually BE 100, or to find the max_value in realtime, but those methods seem hackish to me. 我知道我可以事先处理我的数据,并使所有超过100的数据变为实际100,或者实时找到max_value ,但是这些方法对我来说似乎很糟糕。

Is there a way to accomplish that using matplotlib functions? 有没有一种方法可以使用matplotlib函数来实现?

I ended up normalising my data, between [0,1] , like that: 我最终标准化了[0,1]之间的数据,如下所示:

def normalizer(lower_bound, upper_bound):
    _lower = float(lower_bound)
    _upper = float(upper_bound)

    def do_norm(x):
        return (float(x) - _lower) / (_upper - _lower)

    return do_norm

normalize = normalizer(0, 20)
normalize(10)  # 0.5

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

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