python - 对称色图 matplotlib

标签 python matplotlib

我有一系列在 -180 到 180 之间变化的值,我有一个关于零对称的颜色图,即 -180 的颜色与 180 值的颜色相同,依此类推。这是我第一次使用 matplotlib,我只是没有找到使它围绕零对称的方法,负片的颜色总是与正片不同。 非常感谢。

最佳答案

感谢来自:https://stackoverflow.com/a/31052741 的贡献

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


def symmetrical_colormap(cmap_settings, new_name = None ):
    ''' This function take a colormap and create a new one, as the concatenation of itself by a symmetrical fold.
    '''
    # get the colormap
    cmap = plt.cm.get_cmap(*cmap_settings)
    if not new_name:
        new_name = "sym_"+cmap_settings[0]  # ex: 'sym_Blues'
    
    # this defined the roughness of the colormap, 128 fine
    n= 128 
    
    # get the list of color from colormap
    colors_r = cmap(np.linspace(0, 1, n))    # take the standard colormap # 'right-part'
    colors_l = colors_r[::-1]                # take the first list of color and flip the order # "left-part"

    # combine them and build a new colormap
    colors = np.vstack((colors_l, colors_r))
    mymap = mcolors.LinearSegmentedColormap.from_list(new_name, colors)

    return mymap

测试它给你:

# --- Quick test -------------------------
cmap_settings = ('Blues', None)  # provide int instead of None to "discretize/bin" the colormap
mymap = symmetrical_colormap(cmap_settings= cmap_settings, new_name =None )

data = np.random.rand(10,10) * 2 - 1
plt.pcolor(data, cmap=mymap)
plt.colorbar()
plt.show()

Result_figure

关于python - 对称色图 matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28439251/

相关文章:

numpy - 从 matplotlib 图形中删除边距

python - 绘制每列随时间的观察结果(数据可用性)

python - 如何从节点列表中获取对应的边

python - 如何在Python中使用NLP来分析聊天对话中的问题

python - 如何从 yfinance 数据中删除时区?

python - 为什么我的神经网络显示奇怪的结果?

python - 在seaborn中设置标题大小

python - 已知 x、y 的条形图

python - 如何在 App Engine 上使用 lxml 版本 3.7.3?

python - 在 MacOS 上安装 Spyder/Python - 非常令人沮丧