Plotting Basics with MatPlotlib and Seaborn - Answer Key

Authors

Noor Sohail

Will Gammerdinger

Published

March 15, 2026

Exercise 1

  1. Add a plt.ylabel() layer to the current plot such that the y-axis is labeled “Mean expression”.
# Set the theme to "whitegrid"
sns.set_style(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 and text of the axis label
plt.xlabel(xlabel = "Age in Days",
           fontsize=20)

# Add an plt.ylabel() layers to the current plot such that the y-axis is labeled “Mean expression”.
plt.ylabel(ylabel = "Mean expression",
           fontsize=20)
           
# Render the plot
plt.show()

  1. Use the plt.title() layer to add a plot title of your choice.
# Set the theme to "whitegrid"
sns.set_style(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 and text of the axis label
plt.xlabel(xlabel = "Age in Days",
           fontsize=20)

# Add an plt.ylabel() layers to the current plot such that the y-axis is labeled “Mean expression”.
plt.ylabel(ylabel = "Mean expression",
           fontsize=20)

# Use the plt.title() layer to add a plot title of your choice.
plt.title(label = "Mean expression by age")

# Render the plot
plt.show()

  1. When you add the arguments loc="right" to the plt.title() function, what does it change?
# Set the theme to "whitegrid"
sns.set_style(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 and text of the axis label
plt.xlabel(xlabel = "Age in Days",
           fontsize=20)

# Add an plt.ylabel() layers to the current plot such that the y-axis is labeled “Mean expression”.
plt.ylabel(ylabel = "Mean expression",
           fontsize=20)

# Use the plt.title() layer to add a plot title of your choice.
# When you add the arguments loc="right" to the plt.title() function, what does it change?
plt.title(label = "Mean expression by age",
          loc="right")

# Render the plot
plt.show()

The title of the plot is now right-aligned.

  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(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 and text of the axis label
plt.xlabel(xlabel = "Age in Days",
           fontsize=20)

# Add an plt.ylabel() layers to the current plot such that the y-axis is labeled “Mean expression”.
plt.ylabel(ylabel = "Mean expression",
           fontsize=20)

# Use the plt.title() layer to add a plot title of your choice.
plt.title(label = "Mean expression by age")

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

# Render the plot
plt.show()

plt.legend changes the location of the legend in the plot. We can add as many layers as we would like.

Reuse

CC-BY-4.0