The 您引用的帖子 https://stackoverflow.com/questions/31896718/generation-of-pie-chart-using-gnuplot已经建议了一种放置标签和百分比值的方法。让我逐条解释实现这一目标的步骤。最后我写了完整的脚本。
Q3:如何将值放在图表上?
每个切片都在两个角度内定义(Ai,Af)
。百分比值应放置在每个值的中间,即(x,y)=(0.5*cos(mid), 0.5*sin(mid))
, where mid=0.5*(Ai+Af)
是中点角,并且0.5
代表饼图半径的一半。
对于第一个条目,我们可以设置Ai=0
,和角度Af
根据您的数据计算得出Af=ang($2)
, where 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'
Note:第一个括号(mid=Ai+ang($2), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid))
计算角度Ai
and mid
(Af
其实不需要),然后返回坐标x=-0.5*cos(mid)
.
Caveat:x 轴和 y 轴must具有相同的范围:
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
where STATS_records
是文件的行数(您已经使用它计算过stats 'myfile.csv'
)。该行使用伪文件“+”,并且使用规范有三列(x):(y):(color)
, where x=1.3
fixed, y=i*0.25
for i=1,2,3,...,STATS_records
, and 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)
. The x=1.45
值略大于之前使用的值,并且y=i*0.25
必须与彩色方块相同。name=$1
从第 1 列中提取字符串,该字符串用于with labels
.
Note:您可以使用以下命令控制标签的字体和大小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)。
Note:现在应该使用以下命令绘制饼图:
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
Update:原本的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