Visualization
Copy the code from the exercise below and try out the basics of visualization with matplotlib
.
Create new cells with # %%
as necessary.
Basic Visualization
Section titled “Basic Visualization”# %%# 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
# %%# Import matplotlibimport matplotlib.pyplot as pltimport polars as plfrom matplotlib.axes import Axes
# %%# Read the datapatients = pl.read_csv("../../../data/hosp/patients.csv.gz")
# %%# Create a figure with four axesfig, axs = plt.subplots(2, 2, constrained_layout=True)axs: list[list[Axes]]
# %%# For all plots, set a title and x and y labels
# %%# Make a histogram of ageaxs[0][0].set_title("Histogram")axs[0][0].hist(patients["anchor_age"])axs[0][0].set_xlabel("Age in years")axs[0][0].set_ylabel("Number of subjects")fig
# %%# Make a bar plot for number of patients stratified by genderaxs[0][1].set_title("Bar")axs[0][1].bar(*patients["gender"].value_counts())axs[0][1].set_xlabel("Sex")axs[0][1].set_ylabel("n")fig
# %%# Make a boxplot of age stratified by genderstrat_by_age = patients.group_by("gender").agg("anchor_age")axs[1][0].set_title("Box")axs[1][0].boxplot( strat_by_age["anchor_age"], tick_labels=strat_by_age["gender"].to_list())axs[1][0].set_xlabel("Sex")axs[1][0].set_ylabel("Age")fig
# %%# Make a scatter plot of age against yearaxs[1][1].set_title("Scatter")axs[1][1].scatter(patients["anchor_age"], patients["anchor_year"])axs[1][1].set_xlabel("Age")axs[1][1].set_ylabel("Year")fig
# %%# Save the figurefig.savefig("../../../assets/img/exercises/visualization.svg")
Different Types of Plots
Section titled “Different Types of Plots”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 Plot
Section titled “Forest 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
# %%# Import packagesimport matplotlib.pyplot as pltimport polars as pl
# %%# Create example data or copy from the solutiontable = pl.DataFrame( { "label": [f"Category {i + 1}" for i in range(5)], "estimate": [1.5, 2.3, 2.8, 3.3, 3.2], "ci_lower": [0.5, 1.2, 2.6, 1.9, 2.0], "ci_upper": [2.5, 3.5, 3.0, 4.7, 4.4], }).sort("estimate")
# %%# Initialize figurefig, ax = plt.subplots(figsize=(3.5, 2.2), constrained_layout=True)
# %%# Draw a dashed line at x = 1ax.axvline(x=1, color="#9ca3af", linestyle="--")fig
# %%# Create the forest plot using an error bar plot typeax.errorbar( table["estimate"], table["label"], xerr=[ table["estimate"] - table["ci_lower"], table["ci_upper"] - table["estimate"], ], fmt="D", capsize=5, color="#1f2937",)fig
# %%# Add a grid behind the barsax.grid(color="#f3f4f6", which="major", clip_on=False)ax.set_axisbelow(True)fig
# %%# Set the y limits to give some space to the barsax.set_ylim(-1, table.height)fig
# %%ax.set_xlabel("Estimate")
# %%# Add text indicating the direction of the effectax.text( 0.02, 1.05, r"$\longleftarrow$ Low effect", size=10, va="baseline", ha="left", color="#6b7280", fontname="Arial", transform=ax.transAxes,)ax.text( 0.98, 1.05, r"High effect $\longrightarrow$", size=10, va="baseline", ha="right", color="#6b7280", fontname="Arial", transform=ax.transAxes,)fig
# %%# Save the figurefig.savefig("../../../assets/img/exercises/forest-plot.svg")
Anatomy of a Figure
Section titled “Anatomy of a 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.
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 packagesimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib.ticker import AutoMinorLocator, MultipleLocator
royal_blue = [0, 20 / 256, 82 / 256]
# %%# Create datanp.random.seed(19680801)
X = np.linspace(0.5, 3.5, 100)Y1 = 3 + np.cos(X)Y2 = 1 + np.cos(1 + X / 0.75) / 2Y3 = np.random.uniform(Y1, Y2, len(X))
# %%# Initialize figurefig = 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
# %%# 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 packagesimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib.ticker import AutoMinorLocator, MultipleLocator
royal_blue = [0, 20 / 256, 82 / 256]
# %%# Create datanp.random.seed(19680801)
X = np.linspace(0.5, 3.5, 100)Y1 = 3 + np.cos(X)Y2 = 1 + np.cos(1 + X / 0.75) / 2Y3 = np.random.uniform(Y1, Y2, len(X))
# %%# Initialize figurefig = plt.figure(figsize=(7.5, 7.5))ax = fig.add_axes((0.2, 0.17, 0.68, 0.7), aspect=1)
# %%# Set the major and minor locator for the x axisax.xaxis.set_major_locator(MultipleLocator(1.000))ax.xaxis.set_minor_locator(AutoMinorLocator(4))fig
# %%# Set the major and minor locator and the minor formatter for the x axisax.yaxis.set_major_locator(MultipleLocator(1.000))ax.yaxis.set_minor_locator(AutoMinorLocator(4))ax.xaxis.set_minor_formatter("{x:.2f}")fig
# %%# Set the limits for the x and y axesax.set_xlim(0, 4)ax.set_ylim(0, 4)fig
# %%# Set the formatting for the major and minor axesax.tick_params(which="major", width=1.0, length=10, labelsize=14)ax.tick_params(which="minor", width=1.0, length=5, labelsize=10, labelcolor="0.25")fig
# %%# Add a grid with dashed linesax.grid(linestyle="--", linewidth=0.5, color=".25", zorder=-10)fig
# %%# Plot a line of X and Y1ax.plot(X, Y1, c="C0", lw=2.5, label="Blue signal", zorder=10)fig
# %%# Plot a line of X and Y2ax.plot(X, Y2, c="C1", lw=2.5, label="Orange signal")fig
# %%# Plot X and Y3 (only every third point for clarity)ax.plot( X[::3], Y3[::3], linewidth=0, markersize=9, marker="s", markerfacecolor="none", markeredgecolor="C4", markeredgewidth=2.5,)fig
# %%# Set the titleax.set_title("Anatomy of a figure", fontsize=20, verticalalignment="bottom")fig
# %%# Set the x and y axis labelsax.set_xlabel("x Axis label", fontsize=14)ax.set_ylabel("y Axis label", fontsize=14)fig
# %%# Add a legendax.legend(loc="upper right", fontsize=14)fig
# %%# Save the figurefig.savefig("../../../assets/img/exercises/anatomy-of-a-figure.svg")
Release Your Inner Artist 🎨
Section titled “Release Your Inner Artist 🎨”-
Pick a plot type from the
Matplotlib
plot gallery that you like -
Look through the practice data to find one more variables that you can use for your plot (or make up your own data)
-
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.
A Publication-Ready Figure
Section titled “A Publication-Ready Figure”-
Look through the practice data to find one more variables that you would like to visualize
-
Pick a plot type from the
Matplotlib
plot gallery that you think would visualize the data well -
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