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:
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?
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 :
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?