输入:
我有一个myfile.csv文件,其中包含以下信息:
Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06GNUPlot处理:
向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并以此作为参考文献创建。
海图的电流输出:
此代码生成以下饼图:

问题:
Q1:如何以RGB格式为图形的每个扇区分配颜色?Q2:有什么方法可以将标签放置在右手角?Q3:,如何在图表上放置值?
图的理想输出:

增编:
在答案的帮助下,我进一步改进了我的代码。答案中的代码对我并不适用,所以我不得不将其修改为:
#!/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:,我在标签/标题上遇到了很大的困难。我尝试了答案中的代码,得到了同样的结果。我如何打印标题而不相互打印?
发布于 2016-04-24 04:26:46
你引用的帖子已经建议了一种放置标签和百分比值的方法。让我逐一解释一下实现这一目标的步骤。最后,我写了完整的脚本。
Q3:,我怎样才能把这个值放在图表上?
每个切片定义在两个角度的(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 for i=1,2,3,...,STATS_records和color=i由linecolor palette使用。点是填充平方(pt 5)与4个像素的长度(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。
注:饼形图现在应该用:
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),它不能正确读取。我们可以忽略这一行,方法是注释它(添加一个#字符,例如# 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发布于 2016-04-27 22:59:12
通过调整和使用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输出分段:

https://stackoverflow.com/questions/36783738
复制相似问题