我正在嘗試使用GNUPLOT生成餅圖,我已經生成了繪圖,並且已經使用數組爲顏色分配了圖例。這是代碼,我已經使用這個question作爲參考GNUPLOT - 在餅圖中分配顏色
script.sh
#!/usr/bin/gnuplot
set terminal pngcairo nocrop enhanced size 800,400 font "Siemens Sans,8"
set output 'output.png'
filename = 'source.dat'
rowi = 1
rowf = 2
# obtain sum(column(2)) from rows `rowi` to `rowf`
set datafile separator ','
stats filename u 2 every ::rowi::rowf noout prefix "A"
# rowf should not be greater than length of file
rowf = (rowf-rowi > A_records - 1 ? A_records + rowi - 1 : rowf)
angle(x)=x*360/A_sum
percentage(x)=x*100/A_sum
# circumference dimensions for pie-chart
centerX=0
centerY=0
radius=0.2
# label positions
yposmin = 0.0
yposmax = 0.95*radius
xpos = 1.5*radius
ypos(i) = yposmax - i*(yposmax-yposmin)/(1.0*rowf-rowi)
#-------------------------------------------------------------------
# now we can configure the canvas
set style fill solid 1 # filled pie-chart
unset key # no automatic labels
unset tics # remove tics
unset border # remove borders; if some label is missing, comment to see what is happening
set size ratio -1 # equal scale length
set xrange [-radius:2*radius] # [-1:2] leaves space for labels
set yrange [-radius:radius] # [-1:1]
#-------------------------------------------------------------------
pos = 0 # init angle
colour = 0 # init colour
colors = "blue red"
# 1st line: plot pie-chart
# 2nd line: draw colored boxes at (xpos):(ypos)
# 3rd line: place labels at (xpos+offset):(ypos)
plot filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every ::rowi::rowf w circle lc var,\
for [i=0:rowf-rowi] '+' u (xpos):(ypos(i)) w p pt 5 ps 4 lc rgb word(colors,i+1),\
for [i=0:rowf-rowi] filename u (xpos):(ypos(i)):(sprintf('%05.2f%% %s', percentage($2), stringcolumn(1))) every ::i+rowi::i+rowi w labels left offset 3,0
源文件生成餅圖是本
source.dat
"Tipo","Valor"
"Periodo laboral",723
"Periodo no laboral",81
而當我運行腳本時,我得到一個output.png
文件看起來像這樣
output.png
正如你所看到的,餅圖顏色不適合傳奇色彩。這是因爲我可以通過for
索引輕鬆設置圖例顏色,但是爲了生成餅圖,我重複了every
子句,因此無法獲取索引。我也曾嘗試類似:
plot filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every ::rowi::rowf w circle lc rgb(word(colors, colour)),\
,但我有以下錯誤:
"gnuplot/piechart.sh", line 49: warning: This plot style does not work with 6 cols. Setting to xyerrorbars
你能幫助我嗎?提前致謝。
修訂
我想我已經邁出了一步。我已刪除繪圖命令的(colour=colour+1)
一部分,現在我可以設置一個特定的顏色這樣
plot filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)):(colour=colour+1) every ::rowi::rowf w circle lc rgb word(colors,colour+1),\
但這繪製餅圖完全地藍,我還需要一個索引或東西,因爲它似乎colours
唐不會改變它的價值。