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
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4