Visualizing Tabular Data
Overview
Teaching: 30 min
Exercises: 20 minQuestions
How can I visualize tabular data in Python?
How can I group several plots together?
Objectives
Plot simple graphs from data.
Plot multiple graphs in a single figure.
Visualizing data
- visualisations can help us develop insights
- this is a massive topic, but we will look at a few features of
matplotlib
- de facto standard
- start by importing:
- create a heat map of our data
import matplotlib.pyplot
image = matplotlib.pyplot.imshow(data)
matplotlib.pyplot.show()
- row = patient in the dataset
- column = day
- blue pixels = low values
- yellow pixels = high values
- we can see flare-ups -> values rise and fall over 40 day period
Now let’s take a look at the average inflammation over time:
ave_inflammation = numpy.mean(data, axis=0)
ave_plot = matplotlib.pyplot.plot(ave_inflammation)
matplotlib.pyplot.show()
- average inflammation per day across all patients
- looks reasonable
- two more stats: max and min
max_plot = matplotlib.pyplot.plot(numpy.max(data, axis=0))
matplotlib.pyplot.show()
min_plot = matplotlib.pyplot.plot(numpy.min(data, axis=0))
matplotlib.pyplot.show()
- max is linear, min is a step function
- seems fishy!
- would have been hard to tell from just looking at the values
Grouping plots
- group similar plots using subplots
matplotlib.pyplot.figure()
creates a space into which we will place all of our plotsfigsize
defines the size of the figure (in inches)- add subplot with
add_subplot
- params:- total rows
- total columns
- number of the current subplot, counting from top left-to-right, top-to-bottom
- Each subplot is stored in a different variable (
axes1
,axes2
,axes3
) - set titles for each axis with
set_xlabel()
command (orset_ylabel()
)
Here are our three plots side by side:
import numpy
import matplotlib.pyplot
data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))
axes1 = fig.add_subplot(1, 3, 1)
axes2 = fig.add_subplot(1, 3, 2)
axes3 = fig.add_subplot(1, 3, 3)
axes1.set_ylabel('average')
axes1.plot(numpy.mean(data, axis=0))
axes2.set_ylabel('max')
axes2.plot(numpy.max(data, axis=0))
axes3.set_ylabel('min')
axes3.plot(numpy.min(data, axis=0))
fig.tight_layout()
matplotlib.pyplot.savefig('inflammation.png')
matplotlib.pyplot.show()
- If we leave out that call to
fig.tight_layout()
, the graphs will actually be squeezed together more closely. savefig
stores the plot as a graphics file- graphics format automatically determined by file ending
- supported formats include: svg, pdf and jpeg
MAYBE EXERCISE: Moving Plots Around
Modify the program to display the three plots on top of one another instead of side by side.
Solution
import numpy import matplotlib.pyplot data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',') # change figsize (swap width and height) fig = matplotlib.pyplot.figure(figsize=(3.0, 10.0)) # change add_subplot (swap first two parameters) axes1 = fig.add_subplot(3, 1, 1) axes2 = fig.add_subplot(3, 1, 2) axes3 = fig.add_subplot(3, 1, 3) axes1.set_ylabel('average') axes1.plot(numpy.mean(data, axis=0)) axes2.set_ylabel('max') axes2.plot(numpy.max(data, axis=0)) axes3.set_ylabel('min') axes3.plot(numpy.min(data, axis=0)) fig.tight_layout() matplotlib.pyplot.show()
Importing libraries with shortcuts
In this lesson we use the
import matplotlib.pyplot
syntax to import thepyplot
module ofmatplotlib
. However, shortcuts such asimport matplotlib.pyplot as plt
are frequently used. Importingpyplot
this way means that after the initial import, rather than writingmatplotlib.pyplot.plot(...)
, you can now writeplt.plot(...)
. Another common convention is to use the shortcutimport numpy as np
when importing the NumPy library. We then can writenp.loadtxt(...)
instead ofnumpy.loadtxt(...)
, for example.Some people prefer these shortcuts as it is quicker to type and results in shorter lines of code - especially for libraries with long names! You will frequently see Python code online using a
pyplot
function withplt
, or a NumPy function withnp
, and it’s because they’ve used this shortcut. It makes no difference which approach you choose to take, but you must be consistent as if you useimport matplotlib.pyplot as plt
thenmatplotlib.pyplot.plot(...)
will not work, and you must useplt.plot(...)
instead. Because of this, when working with other people it is important you agree on how libraries are imported.
INFO: Plot Scaling
Why do all of our plots stop just short of the upper end of our graph?
Solution
Because matplotlib normally sets x and y axes limits to the min and max of our data (depending on data range)
If we want to change this, we can use the
set_ylim(min, max)
method of each ‘axes’, for example:axes3.set_ylim(0,6)
Update your plotting code to automatically set a more appropriate scale. (Hint: you can make use of the
max
andmin
methods to help.)Solution
# One method axes3.set_ylabel('min') axes3.plot(numpy.min(data, axis=0)) axes3.set_ylim(0,6)
Solution
# A more automated approach min_data = numpy.min(data, axis=0) axes3.set_ylabel('min') axes3.plot(min_data) axes3.set_ylim(numpy.min(min_data), numpy.max(min_data) * 1.1)
INFO: Drawing Straight Lines
In the center and right subplots above, we expect all lines to look like step functions because non-integer value are not realistic for the minimum and maximum values. However, you can see that the lines are not always vertical or horizontal, and in particular the step function in the subplot on the right looks slanted. Why is this?
Solution
Because matplotlib interpolates (draws a straight line) between the points. One way to do avoid this is to use the Matplotlib
drawstyle
option:import numpy import matplotlib.pyplot data = numpy.loadtxt(fname='inflammation-01.csv', delimiter=',') fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0)) axes1 = fig.add_subplot(1, 3, 1) axes2 = fig.add_subplot(1, 3, 2) axes3 = fig.add_subplot(1, 3, 3) axes1.set_ylabel('average') axes1.plot(numpy.mean(data, axis=0), drawstyle='steps-mid') axes2.set_ylabel('max') axes2.plot(numpy.max(data, axis=0), drawstyle='steps-mid') axes3.set_ylabel('min') axes3.plot(numpy.min(data, axis=0), drawstyle='steps-mid') fig.tight_layout() matplotlib.pyplot.show()
MAYBE EXERCISE: Make Your Own Plot
Create a plot showing the standard deviation (
numpy.std
) of the inflammation data for each day across all patients.Solution
std_plot = matplotlib.pyplot.plot(numpy.std(data, axis=0)) matplotlib.pyplot.show()
Key Points
Use the
pyplot
module from thematplotlib
library for creating simple visualizations.