我有一個myfile.csv
文件具有以下信息:
Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06
GNUPLOT處理:
的myfile.csv
文件餵了pie_chart_generator.gnuplot
這個文件是:
#!/usr/bin/gnuplot -persist
reset
set title "\n"
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 0,1.125 left
set terminal wxt
unset key
set datafile separator ","
set terminal png
set size square
set output "piechart.png"
stats 'myfile.csv' u 2 noout # get STATS_sum (sum of column 2)
ang(x)=x*360.0/STATS_sum # get angle (grades)
perc(x)=x*100.0/STATS_sum # get percentage
#set size square # square canvas
set xrange [-1:1.5]
set yrange [-1.25:1.25]
set style fill solid 1
unset border
unset tics
unset key
Ai = 0.0; Bi = 0.0; # init angle
mid = 0.0; # mid angle
i = 0; j = 0; # color
yi = 0.0; yi2 = 0.0; # label position
plot 'myfile.csv' u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i=i+6) with circle linecolor var
並與此創建爲reference。
電流圖的輸出:
問題:
Q1:我怎樣才能顏色分配給每個扇區RGB格式的圖表? Q2:有什麼方法可以將標籤放在右側角落? 問題3:如何將值放在圖表上?
附錄:
我進一步提高了我的代碼的答案的幫助。在答案中的代碼沒有聲張爲我工作,所以我不得不把它調整到:從代碼表的
#!/usr/bin/gnuplot -persist
reset
dataname = 'myfile.csv'
set datafile separator ','
# Get STATS_sum (sum of column 2) and STATS_records
stats dataname u 2 noout
# Define angles and percentages
ang(x)=x*360.0/STATS_sum # get angle (grades)
perc(x)=x*100.0/STATS_sum # get percentage
# Set Output
set terminal png
set output "piechart.png"
set size square
# Print the Title of the Chart
set title "\n"
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 0,1.125 left
#set terminal wxt
unset key
set key off
set xrange [-1.5:1.5]
set yrange [-1.5:1.5]
set style fill solid 1
unset border
unset tics
unset colorbox
# some parameters
Ai = 5.0; # Initial angle
mid = 0.0; # Mid angle
# This defines the colors yellow~FFC90E, and blue~1729A8
# Set palette defined (1 '#FFC90E', 2 '#1729A8') # format '#RRGGBB'
set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659) # format R G B (scaled to [0,1])
plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i) every ::2 with circle linecolor palette,\
dataname u (mid=(Ai+ang($2)), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)):(-0.5*sin(mid)):(sprintf('%.2f\%', $2, perc($2))) w labels center font ',10',\
for [i=1:STATS_records]dataname u (1.45):(i*0.25):1 every ::1 with labels left,\
for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 lc palette
# first line plot semicircles: (x):(y):(radius):(init angle):(final angle):(color)
# second line places percentages: (x):(y):(percentage)
# third line places the color labels
# fourth line places the color symbols
unset output
電流輸出上面:
附錄問題:
Q4:我對標籤/標題有很大的困難。我已經嘗試了答案中的代碼,我得到了相同的結果。如何在不打印對方的情況下打印標題?
只是一個評論:你把[i] ... every :: i-1 :: i-1'的第一個plot改爲'every :: 2'。這樣你就可以兩次繪製餅圖**,因爲你讀取了每一個「i」值('for [i = 1:2])的全部數據('every :: 2'從第2行讀到末尾)記錄]')。這就是爲什麼你看到重疊的標題。我會寫'plot for [i = 1:STATS_records] ... every :: i :: i'。請在我的答案結尾處查看我的編輯。 – vagoberto
@vagoberto是的!那就是訣竅!因爲對於除了樣本之外的其他值 - 它不是將餡餅畫成一個完整的圓圈,而是像口袋裏打開的餡餅(<但是'every :: i :: i')做了訣竅 - 我也更新了我的回答!謝謝...... – 3kstc
我會再次禮貌地要求社區投票贊成這個問題,因爲我已經分享了我自己的恩惠,不僅讓我自己受益,還讓社區受益。 – 3kstc