Search code examples
labelgnuplotpie-chart

Centering labels on a pie chart


I am following this post to make a pie chart using Gnuplot. The only problem with the approach is that I can't align my percentage labels. What am I missing here?

DATA FILE:

"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

CODE:

#!/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

OUTPUT:

The percentages positions are not correct in the figure generated by the script.

Wrong position for the percentages.


Solution

  • Your labels are at wrong positions, because your label plot starts at 1, i.e. you skip the first entry. Also, what I don't understand is, why you plot the pie parts counterclockwise, and the labels clockwise.

    Here is a working version of you script, without some parts which are superfluous for demonstration. Both labels and pie parts are plotted starting at an angle of A = 0 (note the second initialization between the two plots):

    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
    

    The script contains some other improvements:

    • You don't need to iterate over STATS_records
    • The text and point for the key are plotted at the same position, the label is shifted with the offset parameter by three character units (offset char 3). That makes fine-tuning easier.

    enter image description here