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
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more and more Linux systems are getting targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.