gpt4 book ai didi

gnuplot - gnuplot 条形图中不同颜色的条形?

转载 作者:行者123 更新时间:2023-12-02 21:06:20 32 4
gpt4 key购买 nike

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

Critical 2
High 18
Medium 5
Low 14

根据该数据集在 gnuplot 中创建条形图很容易,但所有条形图的颜色都相同。我希望将 Critical 设为黑色,high 设为红色,等等,但似乎几乎没有任何在线教程可以做到这一点。

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

最佳答案

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)这就是我们如何让盒子保持适当的间距。

可能稍微更干净的(仅限 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 )完成间距,这是记录号在数据集中(在本例中为行号)。

关于gnuplot - gnuplot 条形图中不同颜色的条形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11082159/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com