2016-04-22 92 views
5

輸入:如何修改使用Gnuplot創建一個餅圖

我有一個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

電流圖的輸出:

此代碼生成此餅圖: Output .png pie chart

問題:

Q1:我怎樣才能顏色分配給每個扇區RGB格式的圖表? Q2:有什麼方法可以將標籤放在右側角落? 問題3:如何將值放在圖表上?

圖的理想輸出: enter image description here

附錄:

我進一步提高了我的代碼的答案的幫助。在答案中的代碼沒有聲張爲我工作,所以我不得不把它調整到:從代碼表

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

電流輸出上面

enter image description here

附錄問題:

Q4:我對標籤/標題有很大的困難。我已經嘗試了答案中的代碼,我得到了相同的結果。如何在不打印對方的情況下打印標題?

+1

只是一個評論:你把[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

+0

@vagoberto是的!那就是訣竅!因爲對於除了樣本之外的其他值 - 它不是將餡餅畫成一個完整的圓圈,而是像口袋裏打開的餡餅(<但是'every :: i :: i')做了訣竅 - 我也更新了我的回答!謝謝...... – 3kstc

+0

我會再次禮貌地要求社區投票贊成這個問題,因爲我已經分享了我自己的恩惠,不僅讓我自己受益,還讓社區受益。 – 3kstc

回答

2

post you cited已經提出了一種放置標籤和百分比值的方法。爲了實現這一目標,讓我逐步解釋步驟。最後我寫完整的腳本。

問題3:如何將值放在圖表上?

每個切片在兩個角度內定義(Ai,Af)。百分比值應放置在每一個的中間,在(x,y)=(0.5*cos(mid), 0.5*sin(mid))處,其中mid=0.5*(Ai+Af)是中點角度,0.5表示餅圖半徑的一半。

因爲我們可以設置Ai=0中的第一項,角度Af從你的數據計算Af=ang($2),其中ang(x)在你的腳本中定義。 對於第二項,我們更新Ai=Af並再次計算Af=ang($2),依此類推。

最後,以下行應該把百分比:

plot 'myfile.csv' 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))) every ::1 w labels center font ',10' 

注:第一括號(mid=Ai+ang($2), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid))計算(實際上是不需要AfAimid角度,然後返回座標x=-0.5*cos(mid)

警告: x和y軸必須具有相同的範圍:

set xrange [-1.5:1.5] 
set yrange [-1.5:1.5] 

Q2:有沒有一種方法,我可以放置在右上角的標籤?

對於顏色的方塊,你可以在固定的x積點,並以等間隔的y值:

for [i=1:STATS_records] '+' using (1.3):(i*0.25):(i) pt 5 ps 4 linecolor palette 

其中STATS_records是您的文件的行(你已經與stats 'myfile.csv'計算的數)。該行使用僞文件'+',並且使用規範具有三列(x):(y):(color),其中x=1.3是固定的,y=i*0.25i=1,2,3,...,STATS_records,並且color=i是由linecolor palette使用的。點是具有4個像素長度的實心方塊(pt 5)(ps 4)。顏色的繪製順序與相應的圓形切片相同。

plot for [i=1:STATS_records] 'myfile.csv' u (1.45):(i*0.25):1 every ::i::i with labels center font ',10' 

,其中使用規範中的列(x):(y):(name)

每種顏色的標籤可以被繪製。 x=1.45的值稍大於之前使用的值,並且y=i*0.25必須與彩色正方形的值相同。 name=$1從列1中提取字符串,該列由with labels使用。

注意:您可以使用with labels font 'Arial,10'控制標籤的字體和大小。您可以省略字體名稱font ',10'

Q1:如何以RGB格式爲圖形的每個扇區分配顏色?

在最後一個命令,我使用linecolor palette,使我們能夠用

set palette defined (1 1 0.788 0.055, 2 0.090 0.161 0.659) 

其中I使用RGB的顏色,縮放,以限定顏色的[0,1](黃色狀是1 0.788 0.055;藍色樣是0.090 0.161 0.659)。

注:餅圖應該現在得出:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i) every ::i-1::i-1 with circle linecolor palette 

這是我與你的數據

pie-chart

這是一個完整的腳本獲得:

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

# output 
set terminal png 
set output 'piechart.png' 
set size square 

set title "\n" 
set label 1 "My Pie Chart\nShift Usage Break Down" at graph 00.5,0.95 left 

set xrange [-1.5:2.5]  # length (2.5+1.5) = 4 
set yrange [-2:2]   # length (2+2) = 4 
set style fill solid 1 

# unset border   # remove axis 
unset tics    # remove tics on axis 
unset colorbox   # remove palette colorbox 
unset key     # remove titles 

# some parameters 
Ai = 15.0;    # init 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 ::i::i 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))) ever\ 
y ::1 w labels center font ',10',\ 
    for [i=1:STATS_records] dataname u (1.45):(i*0.25):1 every ::i::i 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 

更新:原始myfile.csv有一個標題(第一行Shift,Percentage)gnuplot不能正確讀取。我們可以通過評論它忽略此行(添加#字符,如# Shift,Percentage),或將一個every命令中的情節線,從1開始:

plot for [i=1:STATS_records] dataname u (0):(0):(1):(Ai):(Ai=Ai+ang($2)):(i) every ::i::i 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))) ever\ 
y ::1 w labels center font ',10',\ 
    for [i=1:STATS_records] dataname u (1.45):(i*0.25):1 every ::i::i with labels left,\ 
    for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 lc palette 
+0

感謝您的全面解釋 - 代碼沒有我現在有問題 - 我已經添加了一個詳細的描述問題 - 你能幫我嗎? – 3kstc

+0

我把第二個'for'換成了'for [for [ i = 1:STATS_records] dataname u(1.45):(i * 0.25):1 every :: i :: i with left left,'' – 3kstc

+0

我對GNUPlot有點不安,我有'set border'並試過[GuidoLabs](http://labs.guidolin.net/2010/03/how-to-create-beautiful-gnuplot-graphs.html)的一些內容,但我想在整個圖像周圍創建一個邊框,以便標題和圖例坐在邊框內 - 我怎麼能做到這一點? – 3kstc

2

通過調整和使用Vagobertos好解釋的代碼(因爲代碼並沒有爲我工作),並做進一步的讀物,我的代碼如下:

GNUPLOT代碼:

#!/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.888 0.055, 2 0.156 0.455 0.651) # format R G B (scaled to [0,1]) 


set palette defined (1 0.961 0.690 0.255, 2 0.180 0.525 0.757) # 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 ::i::i 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 ',16',\ 
     for [i=1:STATS_records]dataname u (1.45):(i*0.25):1 every ::i::i with labels left font 'Arial-Bold,10',\ 
     for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 linecolor palette 


# *************************************************** +--------------------   shape code= 
#  for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 8 ps 4 linecolor palette   # empty triangle 
#  for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 9 ps 4 linecolor palette   # solid triangle 
#  for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 7 ps 4 linecolor palette   # solid circle 
#  for [i=1:STATS_records] '+' u (1.3):(i*0.25):(i) pt 6 ps 4 linecolor palette   # empty circle 


# 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 

輸出餅圖: enter image description here