【问题标题】:How can be made a colormap in matplotlib from gnuplot commands?如何从 gnuplot 命令在 matplotlib 中制作颜色图?
【发布时间】:2021-07-24 10:39:33
【问题描述】:

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

如何在 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

任何帮助将不胜感激。

【问题讨论】:

  • 我给出的命令是自定义颜色图。我正在使用其他人编写的代码。共享命令没有害处。
  • 很遗憾,我无法将代码添加到注释中,因为它有点长。

标签: matplotlib gnuplot colormap


【解决方案1】:

颜色图在 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, 0) 到 (1,1) 并最终
  • 蓝色通道从 (0, 0) 到 (0.5, 0) 到 (1, 1)。

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], [0.25, 1, 1], [0.42, 1, 1], [0.92, 0, 0], [1, 1, 1]],
    ...:          'green':[[0, 0, 0], [1, 1, 1]],
    ...:          'blue':[[0,0,0], [0.5,0,0], [1,1,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()

【讨论】:

  • 你的回答太棒了,无需多言,非常感谢@gbofii。你也给我们做了一个讲座。再次感谢一百万。
  • 我查看了命令 show palette rgbformulae 的输出,但它有点复杂,我找不到您定义的 RGB 数字。如何在红色通道和其他通道中获得这些 RGB 限制 0.25、0.42 和 0.92?
  • 红色通道的输出是这样的 ` 0: 0 3: x 6: x^4 9: sin(90x) 12: (2x-1)^2 15: sin(360x) 18 : |cos(360x)| 21:3x 24:|3x-1| 27: (3x-2)/2 30: x/0.32-0.78125 33: |2*x - 0.5| 36: 2*x - 1`
  • @Bluerose 当你说set palette rgb 32,3,36 oit 的意思是“红色通道使用公式 32,绿色通道使用公式 3,蓝色通道使用公式 36。最简单的一种没有。3 x 这意味着绿色遵循从 0 到 1 的斜坡,因为 x 也打算从 0 到 1,为了理解其他公式,绘制不同部分并保持结果以 0 为界和 1.
  • 在我的例子中,这些参数是32: 4x;1;-2x+1.84;x/0.08-11.5 for R, 3: x for G and 36: 2*x - 1 for B不是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
相关资源
最近更新 更多