如何为feedgnuplot生成的直方图指定单个/特定条形着色?
实现feedgnuplot条形图指定团队标红
核心思路
要给特定团队的条形标红,本质是让gnuplot对目标数据使用不同的填充颜色。可以通过拆分数据分别绘制或条件判断动态设色两种方式实现。
方法1:拆分数据分别绘制
把需要标红的团队数据单独提取,用独立的plot命令指定红色填充,其余团队用默认颜色绘制。
修改后的gnuplot代码示例(以团队"B"为例):
set grid set boxwidth 1 histbin(x) = 1 * floor(0.5 + x/1) set style data histograms set style fill solid border lt -1 # 绘制普通团队条形(默认颜色) plot '-' using 3:xticlabels(2) notitle, \ # 绘制目标红色条形,指定填充色 '-' using 3:xticlabels(2) notitle fill rgb "red" lt 1 # 普通团队数据 2 "E" 8 3 "H" 7 5 "F" 5 6 "A" 4 7 "D" 3 8 "C" 2 e # 红色团队数据 4 "B" 6 e
对应feedgnuplot命令(自动拆分数据):
用awk预处理csv文件,自动分离目标团队和其他团队数据:
(awk '$2 != "B"' ../gnuplot.csv; echo "e"; awk '$2 == "B"' ../gnuplot.csv; echo "e") | /usr/bin/feedgnuplot --set 'style data histograms' --set 'style fill solid border lt -1' --set 'plot "-" using 3:xticlabels(2) notitle, "-" using 3:xticlabels(2) notitle fill rgb "red" lt 1'
方法2:条件判断动态设色
无需拆分数据,通过gnuplot的条件表达式动态为目标团队分配红色,其他团队用默认颜色。
修改后的gnuplot代码示例:
set grid set boxwidth 1 histbin(x) = 1 * floor(0.5 + x/1) set style data histograms set style fill solid border lt -1 # 用变量颜色:团队名为"B"时用红色,否则用默认色 plot '-' using 3:xticlabels(2):($2 eq "B" ? 1 : 0) notitle lc variable 2 "E" 8 3 "H" 7 4 "B" 6 5 "F" 5 6 "A" 4 7 "D" 3 8 "C" 2 e
对应feedgnuplot命令:
直接通过--set传递条件判断的plot参数(注意shell中转义双引号):
< ../gnuplot.csv /usr/bin/feedgnuplot --set 'style data histograms' --set 'style fill solid border lt -1' --set 'plot "-" using 3:xticlabels(2):($2 eq \"B\" ? 1 : 0) notitle lc variable'
扩展:标红多个团队
如果需要标红多个团队(比如"B"和"E"),只需调整逻辑:
- 方法1中把多个团队的数据都放到红色数据块里
- 方法2中把条件改为
($2 eq "B" || $2 eq "E" ? 1 : 0)
内容的提问来源于stack exchange,提问作者Phil
相关产品推荐
相关产品推荐

