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
-
Red Hat Adds New Deployment Option for Enterprise Linux Platforms
Red Hat has re-imagined enterprise Linux for an AI future with Image Mode.
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.