【问题标题】:Centering labels on a pie chart在饼图上居中标签
【发布时间】:2018-12-04 13:18:42
【问题描述】:

我正在关注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

输出:

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

【问题讨论】:

    标签: label gnuplot pie-chart


    【解决方案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)。这使得微调更容易。

    【讨论】:

    • 代码现在干净多了。非常感谢您的改进和仔细的解释。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多