在饼图上居中放置标签

时间:2018-12-04 13:18:42

标签: label gnuplot pie-chart

我正在跟踪this post,使用Gnuplot制作饼图。这种方法的唯一问题是我无法对齐百分比标签。我在这里想念什么?

数据文件:

"Others"        1.085117e-01    3.904323e-02
"D_o"           2.894902e-01    6.145359e-01
"{/Symbol b}_o" 5.760601e-01    3.760299e-01
"O_h"           5.393108e-01    1.000000e+00
"D_p"           6.743313e-01    2.284404e-01
"{/Symbol a}_p" 1.000000e+00    1.271822e-01
"{/Symbol b}_f" 4.020115e-01    2.233656e-01
"D_m"           2.389996e-01    8.577689e-02
"{/Symbol a}_m" 3.601146e-01    1.033153e-01
"{/Symbol b}_m" 5.596836e-01    1.947165e-01

代码:

#!/usr/bin/gnuplot

# Terminal & Encoding
set terminal epscairo enhanced color dashed rounded size 8.5, 4.5
set output 'mu_piechart.eps'
set termoption enhanced
set encoding utf8 

# Get Status
filename = './datafile.dat'
stats filename u 2 noout

# Get Angles & Percentages
ANG(x)=x*360.0/STATS_sum
PER(x)=x*100.0/STATS_sum

# Square Canvas
set size square
set xrange [-1:1.5]
set yrange [-1.25:1.25]
set style fill solid 1

# Remove Base Properties (Titles, Tics, Axis, Palette)
unset key
unset tics
unset border
unset colorbox

# Initial Angle, Mid Angle, Initial Color
A = 0.0; M = 0.0; i = 0;

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

# Plot
plot for [i=0:STATS_records-1] filename u (0):(0):(1):(A):(A=A+ANG($2)):(i) every ::i::i with circle linecolor palette,\
     filename u (M=A+ANG($2), A=2*M-A, M=M*pi/360.0, -0.5*cos(M)):(-0.5*sin(M)):(PER($2) > 8.00 ? sprintf('%.1f\%', PER($2)) : " ") every ::1 w labels center font ',10',\
     for [i=0:STATS_records-1] filename u (1.45):(i*0.25)-1.11:($1) every ::i::i with labels left,\
     for [i=0:STATS_records-1] '+' u (1.3):(i*0.25)-1.11:(i) pt 5 ps 4 lc palette

exit

输出:

在脚本生成的图中百分比位置不正确。

Wrong position for the percentages.

1 个答案:

答案 0 :(得分:1)

您的标签位置错误,因为您的label图以1开始,即您跳过了第一个条目。 另外,我不明白的是,为什么要沿逆时针方向绘制饼图部分,并沿顺时针方向绘制标签。

这是您脚本的工作版本,其中没有一些多余的示例。标签和饼图部分均以A = 0的角度开始绘制(请注意两个图之间的第二次初始化):

reset

# Get Status
filename = './datafile.dat'
stats filename u 2 noout

# Get Angles & Percentages
ANG(x)=x*360.0/STATS_sum
PER(x)=x*100.0/STATS_sum

# Y position of key point and label
YLBL(row) = 2.0 * (row - 0.5*(STATS_records - 1))/(STATS_records - 1)

# Square Canvas
set size square
set xrange [-1:1.5]
set yrange [-1.25:1.25]
set style fill solid 1

# Remove Base Properties (Titles, Tics, Axis, Palette)
unset key
unset tics
unset border
unset colorbox

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

# Plot
A = 0.0
plot filename u (0):(0):(1):(A):(A=A+ANG($2)):0 with circle linecolor palette,\
     A = 0,\
     filename u (M=A+ANG($2), A=2*M-A, M=M*pi/360.0, 0.5*cos(M)):(0.5*sin(M)):(PER($2) > 8.0 ? sprintf('%.1f\%', PER($2)) : "" ) w labels center,\
     filename u (1.3):(YLBL($0)):1 with labels offset char 3 left ,\
     filename u (1.3):(YLBL($0)):0 pt 5 ps 4 lc palette

该脚本还包含其他一些改进:

  • 您不需要遍历STATS_records
  • 键的文本和要点绘制在同一位置,使用offset参数将标签移动三个字符单位(offset char 3)。这使微调变得容易。

enter image description here