简体   繁体   English

使用带有 matplotlib 的 brewer2mpl 发散颜色图,gamma 给出的结果很差,其值不同于 1

[英]Using brewer2mpl diverging colormap with matplotlib, gamma gives poor results with values different than 1

Playing with colormaps in this question I was introduced to brewer2mpl in comments.这个问题中使用颜色图 我在评论中被介绍给了brewer2mpl For the diverging map (with zero in the middle) I noticed that gamma is implemented to work in a naive way and so gives unexpected results when it is not equal to 1. The neutral tone meant to represent zero slides around so that values near zero are blue with gamma = 0.5 and orang when gamma = 1.5对于发散图(中间为零),我注意到伽马以一种幼稚的方式工作,因此当它不等于 1 时会给出意想不到的结果。表示零的中性色调会四处滑动,因此值接近零是伽马 = 0.5 的蓝色,伽马 = 1.5 时的橙色

Is there anyway to make a gamma-like parameter that operates symmetrically around zero in matplotlib or brewer2mpl or do I just have to do it manually somehow by raising np.abs(u) to some power then re-normalizing and re-introducing the sign?有没有办法在matplotlibbrewer2mpl制作一个在零附近对称运行的类似伽马的参数,或者我是否只需要通过将np.abs(u)提高到某个幂然后重新规范化并重新引入符号来手动完成?

expected behavior would be to apply gamma symmetrically about zero; 预期行为是在零附近对称地应用伽马; at least that's what I had expected.至少这是我所期望的。

brewer2mpl 和 gamma 不等于 1 的发散颜色图给出了不好的结果

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

Re = 6378137. # meters
J2 = 1.7555E+25 # m^5/s^2
hw = 400
x = np.linspace(-2.5*Re, 2.5*Re, 2*hw+1)
x, z = np.meshgrid(x, x)
r = np.sqrt(x**2 + z**2)
u = J2 * r**-5 * 0.5 * (3*z**2 - r**2)
u[r<Re] = np.nan

if True:
    umax = np.nanmax(np.abs(u))
    bmap = brewer2mpl.get_map('RdBu', 'Diverging', 9)
    gammas = 0.5, 1, 1.5
    plt.figure()
    for i, gamma in enumerate(gammas):
        cmap = bmap.get_mpl_colormap(N=100, gamma=gamma)
        plt.subplot(1, 3, i+1)
        plt.imshow(u, cmap=cmap, vmin=-umax, vmax=umax)
        plt.colorbar()
        plt.title('gamma = ' + str(gamma))
        plt.plot([hw, hw], [0.3*hw, 1.7*hw], '-k')
        plt.xlim(0, 2*hw+1)
        plt.ylim(0, 2*hw+1)
    plt.suptitle("Earth's geopotential's J2 component", fontsize=16)
    plt.show()

You can create two colormaps, one from red to white, one from white to blue.您可以创建两个颜色图,一个从红色到白色,一个从白色到蓝色。 Then apply gamma to each of those.然后将 gamma 应用于每个。 Finally get the colors from those colormaps and create a new one with the combined colors.最后从这些颜色图中获取颜色并使用组合颜色创建一个新颜色。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from copy import copy

cmap = plt.get_cmap("RdBu", lut=256)
# Create two new colormaps, each with one half of the original
cmap_lower = LinearSegmentedColormap.from_list("", cmap(np.arange(0,128)))
cmap_upper = LinearSegmentedColormap.from_list("", cmap(np.arange(128,256)))
gammas = [1, 0.5, 1.5]


fig, axs = plt.subplots(ncols=3, figsize=(8,5))

for ax, gamma in zip(axs, gammas):
    # copy each colormap and set the respective gamma
    cm1 = copy(cmap_lower)
    cm1.set_gamma(gamma)
    cm2 = copy(cmap_upper)
    cm2.set_gamma(gamma)
    # get the colors from the each
    colors = np.concatenate((cm1(np.arange(0,256)), cm2(np.arange(0,256))), axis=0) 
    this_cmap = LinearSegmentedColormap.from_list("", colors)

    im = ax.imshow(np.arange(300).reshape(30,10), cmap=this_cmap)
    fig.colorbar(im, ax=ax)
    ax.set_title(f"gamma={gamma}")

plt.show()

在此处输入图片说明

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

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