Skip to content

Instantly share code, notes, and snippets.

@jmarhee
Created April 9, 2023 05:44
Show Gist options
  • Save jmarhee/56ad7e011434949a307e5850d0b22564 to your computer and use it in GitHub Desktop.
Save jmarhee/56ad7e011434949a307e5850d0b22564 to your computer and use it in GitHub Desktop.
Generate an epidemic curve with gnuplot from a CSV of data.

Epidemic Curve Graph Bash Script

This is a Bash script that generates an epidemic curve graph using the gnuplot tool. The script takes in a data file in CSV format and outputs a PNG image of the epidemic curve.

Prerequisites

In order to use this script, you will need to have gnuplot installed on your system. If you're using a Debian-based distribution, you can install gnuplot using the following command:

sudo apt-get install gnuplot

Usage

Clone or download the repository to your local machine. Place your data file in CSV format in the same directory as the script. Edit the script to set the datafile variable to the name of your data file and the output variable to the desired name of your output image file.

Run the script using the following command:

./epidemic_curve.sh

The output image file will be saved in the same directory as the script.

Data Format

The data file should be in CSV format with two columns: date and cases. The date should be in YYYY-MM-DD format and the cases should be a numeric value. The first line of the file should be a header and will be ignored by the script.

Customization

You can customize the appearance of the graph by modifying the settings in the epidemic_curve.plot file. You can change the title, axis labels, data style, and more. Consult the gnuplot documentation for more information on customizing graphs.

License

This script is released under the MIT License. Feel free to modify and distribute the script as needed.

#!/bin/bash
# set data file path and output image path
datafile="./data.csv"
output="./epidemic_curve.png"
# preprocess data to remove header and convert date format
tail -n +2 "$datafile" | awk -F ',' '{print $1, $2}' | sed 's/\//-/g' > ./processed_data.txt
# create gnuplot script file
cat > epidemic_curve.plot <<EOF
set terminal pngcairo enhanced font 'Verdana,12' size 800,600
set output "$output"
set title "Epidemic Curve"
set xlabel "Date"
set ylabel "Cases"
set xdata time
set timefmt "%Y-%m-%d"
set format x "%m/%d"
set style data lines
plot "./processed_data.txt" using 1:2 title "Cases"
EOF
# use gnuplot to generate graph
gnuplot epidemic_curve.plot
# remove temporary files
rm ./processed_data.txt
rm ./epidemic_curve.plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment