python-2.7 - python 2.7的暮光cmap
问题描述
我想在我的 2.7 python 版本中使用 twilight 或 twilight_shifted 颜色图,但它似乎只是 python 3?有什么方法可以手动添加吗?
解决方案
twilight
仅在 python 3 的 matplotlib v3.0 中添加。但是我们可以在源代码中找到它被添加的地方,对其进行重新设计。
在下面的代码中,您只需要twilight
通过以下链接从 github 上的 matplotlib 源中获取用于的数据。
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()
用 , 和twilight
colormapstwilight_r
创建一个dict 。twilight_shifted
twilight_shifted_r
该脚本还生成此测试图像:
推荐阅读
- spring - 弹簧型材多合一
- excel - 删除用户窗体中的框架边框
- apache-spark - 如何使用 Spark Fair 调度程序将处理后的数据重用于并行作业
- regex - 如何捕获每行的第二场比赛?
- gremlin - 使用 Gremlin 针对 Azure Cosmos db 图将边限制为同一组 verices 的最佳方法
- sql - SQL - DTS 向导导出数据失败
- c++ - 如何升级提升进度?
- dynamic - 根据之前的按钮点击跟踪按钮的点击
- arrays - 删除两个数组中满足条件的元素 MATLAB
- python - Filtering rows in DataFrame with a numeric value OR NaN in column