【问题标题】:Gnuplot plotting coloursGnuplot 绘制颜色
【发布时间】:2021-10-14 07:20:13
【问题描述】:

我正在尝试制作 RGB 值与颜色的图表,例如:

我的方法基于http://gnuplot.sourceforge.net/demo/pm3dcolors.html

为此,能够正确显示任意数量的颜色并与图对齐非常重要。

在上图中,您可以看到颜色之间奇怪的重叠,并且有不同的宽度,并且与图表不完全一致。

这篇文章的其余部分展示了让它看起来怪异的最低要求。

我发现在 33 种颜色之后它开始搞砸了。

#!/usr/bin/env gnuplot

set format cb "%3.1f"

set style function pm3d
set view map scale 1

unset xtics
unset ytics
unset ztics
unset colorbox
unset key


set title "33 colours"
set palette model RGB
set palette file "-"
1 0 0
0 1 0
0 0 1
... repeat many times ...
1 0 0
0 1 0
0 0 1
e
set palette maxcolors 33
g(x)=x
splot g(x)

pause mouse any "press the any key to exit gnuplot
  • 这是 33 及以下颜色的情况:
  • 这就是 34 所发生的情况(中间的绿色不那么宽):
  • 如果我们增加到 66,您会看到更多错误:

总的来说,这显然是对 gnuplot 的滥用。

还有其他更好的方法吗? 如果没有,我应该如何调试?

我正在从 debian Bullseye 上的 debian repos 运行 gnuplot 5.4 patchlevel 1。

感谢您的任何建议。


编辑:

我陷入了 XY 问题的陷阱。 我希望问题中的第一个图表没有奇怪的东西,比如当它们都应该是相同的宽度时,你如何能看到不同的颜色宽度。 我询问了我发现的用设置调色板显示我所看到的奇怪事物的最小方式。

我当前的输入数据如下所示:

Hue, Red, Green, Blue

0.0100, 255,  15,   0
0.0300, 255,  46,   0
0.0400, 255,  61,   0
0.0600, 255,  92,   0
0.0700, 255, 107,   0
...

我生成该情节的脚本是http://ix.io/3BE2

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    尝试解决修改后问题的替代答案

    修改后的问题:

    感谢您的回复,但“显示任意数量的颜色”是指我有一个颜色列表并且我想显示它们,我没有办法将它们表示为一个函数。

    # snippet of code from your attachment
    set palette file 'plotData/rgb.csv' using (($2/255)):(($3/255)):(($4)/255)
    set palette maxcolors system("wc -l plotData/rgb.csv | sed 's/ .*//'")
      
    g(x)=x
    splot g(x)
    

    我将首先指出这种方法的一些问题。我仍然不太了解输出的要求是什么,但我认为问题的一部分是它不会产生一组与调色板颜色数量相匹配的均匀间隔区域。这是可以理解的,因为

    • 只有通过计算外部文件中的行数才能知道颜色数 Ncol。
    • splot F(x) 生成的沿 x 填充的区间数由 set samples 确定,与 Ncol 没有内在联系。
    • 您可以尝试通过说set samples Ncol 来对齐两者,但这仍然允许沿x 轴的间隔边界与沿颜色轴cb 的间隔边界之间不匹配。可能您可以通过修复 set xrange [A:B]; set cbrange [A:B] 来解决此问题,但这需要提前知道极限值 A 和 B。此外,在大多数应用中,x 上所需的空间分辨率很可能远大于索引调色板支持的颜色数量。

    我建议您根本不希望您的案例使用调色板。您只需要 RGB 颜色值。这里有两个基于上面代码片段的变体

    # convert file of numerical RGB components (0-255) to 24-bit hex representation
    # $RGBtable will be a datablock containing Ncol string values
    set table $RGBtable
    plot 'rgbvalues.csv' using (sprintf("0x%2x%2x%2x", $2, $3, $4)) with table
    unset table
    Ncol = |$RGBtable|
    
    # create an array containing the same set of colors as integer values
    array RGBarray[Ncol]
    do for [i=1:Ncol] { RGBarray[i] = int($RGBtable[i]) }
    
    # Now we can use either the string form or the integer form to plot with.
    # I still don't understand exactly what it is you are plotting, but here
    # is an example using either the datablock or the array to access colors
    set xrange [0 : Ncol+1]
    set yrange [0 : 3]
    plot sample [i=1:Ncol] '+' using 1:(1):(RGBarray[i]) with points pt 5 lc rgb variable, \
         [i=1:Ncol] '+' using 1:(2):(int($RGBtable[i])) with points pt 7 lc rgb variable
    

    最后,这里有一些类似于原始 splot 命令的东西,它使用沿水平方向的高分辨率采样,但从 [1:Ncol] 范围内的整数值中获取一组离散的颜色。 请注意,using 说明符的字段 3 中的标称 z 值与此图无关,因为颜色信息按照 lc rgb variable 的要求与字段 4 分开。

    set view map
    set sample 999
    set urange [1:Ncol]
    splot '++' using 1:2:(0):(RGBarray[int($1)]) with pm3d lc rgb variable
    

    【讨论】:

    【解决方案2】:

    如果我正确理解您尝试过的事物的描述,它们都是基于调色板定义选项set palette defined。这本质上会受到范围插值伪影的影响,尽管随着调色板定义中使用的增量数量的增加,伪影会变得不那么明显。

    由于您的主要目标是“能够正确显示任意数量的颜色”,因此您最好使用不同的方法来定义调色板,一种基于 RGB 组件的一组连续函数的方法。而且您确实想使用set palette maxcolors,因为该命令的全部意义在于将调色板限制为一组离散的颜色,而不是一个连续的颜色。 p>

    一个预定义的选项是使用set palette cubehelix,它使用一组固定的连续函数,但允许您调整各种控制参数。例如set palette cubehelix start 0.15 cycles 1.0 saturation 1.0 生成下面的调色板。然而,这一系列调色板的一个定义属性是强度从 0 到 1,这可能是您不想要的。

    更通用的解决方案是为调色板定义提供您自己的连续函数。例如,set palette model HSV functions gray, 1, 0.9 生成下面的调色板。这与您显示的基本相同,只是它是根据连续函数定义的。

    请注意,test palette 的输出在底部的颜色栏中仅显示 128 个样本。但这不是调色板的固有限制,只是为了方便生成测试输出而选择它。我将尝试显示区别(不确定它是否会在此网站上显示)

    set palette model HSV functions gray, 1, 0.9
    set colorbox horiz user origin 0.05, 0.01 size 0.9, 0.05
    set lmargin at screen 0.05; set rmargin at screen 0.95
    unset ytics
    set title "x sampling 1000 intervals"
    set cblabel "colorbox sampling 128 intervals" norotate offset 0,1.5
    set view map
    set sample 1000
    splot x with pm3d
    

    您可能还需要担心您正在制作的文档所使用的颜色系统的限制。并非所有输出格式都可以使用完整的 24 位 RGB 颜色规范。 pdf/svg/cairo 应该没问题。另一方面,旧式颜色索引 gif 或 png 总共限制为 256 种不同的颜色。

    【讨论】:

    • 感谢您的回复,但“显示任意数量的颜色”是指我有一个颜色列表并且我想显示它们,我没有办法将它们表示为功能。我将编辑问题以添加一些我的实际样本数据,尽管我似乎再次成为 XY 问题的受害者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多