Atharva Bhagwat

Stage 8: Plotting distributions

0Plan
Stage 8Core
★★

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:

run this first
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?

QuestionPlotWatch out for
What is the shape of one measurement?HistogramThe number of bins changes the picture. Try 20, 40 and 80.
Compare the centre and spread of groupsBox plotIt hides the shape. Two humps can look like one box.
Compare the shapes of groupsViolin plotNeeds plenty of points per group.
Show every single observationStrip plotDots overlap, so make them small and see-through.
How many of each category?Count plotBars should start at zero.

The plots

1. histogram
sns.histplot(data=miss, x="score", bins=40)
plt.title("Missense variants")
plt.xlabel("abundance score")
plt.ylabel("number of variants")
plt.show()
2. box plot
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()
3. violin plot
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()
4. strip plot
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()
5. count plot
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.

run this
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

Draw a histogram of the scores of the 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.

one solution
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

Draw a box plot of missense scores for each 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.

one solution
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().

one way to get it
print(df[df["record_type"] == "nonsense"]["score"].median())

Special thanks to Nilay!