python 2.7 的暮色 cmap

twilight cmap for python 2.7

我想在我的 2.7 python 构建中使用暮色或 twilight_shifted 颜色图,但它似乎只有 python 3?有什么办法可以手动添加吗?

您可以创建新的自定义颜色图,如此 tutorial 所示。

"twilight" 和 "twilight_shifted" 颜色图的数据是 here

twilight 是在 matplotlib v3.0 中添加的,它只是 python 3。但是我们可以在源代码中找到它添加的地方重新设计它。

在下面的代码中,您只需按照 link.

从 github 上的 matplotlib 源中获取用于 twilight 的数据
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors

_twilight_data = [ # data too long for stack overflow. get it from here:
                   # https://github.com/matplotlib/matplotlib/blob/f2116d82dfd6b82fe178230766d95ea9ac2b0c8c/lib/matplotlib/_cm_listed.py#L1288
                 ]

_twilight_shifted_data = (_twilight_data[len(_twilight_data)//2:] +
                          _twilight_data[:len(_twilight_data)//2])
_twilight_shifted_data.reverse()

cmaps = {}
for (name, data) in (('twilight', _twilight_data),
                     ('twilight_shifted', _twilight_shifted_data)):

    cmaps[name] = colors.ListedColormap(data, name=name)
    # generate reversed colormap
    name = name + '_r'
    cmaps[name] = colors.ListedColormap(list(reversed(data)), name=name)


fig, ax = plt.subplots()
p = ax.pcolormesh(np.arange(25).reshape(5, 5), cmap=cmaps['twilight'])
fig.colorbar(p, ax=ax)

plt.show()

twilighttwilight_rtwilight_shiftedtwilight_shifted_r 颜色映射创建字典。

该脚本还会生成此测试图像:

您可以从当前版本中获取 _cm_listed.py 文件并将其复制到您的 matplotlib 2.2.3 文件夹中。由于该文件与版本无关,这应该会直接为您提供额外的颜色图。