基于此https://stackoverflow.com/a/63699595/1140754我定义了一个辅助
myLinecolor(i) = word(LINECOLORS,i)
来为每个条形使用不同的颜色,它对应于输入数据文件中一行的值。
但是,关于正在绘制的条形索引(即数据文件中的行),我应该将哪个参数传递给
myLinecolor()
?
通过
myLinecolor(variable)
会出现错误 internal error : non-INTGR argument
LINECOLORS = 'red green blue yellow brown gray turquoise'
myLinecolor(i) = word(LINECOLORS,i)
set output 'results.svg'
# using xval:ydata:box_width:color_index:xtic_labels
# 0:5:(0.8):0:xtic(1)
# xval 0- each row
# ydata 5 - value of column 5
# box_width 0.8
# color_index 0 - same as the number of the row
# xtic_labels xtic(1) - value of column 1
plot 'results.csv' every ::1 using 0:5:(0.8):0:xtic(1) with boxes lc rgb myLinecolor(variable)
例如我想要 bar1=红色,bar2=绿色,...,bar7=青绿色
您还可以通过
linestyle
或 linetype
和索引定义线条颜色,请检查 help linestyle
和 help linetype
。这里是通过lc rgb
完成的,检查help linecolor
。
脚本:
### define line color from a string of colornames
reset session
$Data <<EOD
1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8
2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
3 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8
EOD
LINECOLORS = 'red green blue yellow brown gray turquoise'
myLinecolor(i) = word(LINECOLORS,i)
set key top left
plot for [col=2:8] $Data u 1:col w lp pt 7 lc rgb myLinecolor(col-1) ti sprintf("Column %d",col)
### end of script
结果:
补充:
这是一个条形图的示例,其中每个条形都有不同的颜色,如带有颜色的字符串中定义的那样。
脚本:
### bar chart with colors defined in string
reset session
$Data <<EOD
A 1.1
B 6.2
C 4.3
D 2.4
E 3.5
F 7.6
G 5.7
EOD
set yrange[0:]
set style fill solid 1.0
set boxwidth 0.8
set key noautotitle
# colors red green blue yellow brown grey turquoise
myColors = "0xff0000 0x00ff00 0x0000ff 0xffff00 0xa52a2a 0xcccccc 0x30e0d0"
myColor(i) = int(word(myColors,int(i+1)))
plot $Data u 0:2:(myColor($0)):xtic(1) w boxes lc rgb var
### end of script
如果将最后一个块替换为
,您会得到类似的结果set linetype 1 lc "red"
set linetype 2 lc "green"
set linetype 3 lc "blue"
set linetype 4 lc "yellow"
set linetype 5 lc "brown"
set linetype 6 lc "grey"
set linetype 7 lc "turquoise"
plot for [i=1:7] $Data every ::i-1::i-1 u (i):2:xtic(1) w boxes lt i
但是,我更喜欢第一个变体。
结果: