微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 gnuplot 命令在 matplotlib 中制作颜色图?

如何解决如何从 gnuplot 命令在 matplotlib 中制作颜色图?

我想在 Python 中使用 gnuplot 颜色映射。所附图片是使用surface plot命令在gnuplot中生成的。但我对 gnuplot 及其颜色图了解不多。

enter image description here

如何在 matplotlib 中制作类似于此 gnuplot 颜色图的颜色图? 显示图片的 Gnuplot 命令如下。但无法提供代码中使用的文件

set palette rgb 32,3,36 negative

set style line 1 lc rgb "blue" pt 6 ps 0.6
set style line 2 lc rgb "blue" pt 6 ps 0.5
set style line 3 lt 2 lc rgb "red"
set style line 4 lc rgb "black" lw 0.5
set style line 5 lc rgb "blue" pt 7 ps 0.2

任何帮助将不胜感激。

解决方法

颜色图在 Gnuplot 中定义为 set palette rgb 32,3,36 negative,所以它是一个颜色图,其中(见 show palette rgbformulae 的输出)

  • 红色通道带有从 (0,0) 到 (0.25,1) 到 (0.42,1) 到 (0.92,0) 到 (1,1) 的线性段,
  • 绿色通道从 (0,1) 并最终
  • 蓝色通道从 (0,0) 到 (0.5,1)。

enter image description here

Matplotlib 提供了 LinearSegmentedColormap 来处理这种类型的颜色图定义,下面您将找到如何定义与 Gnuplot 中定义的颜色图匹配的颜色图

In [62]: %reset -fs
    ...: import numpy as np
    ...: import matplotlib.pyplot as plt
    ...: from matplotlib.colors import LinearSegmentedColormap
    ...: 
    ...: cdata = {'red':[[0,0],[0.25,1,1],[0.42,[0.92,[1,1]],...:          'green':[[0,...:          'blue':[[0,[0.5,1]]}
    ...: gnuplot_cm = LinearSegmentedColormap('test',cdata)
    ...: 
    ...: np.random.seed(1)
    ...: data = np.random.randn(30,30)
    ...: fig,ax = plt.subplots(figsize=(4,3),constrained_layout=True)
    ...: cmesh = ax.pcolormesh(data,cmap=gnuplot_cm,vmin=-2,vmax=2)
    ...: fig.colorbar(cmesh,ax=ax)
    ...: plt.show()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。