← Lab 7

1 · Finding the suspects

Lecture 7 part 1 again, with you typing. Which genes does skin cancer need? The obvious answer is wrong, and finding out why is the point of the notebook.

pandas you already know
df.shape · df.head()
how big the table is; the first five rows
df["oncotree_lineage"].value_counts()
how many cell lines per cancer type
df[df["oncotree_lineage"] == "Skin"]
keep the skin rows
skin.mean(numeric_only=True)
the mean gene effect of every gene, in skin
means.nsmallest(10)
the ten genes skin needs most
(skin_mean - rest_mean)
needed in skin compared with everywhere else
In the notebook
  • ▸what one row is, and what a gene effect of −1 means
  • ▸the ten genes skin needs most — then the same for lung, and for bowel
  • ▸the trap: RAN tops every list, so it tells you nothing
  • ▸the fix: skin minus the rest, and SOX10 appearing near the top
  • ▸your turn: run the same comparison for a lineage of your choice

About 40 minutes. No plots in this notebook — only tables.

2 · Showing it

You have a gene and a number. Now make the figure that convinces someone else, starting from the one line every matplotlib figure begins with.

matplotlib, from scratch
fig, ax = plt.subplots()
a sheet of paper (fig) and one panel on it (ax)
ax.hist(df["SOX10"], bins=40)
the spread of one column
ax.scatter(x, y, s=6, alpha=0.4)
two columns against each other
ax.scatter(...) again
a second group on the SAME panel, drawn on top
ax.set_xlabel · set_ylabel · legend
the labels, and the key
fig.savefig("f.png", dpi=300)
a file you can put in a report
In the notebook
  • ▸the anatomy of a figure: fig, ax, and who owns the title
  • ▸a histogram of SOX10 across every line — and the two humps in it
  • ▸BRAF against SOX10: grey for everything, colour for skin
  • ▸labels, a legend, and why label= goes on the scatter and not the legend
  • ▸the box plot with every cell line as a dot on top
  • ▸saving at 300 dpi, with bbox_inches="tight"

About 45 minutes. The last figure is the one the project asks for.

3 · A grid, and the loose end

Four panels instead of one, a function so you write the plotting code once — and then the question the box plot leaves hanging.

More than one panel
fig, axes = plt.subplots(2, 2)
four panels; axes is a 2-D array
axes[0][1]
row 0, column 1 — counting from 0
for ax in axes.flat:
visit all four without nested loops
plt.subplots(2, 2, sharey=True)
one y-axis scale, so panels compare
fig.suptitle(...) · fig.tight_layout()
a title for the sheet; stop labels overlapping
def plot_gene(ax, gene):
lab 4 again: write it once, use it four times
In the notebook
  • ▸four genes, four panels, one loop
  • ▸why sharey changes what the reader concludes
  • ▸turning the plotting code into a function that takes an ax
  • ▸the loose end: about 18 of 73 skin lines barely need SOX10 at all
  • ▸find them, look at them, and read about de-differentiation

About 45 minutes. The last section is open-ended on purpose.

Learn more, free

Three notebooks is enough matplotlib to make every figure in your project. If you want it to become second nature, this is the course to do next.

What you can already do
  • ✓One panel, two panels, a grid — and who owns the title
  • ✓Adding a second group to a panel you already drew
  • ✓Histograms, scatters, box plots with the points on top
  • ✓Saving a figure at a resolution a journal will accept
What the course adds
  • ▸Styling: colour maps, markers, and the house style of a journal
  • ▸Time series, error bars, and twin axes
  • ▸Writing a plotting function you reuse across a whole project
  • ▸Then seaborn, next lecture, which does much of this in one line