Skip to content

Instantly share code, notes, and snippets.

@jefftrull
Created July 13, 2024 01:05
Show Gist options
  • Save jefftrull/3b7701bc193d5249baaa792f6c85523b to your computer and use it in GitHub Desktop.
Save jefftrull/3b7701bc193d5249baaa792f6c85523b to your computer and use it in GitHub Desktop.
Simple pie chart from data in gnuplot 6
# Meme-compliant gnuplot 6 pie chart demo
# Jeff Trull 2024-07-12
set terminal png
unset border
unset xtics
unset ytics
unset rtics
unset key
set theta clockwise top
# our data
$datafile << EOD
"Food" 200
"Data" 150
"Rent" 800
"Candles" 3600
"Utility" 150
EOD
# generate statistics for column 2 into global gnuplot variables
stats $datafile using 2 nooutput
# we want STATS_sum, for calculating the thickness of each slice in radians
set yrange [-1.2 : 1.2]
set polar
unset raxis
set size ratio -1 1,1
set title "Someone who is good at the economy help me budget this." font "Noto Sans,16"
set label "my family is dying" at polar pi, polar 1.2 center font "Noto Sans,16"
last_angle = 0
# for plotting the sectors (pie slices) we implement an implicit for loop in the "using" specification like this:
# for (data in $datafile):
# annular_extent = 2*pi*(column2/STATS_sum)
# i.e. the fraction of the total circle this row represents, scaled by 2pi radians
# plot sector with radial extension from r=0 (the center) to r=1, starting at last_angle,
# sweeping annular_extent radians, and using a different color each time (total 5 values supplied)
# last_angle += annular_extent
# the second "plot element" (separated by a comma) plots the labels with a similar loop but using column 1
# and suitable polar coordinates (centered on the wedge, but outside)
plot $datafile \
using (last_angle):(0):(annular_extent=(2*pi*$2/STATS_sum),last_angle=last_angle+annular_extent,annular_extent):(1):($0+1) \
with sectors units xx \
lc variable fill solid, \
last_angle = 0 \
$datafile \
using (annular_extent=(2*pi*$2/STATS_sum),last_angle+annular_extent/2):(last_angle=last_angle+annular_extent, 1.1):1 \
with labels \
font "Noto Sans, 12"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment