Stage 8: Plotting distributions
Five plots for the question every experimentalist asks first: what does my data look like, and do my groups differ?
You will use two plotting libraries. matplotlib is the foundation: it draws and saves pictures. seaborn sits on top and draws statistical plots from a table in one line. Every plot in this stage follows the same three steps: draw it with seaborn, add labels with matplotlib, then show it.
This stage continues with the table df and the miss table from the previous stages. In the notebook, the first cell sets everything up for you. First, load the plotting tools:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(style="whitegrid")
data = df[df["record_type"].isin(["synonymous", "missense", "nonsense"])]Which plot answers which question?
| Question | Plot | Watch out for |
|---|---|---|
| What is the shape of one measurement? | Histogram | The number of bins changes the picture. Try 20, 40 and 80. |
| Compare the centre and spread of groups | Box plot | It hides the shape. Two humps can look like one box. |
| Compare the shapes of groups | Violin plot | Needs plenty of points per group. |
| Show every single observation | Strip plot | Dots overlap, so make them small and see-through. |
| How many of each category? | Count plot | Bars should start at zero. |
The plots
sns.histplot(data=miss, x="score", bins=40)
plt.title("Missense variants")
plt.xlabel("abundance score")
plt.ylabel("number of variants")
plt.show()sns.boxplot(data=data, x="record_type", y="score")
plt.title("Score by kind of variant")
plt.xlabel("kind of variant")
plt.ylabel("abundance score")
plt.show()sns.violinplot(data=data, x="record_type", y="score")
plt.title("Score by kind of variant")
plt.xlabel("kind of variant")
plt.ylabel("abundance score")
plt.show()sns.stripplot(data=data, x="record_type", y="score", size=2, alpha=0.4)
plt.title("Every variant is a dot")
plt.xlabel("kind of variant")
plt.ylabel("abundance score")
plt.show()sns.countplot(data=data, x="record_type")
plt.title("How many variants of each kind")
plt.xlabel("kind of variant")
plt.ylabel("number of variants")
plt.show()Saving a figure
Add one line before plt.show() to save the picture as a file. dpi=300 makes it sharp enough for print. In Colab, the file appears in the Files panel (the folder icon on the left), where you can download it.
sns.histplot(data=miss, x="score", bins=40)
plt.xlabel("abundance score")
plt.ylabel("number of variants")
plt.savefig("score_histogram.png", dpi=300, bbox_inches="tight")
plt.show()Now practise in Colab
You have read the ideas. Next, try them. The notebook repeats the code above with a few blanks for you to fill in, then ends with small exercises. It covers Stages 8 and 9, so scroll to the section with this stage's number. Run the cells in order.
Open the practice notebookYou need a free Google account. When it opens, choose File, then Save a copy in Drive, so your work is yours.
Exercises
These are also in the notebook. Try each one yourself before you open the solution.
Plot nonsense scores
nonsense variants. Give it a title and label both axes.Show a solution (try it first)
Your numbers will depend on the real table, so there is no fixed output to compare with.
nonsense = df[df["record_type"] == "nonsense"]
sns.histplot(data=nonsense, x="score", bins=20)
plt.title("Nonsense variants")
plt.xlabel("abundance score")
plt.ylabel("number of variants")
plt.show()Compare regions with a box plot
region. Label the axes.Show a solution (try it first)
Your numbers will depend on the real table, so there is no fixed output to compare with.
sns.boxplot(data=miss, x="region", y="score")
plt.xlabel("region")
plt.ylabel("abundance score")
plt.show()Check your understanding
First star
What is the median score of the nonsense variants, to 2 decimal places? It is the line in the middle of the nonsense box in the box plot.
Stuck? Nudge and solution
Keep the rows where record_type is "nonsense", take the score column, and use .median().
print(df[df["record_type"] == "nonsense"]["score"].median())Second star
The histogram of missense scores is rarely one tidy hump. How many missense variants have a score strictly below 0.5?
Stuck? Nudge and solution
miss[miss["score"] < 0.5] keeps those rows. Count them with len(...).
print(len(miss[miss["score"] < 0.5]))Special thanks to Nilay!