How to modify a Pie Chart created by GNUPlot -


input:

i have myfile.csv file has following information:

shift,percentage day shift, 39.94 night shift, 60.06 

gnuplot processing:

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

#!/usr/bin/gnuplot -persist reset set title "\n" set label 1 "my pie chart\nshift usage break down" @ 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      # stats_sum (sum of column 2)  ang(x)=x*360.0/stats_sum        # angle (grades) perc(x)=x*100.0/stats_sum       # 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 = 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) circle linecolor var 

and created reference.

current output of chart:

this code generates pie chart: output .png pie chart

questions:

q1: how can assign colours each of sectors of graph in rgb format? q2: there way can place the labels on right hand corner? q3: how can place value on chart?

ideal output of chart: enter image description here

addendum:

i have further improved code aid of answer. code in answer didn't quiet work me had tweak to:

#!/usr/bin/gnuplot -persist reset  dataname = 'myfile.csv' set datafile separator ','  # stats_sum (sum of column 2) , stats_records  stats dataname u 2 noout  # define angles , percentages ang(x)=x*360.0/stats_sum        # angle (grades) perc(x)=x*100.0/stats_sum       # percentage  # set output  set terminal png set output "piechart.png" set size square  # print title of chart set title "\n" set label 1 "my pie chart\nshift usage break down" @ 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  # parameters  ai = 5.0;                     # initial angle mid = 0.0;                    # mid angle  # defines colors yellow~ffc90e, , 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 [0,1])   plot [i=1:stats_records] dataname u (0):(0):(1):(ai):(ai=ai+ang($2)):(i) every ::2 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',\       [i=1:stats_records]dataname u (1.45):(i*0.25):1 every ::1 labels left,\       [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 color labels # fourth line places color symbols unset output 

current output of chart from code above :

enter image description here

addendum questions:

q4: having massive difficulty labels/titles. have tried code in answer got same result. how can print titles without printing on each other?

the post cited suggested way place labels , percentage values. let me explain steps, point point, achieve goal. @ end write full script.

q3: how can place value on chart?

each slice defined within 2 angles (ai,af). percentage values should placed in middle of each one, @ (x,y)=(0.5*cos(mid), 0.5*sin(mid)), mid=0.5*(ai+af) mid-point angle, , 0.5 represents half radius of pie-chart.

for first entry can set ai=0, , angle af calculated data af=ang($2), ang(x) defined in script. second entry, update ai=af , calculate again af=ang($2), , on.

finally, following line should place 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: first parenthesis (mid=ai+ang($2), ai=2*mid-ai, mid=mid*pi/360.0, -0.5*cos(mid)) calculates angles ai , mid (af not needed), , returns coordinate x=-0.5*cos(mid).

caveat: both x , y axes must have same ranges:

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

q2: there way can place the labels on right hand corner?

for colored squares, can plot points @ fixed x, , 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 number of rows of file (which calculated stats 'myfile.csv'). line uses pseudo file '+', , using spec has 3 columns (x):(y):(color), x=1.3 fixed, y=i*0.25 i=1,2,3,...,stats_records, , color=i used linecolor palette. points filled squares (pt 5) 4 pixels length (ps 4). colors drawn in same order respective pie slices.

the labels of each color can drawn with:

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

where using spec has columns (x):(y):(name). x=1.45 value greater used before, , y=i*0.25 must same of colored squares. name=$1 extract string column 1, used with labels.

note: can control font , size of labels with labels font 'arial,10'. can omit font name font ',10'.

q1: how can assign colours each of sectors of graph in rgb format?

in last command, use linecolor palette, allowing define colors with

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

where use colors in rgb, scaled [0,1] (yellow-like 1 0.788 0.055; blue-like 0.090 0.161 0.659).

note: pie-chart should drawn with:

plot [i=1:stats_records] dataname u (0):(0):(1):(ai):(ai=ai+ang($2)):(i) every ::i-1::i-1 circle linecolor palette 

this obtain data

pie-chart

this full script:

#!/usr/bin/gnuplot -persist reset  dataname = 'myfile.csv' set datafile separator ','  # stats_sum (sum of column 2) , stats_records  stats dataname u 2 noout      #define angles , percentages ang(x)=x*360.0/stats_sum        # angle (grades) perc(x)=x*100.0/stats_sum       # 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" @ 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  # parameters  ai = 15.0;                # init angle mid = 0.0;                # mid angle  # defines colors yellow~ffc90e, , 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 [0,1])   plot [i=1:stats_records] dataname u (0):(0):(1):(ai):(ai=ai+ang($2)):(i) every ::i::i 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',\      [i=1:stats_records] dataname u (1.45):(i*0.25):1 every ::i::i labels left,\      [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 color labels # fourth line places color symbols unset output 

update: original myfile.csv has header (the first line shift,percentage) gnuplot don't read correctly. can ignore line commenting (add # character, e.g. # shift,percentage), or place every command starting @ 1 in plot lines:

plot [i=1:stats_records] dataname u (0):(0):(1):(ai):(ai=ai+ang($2)):(i) every ::i::i 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',\      [i=1:stats_records] dataname u (1.45):(i*0.25):1 every ::i::i labels left,\      [i=1:stats_records] '+' u (1.3):(i*0.25):(i) pt 5 ps 4 lc palette 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -