Plotting Basics with MatPlotlib and Seaborn - Answer Key

Authors

Noor Sohail

Will Gammerdinger

Published

March 15, 2026

Exercise 1

  1. The current axis label text defaults to what we gave as input to geom_point (i.e the column headers). We can change this by adding additional layers called xlabel() and ylabel() for the x- and y-axis, respectively. Add these layers to the current plot such that the x-axis is labeled “Age (days)” and the y-axis is labeled “Mean expression”.
# Set the theme to "whitegrid"
sns.set_style("whitegrid")

# Initialize a plot with a specific size
plt.figure(figsize=(8, 6))

# Add a scatterplot layer to the plot, coloring points by genotype
sns.scatterplot(data = new_metadata,
                x="age_in_days", 
                y="mean_expression",
                hue="genotype",
                style="celltype",
                s=50)

# Change the size of the axis labels
plt.xlabel("Age (Days)", fontsize=20)
plt.ylabel("Mean expression", fontsize=20)

  1. Use the plt.title() layer to add a plot title of your choice.
# Set the theme to "whitegrid"
sns.set_style("whitegrid")

# Initialize a plot with a specific size
plt.figure(figsize=(8, 6))

# Add a scatterplot layer to the plot, coloring points by genotype
sns.scatterplot(data = new_metadata,
                x="age_in_days", 
                y="mean_expression",
                hue="genotype",
                style="celltype",
                s=50)

# Change the size of the axis labels
plt.xlabel("Age (Days)", fontsize=20)
plt.ylabel("Mean expression", fontsize=20)
plt.title("Mean expression vs Age (Days)")

  1. When you add the arguments loc="center" to the plt.title() function:
    • What does it change?
    • How many layers can be added to a plot, in your estimation?
# Set the theme to "whitegrid"
sns.set_style("whitegrid")

# Initialize a plot with a specific size
plt.figure(figsize=(8, 6))

# Add a scatterplot layer to the plot, coloring points by genotype
sns.scatterplot(data = new_metadata,
                x="age_in_days", 
                y="mean_expression",
                hue="genotype",
                style="celltype",
                s=50)

# Change the size of the axis labels
plt.xlabel("Age (Days)", fontsize=20)
plt.ylabel("Mean expression", fontsize=20)
plt.title("Mean expression vs Age (Days)", loc="center")

  1. Try adding the layer plt.legend(loc="center right") to the end of your code. What does this do? How many layers can be added to a plot, in your estimation?
# Set the theme to "whitegrid"
sns.set_style("whitegrid")

# Initialize a plot with a specific size
plt.figure(figsize=(8, 6))

# Add a scatterplot layer to the plot, coloring points by genotype
sns.scatterplot(data = new_metadata,
                x="age_in_days", 
                y="mean_expression",
                hue="genotype",
                style="celltype",
                s=50)

# Change the size of the axis labels
plt.xlabel("Age (Days)", fontsize=20)
plt.ylabel("Mean expression", fontsize=20)
plt.title("Mean expression vs Age (Days)", loc="center")

# Change legend location
plt.legend(loc="center right")

Reuse

CC-BY-4.0