【问题标题】:gnuplot: apply colornames from datafilegnuplot:从数据文件中应用颜色名称
【发布时间】:2019-07-06 14:34:05
【问题描述】:

如何从数据文件中获取颜色名称? 我没想到这会如此困难,但显然确实如此。 为什么版本 1 和版本 2 会给出错误的颜色?有没有一种更简单的方法来获得正确的颜色而不使用版本 3 中的数组?而...lc var 仅适用于索引值,但不适用于颜色名称。

### colornames from data
reset session

$Data <<EOD
1   40  orange
2   35  cyan
3   25  red
4   15  yellow
5   5   green
EOD

set yrange [0:]
set multiplot layout 3,1
MyColor = "black"
plot \
    $Data u 1:(MyColor=strcol(3),$2):(0.7) w boxes fs solid 1.0 lc rgb MyColor notitle,\
    $Data u 1:($2/2):(strcol(3)) with labels tc rgb "white" notitle

plot \
    for [i=1:|$Data|] $Data u (MyColor=strcol(3),$1):2:(0.7) every ::i-1::i-1 w boxes fs solid 1.0 lc rgb MyColor notitle,\
    $Data u 1:($2/2):(strcol(3)) with labels notitle

array MyColors[|$Data|]
plot \
    $Data u (NaN):(MyColors[$0+1]=strcol(3)) notitle,\
    for [i=1:|$Data|] $Data u 1:2:(0.7) every ::i-1::i-1 w boxes fs solid 1.0 lc rgb MyColors[i] notitle,\
    $Data u 1:($2/2):(strcol(3)) with labels notitle

print MyColors
unset multiplot
### end of code

【问题讨论】:

    标签: colors gnuplot


    【解决方案1】:

    如果有人想按照@Ethan 的建议使用查找表,这里有 111 种预定义的 gnuplot 颜色数组(取自 gnuplot 手册)。

    array ColorNames[111] = \
    ['white', 'black', 'dark-grey', 'red', 'web-green', 'web-blue', 'dark-magenta', 'dark-cyan', 'dark-orange', 'dark-yellow', \
    'royalblue', 'goldenrod', 'dark-spring-green', 'purple', 'steelblue', 'dark-red', 'dark-chartreuse', 'orchid', 'aquamarine', 'brown', \
    'yellow', 'turquoise', 'grey0', 'grey10', 'grey20', 'grey30', 'grey40', 'grey50', 'grey60', 'grey70', \
    'grey', 'grey80', 'grey90', 'grey100', 'light-red', 'light-green', 'light-blue', 'light-magenta', 'light-cyan', 'light-goldenrod', \
    'light-pink', 'light-turquoise', 'gold', 'green', 'dark-green', 'spring-green', 'forest-green', 'sea-green', 'blue', 'dark-blue', \
    'midnight-blue', 'navy', 'medium-blue', 'skyblue', 'cyan', 'magenta', 'dark-turquoise', 'dark-pink', 'coral', 'light-coral', \
    'orange-red', 'salmon', 'dark-salmon', 'khaki', 'dark-khaki', 'dark-goldenrod', 'beige', 'olive', 'orange', 'violet', \
    'dark-violet', 'plum', 'dark-plum', 'dark-olivegreen', 'orangered4', 'brown4', 'sienna4', 'orchid4', 'mediumpurple3', 'slateblue1', \
    'yellow4', 'sienna1', 'tan1', 'sandybrown', 'light-salmon', 'pink', 'khaki1', 'lemonchiffon', 'bisque', 'honeydew', \
    'slategrey', 'seagreen', 'antiquewhite', 'chartreuse', 'greenyellow', 'gray', 'light-gray', 'light-grey', 'dark-gray', 'slategray', \
    'gray0', 'gray10', 'gray20', 'gray30', 'gray40', 'gray50', 'gray60', 'gray70', 'gray80', 'gray90', \
    'gray100']
     
    array ColorValues[111] = \
    [0xffffff, 0x000000, 0xa0a0a0, 0xff0000, 0x00c000, 0x0080ff, 0xc000ff, 0x00eeee, 0xc04000, 0xc8c800, \
    0x4169e1, 0xffc020, 0x008040, 0xc080ff, 0x306080, 0x8b0000, 0x408000, 0xff80ff, 0x7fffd4, 0xa52a2a, \
    0xffff00, 0x40e0d0, 0x000000, 0x1a1a1a, 0x333333, 0x4d4d4d, 0x666666, 0x7f7f7f, 0x999999, 0xb3b3b3, \
    0xc0c0c0, 0xcccccc, 0xe5e5e5, 0xffffff, 0xf03232, 0x90ee90, 0xadd8e6, 0xf055f0, 0xe0ffff, 0xeedd82, \
    0xffb6c1, 0xafeeee, 0xffd700, 0x00ff00, 0x006400, 0x00ff7f, 0x228b22, 0x2e8b57, 0x0000ff, 0x00008b, \
    0x191970, 0x000080, 0x0000cd, 0x87ceeb, 0x00ffff, 0xff00ff, 0x00ced1, 0xff1493, 0xff7f50, 0xf08080, \
    0xff4500, 0xfa8072, 0xe9967a, 0xf0e68c, 0xbdb76b, 0xb8860b, 0xf5f5dc, 0xa08020, 0xffa500, 0xee82ee, \
    0x9400d3, 0xdda0dd, 0x905040, 0x556b2f, 0x801400, 0x801414, 0x804014, 0x804080, 0x8060c0, 0x8060ff, \
    0x808000, 0xff8040, 0xffa040, 0xffa060, 0xffa070, 0xffc0c0, 0xffff80, 0xffffc0, 0xcdb79e, 0xf0fff0, \
    0xa0b6cd, 0xc1ffc1, 0xcdc0b0, 0x7cff40, 0xa0ff20, 0xbebebe, 0xd3d3d3, 0xd3d3d3, 0xa0a0a0, 0xa0b6cd, \
    0x000000, 0x1a1a1a, 0x333333, 0x4d4d4d, 0x666666, 0x7f7f7f, 0x999999, 0xb3b3b3, 0xcccccc, 0xe5e5e5, \
    0xffffff]
    

    补充: 为了它的价值,只是为了完整性和说明...... 下面是一些代码,用于生成有关预定义 gnuplot 颜色的概述。

    编辑: 清理并修改代码,使其也适用于 gnuplot 4.6(当时没有数组和位移位)。

    代码:

    ### display all predefined gnuplot colors, gnuplot >=4.6.0
    reset
    set term wxt size 1000,700 enhanced   # or change to another terminal
    
    ColorNames = '\
    white black dark-grey red web-green web-blue dark-magenta dark-cyan dark-orange dark-yellow \
    royalblue goldenrod dark-spring-green purple steelblue dark-red dark-chartreuse orchid aquamarine brown \
    yellow turquoise grey0 grey10 grey20 grey30 grey40 grey50 grey60 grey70 \
    grey grey80 grey90 grey100 light-red light-green light-blue light-magenta light-cyan light-goldenrod \
    light-pink light-turquoise gold green dark-green spring-green forest-green sea-green blue dark-blue \
    midnight-blue navy medium-blue skyblue cyan magenta dark-turquoise dark-pink coral light-coral \
    orange-red salmon dark-salmon khaki dark-khaki dark-goldenrod beige olive orange violet \
    dark-violet plum dark-plum dark-olivegreen orangered4 brown4 sienna4 orchid4 mediumpurple3 slateblue1 \
    yellow4 sienna1 tan1 sandybrown light-salmon pink khaki1 lemonchiffon bisque honeydew \
    slategrey seagreen antiquewhite chartreuse greenyellow gray light-gray light-grey dark-gray slategray \
    gray0 gray10 gray20 gray30 gray40 gray50 gray60 gray70 gray80 gray90 \
    gray100'
    
    ColorValues = '\
    0xffffff 0x000000 0xa0a0a0 0xff0000 0x00c000 0x0080ff 0xc000ff 0x00eeee 0xc04000 0xc8c800 \
    0x4169e1 0xffc020 0x008040 0xc080ff 0x306080 0x8b0000 0x408000 0xff80ff 0x7fffd4 0xa52a2a \
    0xffff00 0x40e0d0 0x000000 0x1a1a1a 0x333333 0x4d4d4d 0x666666 0x7f7f7f 0x999999 0xb3b3b3 \
    0xc0c0c0 0xcccccc 0xe5e5e5 0xffffff 0xf03232 0x90ee90 0xadd8e6 0xf055f0 0xe0ffff 0xeedd82 \
    0xffb6c1 0xafeeee 0xffd700 0x00ff00 0x006400 0x00ff7f 0x228b22 0x2e8b57 0x0000ff 0x00008b \
    0x191970 0x000080 0x0000cd 0x87ceeb 0x00ffff 0xff00ff 0x00ced1 0xff1493 0xff7f50 0xf08080 \
    0xff4500 0xfa8072 0xe9967a 0xf0e68c 0xbdb76b 0xb8860b 0xf5f5dc 0xa08020 0xffa500 0xee82ee \
    0x9400d3 0xdda0dd 0x905040 0x556b2f 0x801400 0x801414 0x804014 0x804080 0x8060c0 0x8060ff \
    0x808000 0xff8040 0xffa040 0xffa060 0xffa070 0xffc0c0 0xffff80 0xffffc0 0xcdb79e 0xf0fff0 \
    0xa0b6cd 0xc1ffc1 0xcdc0b0 0x7cff40 0xa0ff20 0xbebebe 0xd3d3d3 0xd3d3d3 0xa0a0a0 0xa0b6cd \
    0x000000 0x1a1a1a 0x333333 0x4d4d4d 0x666666 0x7f7f7f 0x999999 0xb3b3b3 0xcccccc 0xe5e5e5 \
    0xffffff'
    
    # get R,G,B components
    R(Color) = (Color & 0xff0000)/0x10000
    G(Color) = (Color & 0x00ff00)/0x100
    B(Color) = (Color & 0xff)
       
    PosX(i) = int(i)%10
    PosY(i) = floor(i/10.)*10
    ColorName(i) = word(ColorNames,int(i))
    ColorValue(i) = int(word(ColorValues,int(i)))
    ColorValueLabel(i) = sprintf('#%06x',ColorValue(i))
    # "empirical" formula do decide whether using a white or a black label on the colored background
    ColorLabelColor(c) = (R(c) + G(c)*1.5 + B(c)*0.5)/3. > 127 ? 0x000000 : 0xffffff
    
    set title "Predefined colors in gnuplot" font ",18"
    set xrange[-0.5:9.5]
    set x2range[-0.5:9.5]
    set xtics 1
    set x2tics 1
    set yrange[119:-5]
    set y2range[119:-5]
    set ytics 10
    set y2tics 10
    
    set style fill solid 1.0 border lc rgb "black"
    set samples 111
    unset key
    
    plot '+' u (c=$0+1,PosX(c)):(PosY(c)):(0.4):(2.5):(ColorValue(c)) w boxxy lc rgb var, \
         '+' u (c=$0+1,PosX(c)):(PosY(c)+4.2):(ColorName(c)) w labels, \
         '+' u (c=$0+1,PosX(c)):(PosY(c)):(ColorValueLabel(c)):(ColorLabelColor(ColorValue(c))) \
             w labels tc rgb var font "Courier Bold,10"
    ### end of code
    

    结果:

    【讨论】:

      【解决方案2】:

      这是另一种方法,使用从数据文件或数据块中自动生成的调色板。 有了这个,您不必关心十六进制数字或任何颜色的数字或代码。 您只需要确保您没有使用无效的颜色名称。除非对长数据集的调色板长度有限制,否则这应该可以工作。

      ### colornames from data
      reset session
      
      $Data <<EOD
      1   40  red
      2   35  orange
      3   25  yellow
      4   20  greenyellow
      5   15  web-green
      6   12  web-blue
      7   10  violet
      EOD
      
      MyPalette = '('
      set table $Dummy
          plot $Data u (MyPalette = MyPalette.sprintf('%d "%s", ',$0,strcol(3))) with table
      unset table
      MyPalette = MyPalette[:strlen(MyPalette)-2].')'
      set palette model RGB defined @MyPalette
      unset colorbox 
      
      set yrange[0:]
      plot $Data u 1:2:(0.7):0 w boxes fs solid 1.0 lc palette notitle,\
          '' u 1:($2/2):(strcol(3)) with labels notitle
      ### end of code
      

      【讨论】:

        【解决方案3】:

        我很惊讶这些方法中的任何一种都有效!

        从文件中读取颜色信息的预期方法是

        plot foo using 1:2:3 lc rgb variable
        

        参见例如在线演示 rgb_variable.dem,它从数据文件 rgb_variable.dat 中读取颜色。

        但是,假设第 3 列包含十六进制 RGB(或 ARGB)值, 不是一个颜色名称的英文单词。 gnuplot 识别的颜色词名称是名称->RGB 映射的内置子集,该映射过去在系统文件 X11/rgb.txt 中普遍为 X11 提供。见Wikipedia X11 colors。此列表的一个变体成为 SVG 标准的一部分。 gnuplot 中包含的内容旨在用于交互式命令,而不是作为数据文件内容。

        当然可以使用 gnuplot 数组和用户函数编写扩展名称->RGB 映射的脚本。例如:

        $Data <<EOD
        1   40  orange
        2   35  cyan
        3   25  red
        4   15  yellow
        5   5   green
        EOD
        
        array colorname  = ["orange", "cyan", "red", "yellow", "green"]
        array colorvalue = [0xFFD700, 0x00DDDD, 0xDD0000, 0xFFFF00, 0x00DD00]
        N = |colorname|
        name2RGB(name) = sum [i=1:N] (name eq colorname[i] ? colorvalue[i] : 0) 
        
        set yrange [0:]
        set boxwidth 0.7
        set style fill solid 1.0
        unset key
        
        plot $Data u 1:2:(name2RGB(strcol(3))) w boxes fc rgb variable
        

        如果您经常处理此类文件内容,我想您可以将整个 SVG 或 X11 颜色名称表自动转换为这样一对 gnuplot 数组,并从 ~/.gnuplot 初始化中加载。

        【讨论】:

        • 感谢您的详细解答。没错,查找表是另一种方法,尽管如果您想包含所有 111 种预定义的 gnuplot 颜色(甚至是 X11 颜色名称的“无尽”列表),它会有点冗长。还没玩过~/.gnuplot。目前,我对我的版本 3 还是很满意的。
        • 其实是什么原因导致using部分定义的变量不能在with部分重用?例如plot '+' using 0:(i=$1) with p lc iplot $Data using 1:(myColor=strcol(3),$2) with p lc rgb myColor。虽然,plot DATA using x:y:pointsize:pointtype:color with points lc variable pt variable ps variable 正朝着这个方向发展,但不幸的是不是针对字符串变量。
        • 几个原因,其中最基本的原因是“with ”每幅图只评估一次,而“using ...”则针对数据集中的每个点重新评估。
        • 'with ' 每幅图只评估一次。没有例外。如果该评估包括带有关键字“变量”的属性,则设置一个标志,以便在数据输入期间填写所需的数据值。该机制对数字值与字符串值没有特殊限制,但到目前为止接受的属性“变量”碰巧需要数字而不是字符串。唯一需要字符串输入(“带标签”)的绘图样式不需要“变量”关键字,因为它总是以这种方式行事。
        • 2020 年添加:gnuplot 5.5 提供了一个函数rgbcolor("colorname"),它提供了缺失的步骤。 plot FOO using 1:2:(rgbcolor(stringcolumn(3))) lc rgb variable.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-23
        相关资源
        最近更新 更多