[英]Gnuplot - use different line colors for average bar
I've got a simple CSV file consisting of 3 columns: date, value 1, value 2 .我有一个简单的 CSV 文件,包含 3 列:日期、值 1、值 2 。 I'm using gnuplot to create a stacked bar chart and I'd like the last bar (average values) to be plotted with a different color.
我正在使用 gnuplot 创建一个堆叠条形图,我希望最后一个条形图(平均值)用不同的颜色绘制。 I'm using the following script:
我正在使用以下脚本:
set terminal png size 1000, 500
set output "bars.png"
set title "Trends"
set style data histograms
set style histogram rowstacked
set yrange [0:*]
set style fill solid
set boxwidth 0.9
set xlabel "Clicks"
set ylabel "Dates"
set xtics right rotate by 45
set datafile separator ","
plot newhistogram, "data.csv" using 2:xtic(1) title "Direct" linecolor rgb "#0000ff",'' using 3 title "Indirect" linecolor rgb '#00ff00'
Sample input data file:示例输入数据文件:
2023-01-11,234,8756
2023-01-13,54,876
2023-01-14,3333,3566
2023-01-15,543,654
2023-01-16,657,767
2023-01-17,876,88
2023-01-18,1606,55
2023-01-20,888,77
Average,1024,1855
I tried to use a conditional value for linecolor but I'm getting an error I can't understand:我尝试使用 linecolor 的条件值,但出现了一个我无法理解的错误:
gnuplot> plot newhistogram, "/tmp/csv.csv" using 2:xtic(1) title "Direct" linecolor rgb "#0000ff", '' using 3 linecolor rgb (strcol(1) eq "Average"? '#ff50ff': '#43FF00') ^ line 0: stringcolumn() called from invalid context
Welcome to StackOverflow, To be honest, I don't know how to set conditional colors in the plotting style with histogram
.欢迎来到 StackOverflow,老实说,我不知道如何在
with histogram
的绘图样式中设置条件 colors 。 Maybe there is a way which I am not aware of.也许有一种我不知道的方法。 Since your data is relatively simple I would go via plotting style
with boxxyerror
(check help boxxyerror
).由于您的数据相对简单,我会通过
with boxxyerror
(查看help boxxyerror
)。 There your have full flexibility, but have to think a bit more what you are plotting.在那里你有充分的灵活性,但必须多想想你在策划什么。 Check the following example:
检查以下示例:
Script:脚本:
### bar chart with conditional colors
reset session
$Data <<EOD
2023-01-11,234,8756
2023-01-13,54,876
2023-01-14,3333,3566
2023-01-15,543,654
2023-01-16,657,767
2023-01-17,876,88
2023-01-18,1606,55
2023-01-20,888,77
Average,1024,1855
EOD
set title "Trends"
set style histogram rowstacked
set yrange [0:*]
set style fill solid
set xlabel "Clicks"
set ylabel "Dates"
set xtics right rotate by 45
set datafile separator ","
set offsets 0.5,0.5,0,0
myBoxwidth = 0.9
myColor(colD,colL) = (avg = strcol(colL) eq "Average" , \
colD==2 ? avg ? 0xaaaaff : 0x000ff : avg ? 0xaaffaa : 0x00ff00)
plot $Data u 0:($2/2): (myBoxwidth/2.):($2/2):(myColor(2,1)):xtic(1) w boxxy ti "Direct" lc rgb var, \
'' u 0:($2+$3/2):(myBoxwidth/2.):($3/2):(myColor(3,1)) w boxxy ti "Indirect" lc rgb var
### end of script
Result:结果:
I couldn't find a way to use a conditional for the linecolor directive, so I ended up with a conditional for using .我找不到为linecolor指令使用条件的方法,所以我最终得到了使用. The updated plot command which does the trick is as follows:
更新后的plot命令如下所示:
plot newhistogram, "data.csv" \
using (strcol(1) ne 'Average' ? $2 : NaN):xtic(1) title "Direct" lc rgb "#0000ff", \
'' using (strcol(1) ne 'Average' ? $3 : NaN) title "Indirect" lc rgb '#00ff00', \
'' using (strcol(1) eq 'Average' ? $2 : NaN) notitle lc rgb '#0088ff', \
'' using (strcol(1) eq 'Average' ? $3 : NaN) notitle lc rgb '#00ff88'
The first 2 lines plot the rows not starting with 'Average' and the last 2 plot the last row which does.前 2 行 plot 不是以“平均”开头的行,最后 2 行 plot 是最后一行。 Each column's color is plotted with an arbitrary color.
每列的颜色都用任意颜色绘制。 The output now looks as intended.
output 现在看起来符合预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.