代码之家  ›  专栏  ›  技术社区  ›  theozh

gnuplot:从数据文件应用颜色名称

  •  0
  • theozh  · 技术社区  · 6 年前

    如何从数据文件中获取颜色名称? 我没想到这会这么难,但显然是这样。 ...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
    

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  4
  •   theozh    6 年前

    如果有人想像@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颜色的概述(尽管,在 #rrggbb 格式)。

    ### display all predefined gnuplot colors
    reset session
    set term wxt size 1000,700 enhanced  # or change to other terminal
    
    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] = \
    ['#ffffff', '#000000', '#a0a0a0', '#ff0000', '#00c000', '#0080ff', '#c000ff', '#00eeee', '#c04000', '#c8c800', \
    '#4169e1', '#ffc020', '#008040', '#c080ff', '#306080', '#8b0000', '#408000', '#ff80ff', '#7fffd4', '#a52a2a', \
    '#ffff00', '#40e0d0', '#000000', '#1a1a1a', '#333333', '#4d4d4d', '#666666', '#7f7f7f', '#999999', '#b3b3b3', \
    '#c0c0c0', '#cccccc', '#e5e5e5', '#ffffff', '#f03232', '#90ee90', '#add8e6', '#f055f0', '#e0ffff', '#eedd82', \
    '#ffb6c1', '#afeeee', '#ffd700', '#00ff00', '#006400', '#00ff7f', '#228b22', '#2e8b57', '#0000ff', '#00008b', \
    '#191970', '#000080', '#0000cd', '#87ceeb', '#00ffff', '#ff00ff', '#00ced1', '#ff1493', '#ff7f50', '#f08080', \
    '#ff4500', '#fa8072', '#e9967a', '#f0e68c', '#bdb76b', '#b8860b', '#f5f5dc', '#a08020', '#ffa500', '#ee82ee', \
    '#9400d3', '#dda0dd', '#905040', '#556b2f', '#801400', '#801414', '#804014', '#804080', '#8060c0', '#8060ff', \
    '#808000', '#ff8040', '#ffa040', '#ffa060', '#ffa070', '#ffc0c0', '#ffff80', '#ffffc0', '#cdb79e', '#f0fff0', \
    '#a0b6cd', '#c1ffc1', '#cdc0b0', '#7cff40', '#a0ff20', '#bebebe', '#d3d3d3', '#d3d3d3', '#a0a0a0', '#a0b6cd', \
    '#000000', '#1a1a1a', '#333333', '#4d4d4d', '#666666', '#7f7f7f', '#999999', '#b3b3b3', '#cccccc', '#e5e5e5', \
    '#ffffff']
    
    #convert a hex string to dec format
    hex2dec(hex)=int('0x'.hex)
    # get decimal number for R,G,B
    R(ColorCode) = hex2dec(ColorCode[2:3])
    G(ColorCode) = hex2dec(ColorCode[4:5])
    B(ColorCode) = hex2dec(ColorCode[6:7])
    
    # "empirical" formula do decide whether using a white or a black label on the colored background
    LabelColor(ColorCode) = \
       (R(ColorCode)+G(ColorCode)*1.5+B(ColorCode)*0.5)/3. > 127 ? "#000000" : "#ffffff"
    
    # Settings
    PosYOffset = 4.5
    SizeX = 0.7
    SizeY = 5
    Cols = 10
    
    # loop for placing colored rectangles and labels
    do for[i=1:111] {
      PosX = i%Cols
      PosY = floor(i/Cols)*Cols
      set object i rectangle at PosX, PosY size SizeX,SizeY fc rgb ColorNames[i]
      set label i at PosX, PosY+PosYOffset ColorNames[i] center font ",9"
      set label 200+i at PosX, PosY ColorValues[i] tc rgb LabelColor(ColorValues[i]) center font ",9"
    }
    
    set title "Predefined colors in gnuplot"
    set xrange[-0.5:9.5]
    set xtics 1
    set link x2 via x inverse x
    set x2tics 1
    
    set yrange[119:-5] reverse
    set ytics 10
    set link y2 via y inverse y
    set y2tics 10
    
    # plot invisible dummy data
    plot '-' w p ps 0 not
    0 0
    e
    ### end of code
    

    enter image description here

        2
  •  3
  •   Ethan    6 年前

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

    plot foo using 1:2:3 lc rgb variable
    

    请参见在线演示 rgb_variable.dem

    但是,假设第3列包含十六进制的RGB(或ARGB)值, Wikipedia X11 colors . 这个列表的一个变体成为SVG标准的一部分。gnuplot中的包含用于交互式命令,而不是作为数据文件内容使用。

    $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
    

    enter image description here

        3
  •  1
  •   theozh    5 年前

    下面是另一种方法,它使用从数据文件或数据块自动生成的调色板。 有了这个你就不必关心十六进制数或任何数字或颜色代码。

    ### 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
    

    enter image description here