Graphing the pandemic with open data
Plotting the Data
Tabular data is actually very dense and conveys a lot of information, however, it does have the side effect of being rather dry, and when the volume of data is too great, it can be difficult to spot trends. I remembered the graphing tool gnuplot [6], which I have used in the past to give data a friendlier look (Figure 1).
Gnuplot is a cross-platform 2D and 3D graphing tool. You can use gnuplot to create line graphs, bar charts, histograms, or even candlestick charts.
Gnuplot is a command-line program that can accept its input via a pipe and send its output to standard output. The output from gnuplot can be redirected to a file, but it is also possible to define where the output should be written.
Gnuplot is available in the package repositories of many popular Linux distributions:
sudo apt-get install gnuplot
Despite the name, gnuplot is not affiliated with the GNU project, and, although it is free to use and redistribute, it has an unusual license. Because of this license, it is not possible to redistribute modified versions of the source code: "Modifications are to be distributed as patches to the released version." It is still possible to release your own modified binaries of gnuplot, well, with a few conditions that are covered in the copyright [7] statement.
Gnuplot makes it possible to save your graphed output in quite a few different formats. You can save the output in all the common graphic file formats – PNG, GIF, JPEG, and SVG, but also other unusual types of output such as as a Postscript, PDF, or LaTeX file.
When you start gnuplot as a command interpreter, it creates a graphical window where your graphed data will be displayed. Thus you can interactively test out some plotting options (Figure 2).
One of the additional advantages to running gnuplot as an interpreter is that, once you are satisfied with the results, you can save the plot datafile. Conversely you can also load a datafile into the interpreter.
load "plotcommands.ext" save "plotcommands.ext"
The actual script for generating graphs from the collected data is quite short (Listing 3). This script actually demonstrates how powerful gnuplot is. The main steps for drawing any graph are:
- defining the units on the X and Y axis
- labeling the axis
- plotting the data
These steps are all depicted in Listing 3.
Listing 3
graphs.gp
01 # Reset all plotting variables to their default values. 02 reset 03 clear 04 05 # set the terminal type (ie output format) 06 # also set the width and height 07 set term png size 1000, 800 08 09 # set x & y axis description 10 set xlabel font ",20" "Time axis" 11 set ylabel font ",20" "Confirmed" 12 13 # setup x and y axis values 14 set xdata time 15 set timefmt "%Y-%m-%d" 16 set xrange [ "2020-01-22":* ] 17 set format x "%Y-%m-%d" 18 set yrange [ 0:* ] 19 20 # graph of usa graph 21 set output 'country.png' 22 set title font ",30" "Covid 19 \nUS Cumulative cases" 23 plot "covid19_USA.data" using 1:2 title 'US confirmed' with boxes, \ 24 "" using 1:3 title 'US deaths' with boxes, \ 25 "" using 1:4 title 'US recovered' with boxes 26 27 28 # graph of minnesota statistics 29 set output 'statemn.png' 30 set title font ",30" "Covid 19 \nMN Cumulative cases" 31 plot "covid19_mn.Data" using 1:2 title 'positive' , \ 32 "" using 1:3 title 'hospitalized' , \ 33 "" using 1:4 title 'deaths' 34 35 36 # set legend below the graph 37 set key below font ",15" 38 39 # compare a few states against each other 40 set output 'statecompare.png' 41 set title font ",30" "Covid 19 \nPositive Tests" 42 plot "covid19_mn.Data" using 1:2 title 'Minnesota' , \ 43 "covid19_ca.Data" using 1:2 title 'California' , \ 44 "covid19_ia.Data" using 1:2 title 'Iowa' , \ 45 "covid19_mo.Data" using 1:2 title 'Missouri' , \ 46 "covid19_mt.Data" using 1:2 title 'Montana' 47 48 49 # compare USA against other countries 50 set output 'confirm.png' 51 set title font ",30" "Covid 19 \nInternational confirmed cases" 52 plot "covid19_USA.data" using 1:2 title 'USA' , \ 53 "covid19_DEU.data" using 1:2 title 'DEU' , \ 54 "covid19_ESP.data" using 1:2 title 'ESP' , \ 55 "covid19_GBR.data" using 1:2 title 'GBP'
The plot statement in Listing 3 is a bit confusing until you recognize that each set of data can come from a different file, and using 1:2
means that column 1 from the data file will be on the X axis and column 2 will be on the Y axis.
The comparative graph of the individual state infections, Figure 3, is much more helpful than viewing all the US figures in tabular form.
Conclusion
The scripts described in this article are available at the Linux Magazine website [8]. I could have gone even further and collected information from the Twitter accounts of state governors and health departments [9], but I don't think important health information can be summarized into 288 characters. Besides, I am not the biggest follower on Twitter.
Infos
- CovidAPI Project: https://covidapi.info/
- Johns Hopkins Coronavirus Resource Center: https://coronavirus.jhu.edu/
- CovidAPI Documentation: https://documenter.getpostman.com/view/2568274/SzS8rjbe?version=latest#intro
- ISO country codes: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes
- Covid Tracking Project: https://covidtracking.com/
- gnuplot: http://www.gnuplot.info/
- gnuplot copyright: https://sourceforge.net/p/gnuplot/gnuplot-main/ci/master/tree/Copyright
- Scripts used in this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/240/
- Twitter account information: https://covid-19-apis.postman.com/
« Previous 1 2
Buy Linux Magazine
Direct Download
Read full article as PDF:
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
OpenMandriva Lx 23.03 Rolling Release is Now Available
OpenMandriva "ROME" is the latest point update for the rolling release Linux distribution and offers the latest updates for a number of important applications and tools.
-
CarbonOS: A New Linux Distro with a Focus on User Experience
CarbonOS is a brand new, built-from-scratch Linux distribution that uses the Gnome desktop and has a special feature that makes it appealing to all types of users.
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.