Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify a Pie Chart created by GNUPlot

Tags:

gnuplot

Input:

I have a myfile.csv file that has the following information:

Shift,Percentage
Day Shift, 39.94
Night Shift, 60.06

GNUPlot Processing:

The myfile.csv file is fed the pie_chart_generator.gnuplot file which is:

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

and was created with this as a reference.

Current Output of Chart:

This code generates this pie chart: Output .png pie chart

Questions:

Q1: How can I assign colours to each of the sectors of the graph in RGB format? Q2: Is there a way I can place the the labels on the right hand corner? Q3: How can I place the value on the chart?

Ideal Output of Chart: enter image description here

Addendum:

I have further improved my code with the aid of the answer. The code in the answer didn't quiet work for me so I had to tweak it to:

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

Current Output of Chart from the code above :

enter image description here

Addendum Questions:

Q4: I am having massive difficulty with the labels/titles. I have tried the code in the answer I got the same result. How can I print the titles without printing over each other?

like image 389
3kstc Avatar asked Apr 22 '16 02:04

3kstc


1 Answers

The post you cited already suggested a way to place the labels and percentage values. Let me explain the steps, point by point, to achieve this goal. At the end I write the full script.

Q3: How can I place the value on the chart?

Each slice is defined within two angles (Ai,Af). The percentage values should be placed in the middle of each one, at (x,y)=(0.5*cos(mid), 0.5*sin(mid)), where mid=0.5*(Ai+Af) is the mid-point angle, and 0.5 represents half the radius of the pie-chart.

For the first entry we can set Ai=0, and the angle Af is calculated from your data as Af=ang($2), where ang(x) is defined in your script. For the second entry, we update Ai=Af and calculate again Af=ang($2), and so on.

Finally, the following line should place the percentages:

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: that the first parenthesis (mid=Ai+ang($2), Ai=2*mid-Ai, mid=mid*pi/360.0, -0.5*cos(mid)) calculates the angles Ai and mid (Af is actually not needed), and then returns the coordinate x=-0.5*cos(mid).

Caveat: both x and y axes must have the same ranges:

set xrange [-1.5:1.5]
set yrange [-1.5:1.5]

Q2: Is there a way I can place the the labels on the right hand corner?

For the colored squares, you can plot points at fixed x, and equally spaced y values with:

for [i=1:STATS_records] '+' using (1.3):(i*0.25):(i) pt 5 ps 4 linecolor palette

where STATS_records is the number of rows of your file (which you already calculated with stats 'myfile.csv'). This line uses a pseudo file '+', and the using spec has three columns (x):(y):(color), where x=1.3 fixed, y=i*0.25 for i=1,2,3,...,STATS_records, and color=i is used by linecolor palette. The points are filled squares (pt 5) with 4 pixels length (ps 4). The colors are drawn in the same order as the respective pie slices.

The labels of each color can be drawn with:

plot for [i=1:STATS_records] 'myfile.csv' u (1.45):(i*0.25):1 every ::i::i with labels center font ',10'

where the using spec has columns (x):(y):(name). The x=1.45 value is slightly greater than that used before, and y=i*0.25 must be the same as those of the colored squares. name=$1 extract the string from column 1, which is used by with labels.

Note: You can control the font and size of the labels with with labels font 'Arial,10'. You can omit the font name as font ',10'.

Q1: How can I assign colours to each of the sectors of the graph in RGB format?

In the last command, I use linecolor palette, allowing us to define the colors with

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

where I use the colors in RGB, scaled to [0,1] (yellow-like is 1 0.788 0.055; blue-like is 0.090 0.161 0.659).

Note: The pie-chart should be drawn now with:

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

This is what I obtain with your data

pie-chart

This is the full script:

#!/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: The original myfile.csv has a header (the first line Shift,Percentage) that gnuplot don't read correctly. We can ignore this line by commenting it (add a # character, e.g. # Shift,Percentage), or to place an every command starting at 1 in the plot lines:

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
like image 141
vagoberto Avatar answered Sep 26 '22 21:09

vagoberto