2016-11-14 4 views
1

GNUPLOT을 사용하여 파이 차트를 생성하려고합니다. 이미 플롯을 생성했으며 배열을 사용하여 범례에 색상을 할당했습니다. 이 코드는, 내가 script.sh이GNUPLOT - 원형 차트에 색상 할당

#!/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

입니다

question 참조로 사용하고있다

"Tipo","Valor" "Periodo laboral",723 "Periodo no laboral",81 

그리고 스크립트를 실행하면 output.png이 표시됩니다. 이

enter image description here

당신이 볼 수 있듯이

을 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) 부분을 제거했고, 지금은이 completly 파란색 원형 차트를 그리는 방법

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 돈을 것 때문에 난 여전히 인덱스 또는 뭔가가 필요 그 가치를 바꾸지 마라. 원형 차트 음모를 꾸미고

답변

1

구분 적 작동합니다 :

colorsrgb = "#0000FF #FF0000" 

plot for [i=1:rowf-rowi+1] filename u (centerX):(centerY):(radius):(pos):(pos=pos+angle($2)) every ::i::i w circle lc rgb word(colorsrgb, i) 

enter image description here

Windows에서의 gnuplot 5.0.0 테스트.