Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions 1_1_matplotlib.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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()"
]
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -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",
Expand Down
48 changes: 30 additions & 18 deletions 1_1_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
31 changes: 5 additions & 26 deletions 1_3_seaborn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
Expand Down Expand Up @@ -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",
Expand All @@ -75,15 +63,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,
Expand Down
12 changes: 3 additions & 9 deletions 1_3_seaborn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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
Expand All @@ -56,9 +53,6 @@

IN_COLAB = "COLAB_GPU" in os.environ

# %%
IN_COLAB = True

# %%
DATA_DIR = Path("data")

Expand Down
84 changes: 84 additions & 0 deletions 1_4_matplotlib_config.ipynb
Original file line number Diff line number Diff line change
@@ -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
}
30 changes: 30 additions & 0 deletions 1_4_matplotlib_config.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 12 additions & 2 deletions 2_plotly.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down
Loading
Loading