输入:
我有一个myfile.csv包含以下信息的文件:
Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
并以此作为参考创建.
图表的当前输出:
问题:
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
Run Code Online (Sandbox Code Playgroud)
上面代码中的 当前图表输出:
附录问题:
问题4:我对标签/标题遇到了很大的困难.我已经尝试了答案中的代码,我得到了相同的结果.如何在不相互打印的情况下打印标题?
您引用的帖子已经建议了一种放置标签和百分比值的方法。让我逐条解释实现这一目标的步骤。最后我写了完整的脚本。
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'
Run Code Online (Sandbox Code Playgroud)
注意:第一个括号(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]
Run Code Online (Sandbox Code Playgroud)
Q2:有没有办法将标签放在右上角?
对于彩色方块,您可以使用以下方法在固定 x 和等距 y 值处绘制点:
for [i=1:STATS_records] '+' using (1.3):(i*0.25):(i) pt 5 ps 4 linecolor palette
Run Code Online (Sandbox Code Playgroud)
其中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。这些点是填充正方形 ( 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'
Run Code Online (Sandbox Code Playgroud)
其中使用规范有 columns (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)
Run Code Online (Sandbox Code Playgroud)
我使用 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
Run Code Online (Sandbox Code Playgroud)
这是我从你的数据中得到的
这是完整的脚本:
#!/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
Run Code Online (Sandbox Code Playgroud)
更新:原始文件有一个gnuplot 无法正确读取的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
Run Code Online (Sandbox Code Playgroud)