【问题标题】:Different coloured bars in gnuplot bar chart?gnuplot条形图中不同颜色的条形图?
【发布时间】:2012-06-18 11:48:43
【问题描述】:

我有一个非常简单的数据集:

Critical 2
High 18
Medium 5
Low 14

用这个数据集在 gnuplot 中创建一个条形图很容易,但是所有的条形图都是相同的颜色。我想要它,以便关键是黑色,高是红色等,但似乎几乎没有任何在线教程可以做到这一点。

谁能指出我正确的方向?

【问题讨论】:

    标签: gnuplot


    【解决方案1】:
    set xrange [-.5:3.5]
    set yrange [0:]
    set style fill solid
    plot "<sed 'G;G' test.dat" i 0 u (column(-2)):2:xtic(1) w boxes ti "Critical" lc rgb "black",\
         "<sed 'G;G' test.dat" i 1 u (column(-2)):2:xtic(1) w boxes ti "High" lc rgb "red" ,\
         "<sed 'G;G' test.dat" i 2 u (column(-2)):2:xtic(1) w boxes ti "Medium" lc rgb "green",\
         "<sed 'G;G' test.dat" i 3 u (column(-2)):2:xtic(1) w boxes ti "Low" lc rgb "blue"
    

    这需要sed 和三倍空间您的文件,以便 gnuplot 将每一行视为不同的数据集(或“索引”)。您可以像我所做的那样使用index &lt;number&gt;i &lt;number&gt; 分别绘制每个索引。此外,索引号以column(-2) 的形式提供,这就是我们如何使框正确间隔的方式。

    使用过滤器可能更干净(仅限 gnuplot)解决方案:

    set xrange [-.5:3.5]
    set yrange [0:]
    set style fill solid
    CRITROW(x,y)=(x eq "Critical") ? y:1/0
    HIGHROW(x,y)=(x eq "High") ? y:1/0
    MIDROW(x,y) =(x eq "Medium") ? y:1/0
    LOWROW(x,y) =(x eq "Low") ? y:1/0
    plot 'test.dat' u ($0):(CRITROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "black" ti "Critical" ,\
         '' u ($0):(HIGHROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "red" ti "High" ,\
         '' u ($0):(MIDROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "green" ti "Medium" ,\
         '' u ($0):(LOWROW(stringcolumn(1),$2)):xtic(1) w boxes lc rgb "blue" ti "Low"
    

    此解决方案也不依赖于数据文件中的任何特定顺序(这就是为什么我更喜欢它而不是其他解决方案。我们在此处使用记录号column(0)(或$0)完成间距在数据集中(在本例中为行号)。

    【讨论】:

      【解决方案2】:

      您可以使用linecolor variable 选项执行此操作。

      如果您知道行始终处于相同的已知顺序,则可以使用行号(第零列,$0)作为线型索引:

      set style fill solid noborder
      set linetype 1 lc rgb 'black'
      set linetype 2 lc rgb 'red'
      set linetype 3 lc rgb 'yellow'
      set linetype 4 lc rgb 'green'
      
      set yrange [0:*]
      unset key
      plot 'alerts.txt' using 0:2:($0+1):xtic(1) with boxes linecolor variable
      

      如果顺序可能不同,可以使用 gnuplot 风格的索引函数,它从以空格分隔的单词的字符串中确定警告级别的索引:

      alerts = 'Critical High Medium Low'
      index(s) = words(substr(alerts, 0, strstrt(alerts, s)-1)) + 1
      
      set style fill solid noborder
      set linetype 1 lc rgb 'black'
      set linetype 2 lc rgb 'red'
      set linetype 3 lc rgb 'yellow'
      set linetype 4 lc rgb 'green'
      
      set yrange [0:*]
      unset key
      plot 'alerts.txt' using 0:2:(index(strcol(1))):xtic(1) with boxes linecolor variable
      

      【讨论】:

        猜你喜欢
        • 2017-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-26
        • 1970-01-01
        • 2015-10-07
        相关资源
        最近更新 更多