From 98da876381a6e8d7b6217f32509fb8c7efabd108 Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 20 Nov 2025 09:40:06 +0100 Subject: [PATCH 1/5] :bug: add dependency for static exports of images on colab --- 2_plotly.ipynb | 14 ++++++++++++-- 2_plotly.py | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/2_plotly.ipynb b/2_plotly.ipynb index c2322c5..6947421 100644 --- a/2_plotly.ipynb +++ b/2_plotly.ipynb @@ -15,10 +15,20 @@ "cell_type": "code", "execution_count": null, "id": "845ce912", - "metadata": {}, + "metadata": { + "tags": [ + "hide-output" + ] + }, "outputs": [], "source": [ - "import plotly.express as px" + "import os\n", + "import plotly.express as px\n", + "\n", + "IN_COLAB = \"COLAB_GPU\" in os.environ\n", + "\n", + "if IN_COLAB:\n", + " !pip install kaleido" ] }, { diff --git a/2_plotly.py b/2_plotly.py index d4e86b3..46cd814 100644 --- a/2_plotly.py +++ b/2_plotly.py @@ -5,8 +5,14 @@ # - [website](https://plotly.com/python/plotly-express/) # %% +import os import plotly.express as px +IN_COLAB = "COLAB_GPU" in os.environ + +if IN_COLAB: + # !pip install kaleido + # %% [markdown] # ## Line Plot # - A simple line plot using Plotly Express. From 1b46ec00de37ecae8e28a40d197ca5e13d2a80ec Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 20 Nov 2025 09:41:23 +0100 Subject: [PATCH 2/5] :bug: remove hard coded in colab variable --- 1_3_seaborn.ipynb | 9 --------- 1_3_seaborn.py | 3 --- 2 files changed, 12 deletions(-) diff --git a/1_3_seaborn.ipynb b/1_3_seaborn.ipynb index 19e6038..1b91b6e 100644 --- a/1_3_seaborn.ipynb +++ b/1_3_seaborn.ipynb @@ -75,15 +75,6 @@ "IN_COLAB = \"COLAB_GPU\" in os.environ" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "IN_COLAB = True" - ] - }, { "cell_type": "code", "execution_count": null, diff --git a/1_3_seaborn.py b/1_3_seaborn.py index 2691b61..6837dcf 100644 --- a/1_3_seaborn.py +++ b/1_3_seaborn.py @@ -56,9 +56,6 @@ IN_COLAB = "COLAB_GPU" in os.environ -# %% -IN_COLAB = True - # %% DATA_DIR = Path("data") From bb448bd8b0cbf6d81879deba97389e65ee1c74ab Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 20 Nov 2025 09:42:20 +0100 Subject: [PATCH 3/5] :memo: hint on color palette picking using C0....C9 --- 1_1_matplotlib.ipynb | 42 ++++++++++++++++++++++++-------------- 1_1_matplotlib.py | 48 +++++++++++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/1_1_matplotlib.ipynb b/1_1_matplotlib.ipynb index 2e4b7c4..352e3db 100644 --- a/1_1_matplotlib.ipynb +++ b/1_1_matplotlib.ipynb @@ -26,8 +26,8 @@ "import numpy as np\n", "import pandas as pd\n", "\n", - "mpl.rcParams['pdf.fonttype'] = 42\n", - "mpl.rcParams['ps.fonttype'] = 42" + "mpl.rcParams[\"pdf.fonttype\"] = 42\n", + "mpl.rcParams[\"ps.fonttype\"] = 42" ] }, { @@ -105,7 +105,13 @@ "x = mu + sigma * np.random.randn(10000)\n", "fig, ax = plt.subplots(figsize=(5, 2.7), layout=\"constrained\")\n", "# the histogram of the data\n", - "n, bins, patches = ax.hist(x, 50, density=True, facecolor=\"C0\", alpha=0.75)\n", + "n, bins, patches = ax.hist(\n", + " x,\n", + " 50,\n", + " density=True,\n", + " facecolor=\"C0\", # first color in color palette\n", + " alpha=0.75,\n", + ")\n", "\n", "ax.set_xlabel(\"Length [cm]\")\n", "ax.set_ylabel(\"Probability\")\n", @@ -141,7 +147,7 @@ "Y2 = 1 + np.cos(1 + X / 0.75) / 2\n", "Y3 = np.random.uniform(Y1, Y2, len(X))\n", "\n", - "data = {'X': X, 'red_line': Y1, 'blue_line': Y2, 'circles': Y3}\n", + "data = {\"X\": X, \"red_line\": Y1, \"blue_line\": Y2, \"circles\": Y3}\n", "data = pd.DataFrame(data)\n", "data.head()" ] @@ -175,19 +181,25 @@ "ax.set_xlim(0, 4)\n", "ax.set_ylim(0, 4)\n", "\n", - "ax.tick_params(which='major', width=1.0, length=10, labelsize=14)\n", - "ax.tick_params(which='minor', width=1.0, length=5, labelsize=10,\n", - " labelcolor='0.25')\n", + "ax.tick_params(which=\"major\", width=1.0, length=10, labelsize=14)\n", + "ax.tick_params(which=\"minor\", width=1.0, length=5, labelsize=10, labelcolor=\"0.25\")\n", "\n", - "ax.grid(linestyle=\"--\", linewidth=0.5, color='.25', zorder=-10)\n", + "ax.grid(linestyle=\"--\", linewidth=0.5, color=\".25\", zorder=-10)\n", "\n", - "ax.plot(X, Y1, c='C0', lw=2.5, label=\"Blue signal\", zorder=10)\n", - "ax.plot(X, Y2, c='C1', lw=2.5, label=\"Orange signal\")\n", - "ax.plot(X[::3], Y3[::3], linewidth=0, markersize=9,\n", - " marker='s', markerfacecolor='none', markeredgecolor='C4',\n", - " markeredgewidth=2.5)\n", + "ax.plot(X, Y1, c=\"C0\", lw=2.5, label=\"Blue signal\", zorder=10)\n", + "ax.plot(X, Y2, c=\"C1\", lw=2.5, label=\"Orange signal\")\n", + "ax.plot(\n", + " X[::3],\n", + " Y3[::3],\n", + " linewidth=0,\n", + " markersize=9,\n", + " marker=\"s\",\n", + " markerfacecolor=\"none\",\n", + " markeredgecolor=\"C4\", # color 5 in color palette\n", + " markeredgewidth=2.5,\n", + ")\n", "\n", - "ax.set_title(\"Anatomy of a figure\", fontsize=20, verticalalignment='bottom')\n", + "ax.set_title(\"Anatomy of a figure\", fontsize=20, verticalalignment=\"bottom\")\n", "ax.set_xlabel(\"x Axis label\", fontsize=14)\n", "ax.set_ylabel(\"y Axis label\", fontsize=14)\n", "ax.legend(loc=\"upper right\", fontsize=14)" @@ -332,7 +344,7 @@ "outputs": [], "source": [ "fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7.4, 4))\n", - "axes = axes.flatten() # in case of more than one dimension (safety snippet for you)\n", + "axes = axes.flatten() # in case of more than one dimension (safety snippet for you)\n", "ax = axes[0]\n", "n, bins, patches = ax.hist(x, bins=30, alpha=0.7, color=\"C0\")\n", "ax = axes[1]\n", diff --git a/1_1_matplotlib.py b/1_1_matplotlib.py index f741054..551cdea 100644 --- a/1_1_matplotlib.py +++ b/1_1_matplotlib.py @@ -13,8 +13,8 @@ import numpy as np import pandas as pd -mpl.rcParams['pdf.fonttype'] = 42 -mpl.rcParams['ps.fonttype'] = 42 +mpl.rcParams["pdf.fonttype"] = 42 +mpl.rcParams["ps.fonttype"] = 42 # %% [markdown] # ## Basic Line Plot @@ -49,7 +49,13 @@ x = mu + sigma * np.random.randn(10000) fig, ax = plt.subplots(figsize=(5, 2.7), layout="constrained") # the histogram of the data -n, bins, patches = ax.hist(x, 50, density=True, facecolor="C0", alpha=0.75) +n, bins, patches = ax.hist( + x, + 50, + density=True, + facecolor="C0", # first color in color palette + alpha=0.75, +) ax.set_xlabel("Length [cm]") ax.set_ylabel("Probability") @@ -73,7 +79,7 @@ Y2 = 1 + np.cos(1 + X / 0.75) / 2 Y3 = np.random.uniform(Y1, Y2, len(X)) -data = {'X': X, 'red_line': Y1, 'blue_line': Y2, 'circles': Y3} +data = {"X": X, "red_line": Y1, "blue_line": Y2, "circles": Y3} data = pd.DataFrame(data) data.head() @@ -95,19 +101,25 @@ ax.set_xlim(0, 4) ax.set_ylim(0, 4) -ax.tick_params(which='major', width=1.0, length=10, labelsize=14) -ax.tick_params(which='minor', width=1.0, length=5, labelsize=10, - labelcolor='0.25') - -ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10) - -ax.plot(X, Y1, c='C0', lw=2.5, label="Blue signal", zorder=10) -ax.plot(X, Y2, c='C1', lw=2.5, label="Orange signal") -ax.plot(X[::3], Y3[::3], linewidth=0, markersize=9, - marker='s', markerfacecolor='none', markeredgecolor='C4', - markeredgewidth=2.5) - -ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom') +ax.tick_params(which="major", width=1.0, length=10, labelsize=14) +ax.tick_params(which="minor", width=1.0, length=5, labelsize=10, labelcolor="0.25") + +ax.grid(linestyle="--", linewidth=0.5, color=".25", zorder=-10) + +ax.plot(X, Y1, c="C0", lw=2.5, label="Blue signal", zorder=10) +ax.plot(X, Y2, c="C1", lw=2.5, label="Orange signal") +ax.plot( + X[::3], + Y3[::3], + linewidth=0, + markersize=9, + marker="s", + markerfacecolor="none", + markeredgecolor="C4", # color 5 in color palette + markeredgewidth=2.5, +) + +ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment="bottom") ax.set_xlabel("x Axis label", fontsize=14) ax.set_ylabel("y Axis label", fontsize=14) ax.legend(loc="upper right", fontsize=14) @@ -180,7 +192,7 @@ # %% fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7.4, 4)) -axes = axes.flatten() # in case of more than one dimension (safety snippet for you) +axes = axes.flatten() # in case of more than one dimension (safety snippet for you) ax = axes[0] n, bins, patches = ax.hist(x, bins=30, alpha=0.7, color="C0") ax = axes[1] From 9bc0e041b6fbf207cc2f7806341522bbfe604c61 Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 20 Nov 2025 10:09:54 +0100 Subject: [PATCH 4/5] :memo: add links to used data for examples (download link on website) --- 1_3_seaborn.ipynb | 22 +++++----------------- 1_3_seaborn.py | 9 +++------ data/README.md | 6 ++++++ exercises/CRISPRi/README.md | 7 +++++++ exercises/CRISPRi/figure_2.ipynb | 20 +++++++++++++++++--- exercises/CRISPRi/figure_2.py | 9 +++++++++ 6 files changed, 47 insertions(+), 26 deletions(-) diff --git a/1_3_seaborn.ipynb b/1_3_seaborn.ipynb index 1b91b6e..838fd61 100644 --- a/1_3_seaborn.ipynb +++ b/1_3_seaborn.ipynb @@ -10,29 +10,18 @@ "- [website](https://seaborn.pydata.org/)" ] }, - { - "cell_type": "markdown", - "metadata": { - "lines_to_next_cell": 2 - }, - "source": [ - "## Recreating the Figure from the sulforaphane Paper" - ] - }, { "cell_type": "markdown", "metadata": {}, "source": [ + "## Recreating the Figure from the sulforaphane Paper\n", "Uses the data from [PXD04621](data/README.md) - using simulated data which looks\n", "similar to the original data\n", "([Marshall et. al. 2023)](https://www.sciencedirect.com/science/article/pii/S1756464623002451)).\n", - "![figure_2](https://ars.els-cdn.com/content/image/1-s2.0-S1756464623002451-gr2.jpg)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ + "- [fake_growth_data.csv](data/growth/fake_growth_data.csv)\n", + "\n", + "![figure_2](https://ars.els-cdn.com/content/image/1-s2.0-S1756464623002451-gr2.jpg)\n", + "\n", "Take a look at this picture - do you think it is simple or complicated?\n", "It is not trivial to recreate it in Python - but don't worry, we will guide through all steps and tricks on making an (almost) exact copy of that picture in this notebook (at least from our knowledge). Let's get started." ] @@ -63,7 +52,6 @@ "import matplotlib as mpl\n", "import matplotlib.lines as mlines\n", "import matplotlib.pyplot as plt\n", - "import numpy as np\n", "import pandas as pd\n", "import seaborn as sns\n", "from matplotlib.ticker import MultipleLocator\n", diff --git a/1_3_seaborn.py b/1_3_seaborn.py index 6837dcf..3777887 100644 --- a/1_3_seaborn.py +++ b/1_3_seaborn.py @@ -20,15 +20,13 @@ # %% [markdown] # ## Recreating the Figure from the sulforaphane Paper - - -# %% [markdown] # Uses the data from [PXD04621](data/README.md) - using simulated data which looks # similar to the original data # ([Marshall et. al. 2023)](https://www.sciencedirect.com/science/article/pii/S1756464623002451)). +# - [fake_growth_data.csv](data/growth/fake_growth_data.csv) +# # ![figure_2](https://ars.els-cdn.com/content/image/1-s2.0-S1756464623002451-gr2.jpg) - -# %% [markdown] +# # Take a look at this picture - do you think it is simple or complicated? # It is not trivial to recreate it in Python - but don't worry, we will guide through all steps and tricks on making an (almost) exact copy of that picture in this notebook (at least from our knowledge). Let's get started. @@ -45,7 +43,6 @@ import matplotlib as mpl import matplotlib.lines as mlines import matplotlib.pyplot as plt -import numpy as np import pandas as pd import seaborn as sns from matplotlib.ticker import MultipleLocator diff --git a/data/README.md b/data/README.md index 34662e8..667ee29 100644 --- a/data/README.md +++ b/data/README.md @@ -25,3 +25,9 @@ to Caroline Jachmann for the great work assembling a peptide atlas with post tra modifications for ecoli: - [E. coli peptide atlas with phosphosites](https://www.uniprot.org/proteomes/UP000000625) + +## Files used in notebooks + +and hosted in the repository of the course. + +- [data/proteins/proteins.csv](proteins/proteins.csv) diff --git a/exercises/CRISPRi/README.md b/exercises/CRISPRi/README.md index 89418b5..2119b69 100644 --- a/exercises/CRISPRi/README.md +++ b/exercises/CRISPRi/README.md @@ -9,3 +9,10 @@ Nat Commun 15, 8985 (2024). [https://doi.org/10.1038/s41467-024-53381-4](https:/ Direct data link: [Source data](https://static-content.springer.com/esm/art%3A10.1038%2Fs41467-024-53381-4/MediaObjects/41467_2024_53381_MOESM6_ESM.xlsx) + +## Files used in notebooks + +- [figure_2a.csv](figure_2a.csv) +- [figure_2b.csv](figure_2b.csv) +- [figure_2c.csv](figure_2c.csv) +- [figure_2d.csv](figure_2d.csv) diff --git a/exercises/CRISPRi/figure_2.ipynb b/exercises/CRISPRi/figure_2.ipynb index b8ba16f..4767707 100644 --- a/exercises/CRISPRi/figure_2.ipynb +++ b/exercises/CRISPRi/figure_2.ipynb @@ -17,9 +17,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "lines_to_next_cell": 2 - }, + "metadata": {}, "outputs": [], "source": [ "import os\n", @@ -41,6 +39,22 @@ "IN_COLAB = \"COLAB_GPU\" in os.environ" ] }, + { + "cell_type": "markdown", + "id": "d848d620", + "metadata": { + "lines_to_next_cell": 2 + }, + "source": [ + "## Setup\n", + "Let's make use of the files\n", + "\n", + "- [figure_2a.csv](figure_2a.csv)\n", + "- [figure_2b.csv](figure_2b.csv)\n", + "- [figure_2c.csv](figure_2c.csv)\n", + "- [figure_2d.csv](figure_2d.csv)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/exercises/CRISPRi/figure_2.py b/exercises/CRISPRi/figure_2.py index bca9a66..20d2123 100644 --- a/exercises/CRISPRi/figure_2.py +++ b/exercises/CRISPRi/figure_2.py @@ -41,6 +41,15 @@ IN_COLAB = "COLAB_GPU" in os.environ +# %% [markdown] +# ## Setup +# Let's make use of the files +# +# - [figure_2a.csv](figure_2a.csv) +# - [figure_2b.csv](figure_2b.csv) +# - [figure_2c.csv](figure_2c.csv) +# - [figure_2d.csv](figure_2d.csv) + # %% A_DATA_FILE = "figure_2a.csv" From 9d1b07b82826799c64b5f15401b9d953fb046f4d Mon Sep 17 00:00:00 2001 From: Henry Webel Date: Thu, 20 Nov 2025 10:10:20 +0100 Subject: [PATCH 5/5] :sparkles: hint to matplotlib parameters --- 1_4_matplotlib_config.ipynb | 84 +++++++++++++++++++++++++++++++++++++ 1_4_matplotlib_config.py | 30 +++++++++++++ index.md | 1 + 3 files changed, 115 insertions(+) create mode 100644 1_4_matplotlib_config.ipynb create mode 100644 1_4_matplotlib_config.py diff --git a/1_4_matplotlib_config.ipynb b/1_4_matplotlib_config.ipynb new file mode 100644 index 0000000..ba9739b --- /dev/null +++ b/1_4_matplotlib_config.ipynb @@ -0,0 +1,84 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "82fc7f4b", + "metadata": { + "lines_to_next_cell": 0 + }, + "source": [ + "# Matplotlib Configuration\n", + "\n", + "Matplotlib Runtime Configuration (rc) allows you to customize the default styles and \n", + "behaviors of plots. The rcParams dictionary stores these settings,\n", + "such as font sizes, line widths,and color schemes.\n", + "You can modify rcParams directly in your code to change the appearance of all plots globally.\n", + "Example: `mpl.rcParams['lines.linewidth'] = 2.0`\n", + "You can also use style sheets or the 'matplotlibrc' file for persistent configuration." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bcf6d84", + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib\n", + "import matplotlib.font_manager\n", + "\n", + "print(matplotlib.rcParams['font.size']) # Default font size (usually 'medium')\n", + "print(matplotlib.font_manager.font_scalings) # Shows the scaling factors for each size keyword" + ] + }, + { + "cell_type": "markdown", + "id": "de009889", + "metadata": {}, + "source": [ + "Find the full list of rcParams you can modify\n", + "See the documentation [here] and specifically \n", + "[`rcParams`](https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.rcParams)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d2460360", + "metadata": { + "lines_to_next_cell": 2 + }, + "outputs": [], + "source": [ + "matplotlib.rcParams['font.size'] = 14 # Set a new default font size" + ] + }, + { + "cell_type": "markdown", + "id": "b8d59ddd", + "metadata": {}, + "source": [ + "See them all below:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "55cb182f", + "metadata": {}, + "outputs": [], + "source": [ + "matplotlib.rcParams" + ] + } + ], + "metadata": { + "jupytext": { + "cell_metadata_filter": "-all", + "main_language": "python", + "notebook_metadata_filter": "-all" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/1_4_matplotlib_config.py b/1_4_matplotlib_config.py new file mode 100644 index 0000000..8921c1a --- /dev/null +++ b/1_4_matplotlib_config.py @@ -0,0 +1,30 @@ +# %% [markdown] +# # Matplotlib Configuration +# +# Matplotlib Runtime Configuration (rc) allows you to customize the default styles and +# behaviors of plots. The rcParams dictionary stores these settings, +# such as font sizes, line widths,and color schemes. +# You can modify rcParams directly in your code to change the appearance of all plots globally. +# Example: `mpl.rcParams['lines.linewidth'] = 2.0` +# You can also use style sheets or the 'matplotlibrc' file for persistent configuration. +# %% +import matplotlib +import matplotlib.font_manager + +print(matplotlib.rcParams['font.size']) # Default font size (usually 'medium') +print(matplotlib.font_manager.font_scalings) # Shows the scaling factors for each size keyword + +# %% [markdown] +# Find the full list of rcParams you can modify +# See the documentation [here] and specifically +# [`rcParams`](https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.rcParams). + +# %% +matplotlib.rcParams['font.size'] = 14 # Set a new default font size + + +# %% [markdown] +# See them all below: + +# %% +matplotlib.rcParams diff --git a/index.md b/index.md index e56e69b..5b864b5 100644 --- a/index.md +++ b/index.md @@ -23,6 +23,7 @@ 1_2_pandas 1_2_seaborn 1_3_seaborn +1_4_matplotlib_config 2_plotly 3_vuecore ```