gnuplot条形图中的不同彩色条? [英] Different coloured bars in gnuplot bar chart?

查看:69
本文介绍了gnuplot条形图中的不同彩色条?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a very simple dataset:

Critical 2
High 18
Medium 5
Low 14

在gnuplot中从该数据集创建条形图很容易,但是所有条形都是相同的颜色.我希望它具有临界"为黑色,高"为红色等的功能,但是似乎几乎没有任何在线教程可以做到这一点.

Creating a bar chart in gnuplot out of this dataset is easy, but all the bars are the same colour. I want to have it so that Critical is black, high is red, etc, but there seem to be hardly any online tutorials for doing this.

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

推荐答案

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 <number>i <number>简短地绘制每个索引,就像我所做的那样.另外,索引号也可以作为column(-2)来获得,这就是我们使框正确隔开的方式.

This takes sed and triple spaces your file so that gnuplot sees each line as a different dataset (or "index"). You can plot each index separately using index <number> or i <number> for short as I have done. Also, the index number is available as column(-2) which is how we get the boxes properly spaced.

可能使用过滤器的方法更干净(仅适用于gnuplot):

Possibly a slightly more clean (gnuplot only) solution is using filters:

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)来完成间距)在数据集中(在本例中为行号).

This solution also doesn't depend on any particular ordering in your datafile (which is why I prefer it slightly to the other solution. We accomplish the spacing here with column(0) (or $0) which is the record number in the dataset (in this case, the line number).

这篇关于gnuplot条形图中的不同彩色条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆