输入:
我有一个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
图表的当前输出 来自上面的代码 :
附录问题:
问题4:我对标签/标题遇到了很大的困难。我已经尝试了答案中的代码,我得到了相同的结果。如何在不相互打印的情况下打印标题?
答案 0 :(得分:3)
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))
计算角度Ai
和mid
(Af
实际上不需要),然后返回坐标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.25
用于i=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)
我使用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
这是我使用您的数据获得的
这是完整的脚本:
#!/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
),或者在绘图行中从1开始every
命令:
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
答案 1 :(得分: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