Skip to content

Visualization

Copy the code from the exercise below and try out the basics of visualization with matplotlib. Create new cells with # %% as necessary.


# %%
# Import matplotlib
# %%
# Read the data
# %%
# Create a figure with four axes
# %%
# For all plots, set a title and x and y labels
# %%
# Make a histogram of age
# %%
# Make a bar plot for number of patients stratified by gender
# %%
# Make a boxplot of age stratified by gender
# %%
# Make a scatter plot of age against year
# %%
# Save the figure

Look through the Matplotlib plot gallery and try some more different plot types. For example a stem plot instead of the bar plot and an error bar or violin plot instead of the box plot.

Forest plots are common when presenting epidemiological results. Make a forest plot for some made up data using the error bar plot type.

# %%
# Import packages
# %%
# Create example data or copy from the solution
# %%
# Initialize figure
# %%
# Draw a dashed line at x = 1
# %%
# Create the forest plot using an error bar plot type
# %%
# Add a grid behind the bars
# %%
# Set the y limits to give some space to the bars
# %%
# %%
# Add text indicating the direction of the effect
# %%
# Save the figure

This example illustrates most of the important parts of a figure and comes from the official documentation: https://matplotlib.org/stable/gallery/showcase/anatomy.html

Your job is to recreate the figure below with the help of the label hints in the figure and the solution code.

Anatomy of a Figure with labels

To make things a little easier copy the first part of the code that has to do with the data generation and initialization of the figure.

# %%
# Import packages
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import AutoMinorLocator, MultipleLocator
royal_blue = [0, 20 / 256, 82 / 256]
# %%
# Create data
np.random.seed(19680801)
X = np.linspace(0.5, 3.5, 100)
Y1 = 3 + np.cos(X)
Y2 = 1 + np.cos(1 + X / 0.75) / 2
Y3 = np.random.uniform(Y1, Y2, len(X))
# %%
# Initialize figure
fig = plt.figure(figsize=(7.5, 7.5))
ax = fig.add_axes((0.2, 0.17, 0.68, 0.7), aspect=1)
# ...
# %%
# This example illustrates most of the important parts of a figure and comes from the
# official documentation: https://matplotlib.org/stable/gallery/showcase/anatomy.html
# %%
# Import packages
# %%
# Create data
# %%
# Initialize figure
# %%
# Set the major and minor locator for the x axis
# %%
# Set the major and minor locator and the minor formatter for the x axis
# %%
# Set the limits for the x and y axes
# %%
# Set the formatting for the major and minor axes
# %%
# Add a grid with dashed lines
# %%
# Plot a line of X and Y1
# %%
# Plot a line of X and Y2
# %%
# Plot X and Y3 (only every third point for clarity)
# %%
# Set the title
# %%
# Set the x and y axis labels
# %%
# Add a legend
# %%
# Save the figure
  1. Pick a plot type from the Matplotlib plot gallery that you like

  2. Look through the practice data to find one more variables that you can use for your plot (or make up your own data)

  3. Make the most ridiculous, colourful, interesting, or otherwise fantastical figure you can come up with. This is about having fun and changing as many things about the figure as possible to learn what is possible, so no need to make it “professional” looking.

  1. Look through the practice data to find one more variables that you would like to visualize

  2. Pick a plot type from the Matplotlib plot gallery that you think would visualize the data well

  3. Now that we have had our fun in the previous exercise, do your best to make a nice-looking figure that you could submit to a paper