Skip to content

Commit c93d1d7

Browse files
authored
Merge pull request #1 from biosustain/first_draft
First draft. Needs to be update with plots used in R.
2 parents 798c8e3 + 2996174 commit c93d1d7

21 files changed

+86743
-6
lines changed

.devcontainer/devcontainer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/python
3+
{
4+
"name": "Python 3",
5+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6+
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
7+
"workspaceFolder": "/workspaces/dsp_workshop_dataviz_python",
8+
9+
// Features to add to the dev container. More info: https://containers.dev/features.
10+
// "features": {},
11+
12+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
13+
// "forwardPorts": [],
14+
15+
// Use 'postCreateCommand' to run commands after the container is created.
16+
"postCreateCommand": "pip3 install --user -r requirements.txt",
17+
// "onCreateCommand": "bash .devcontainer/setup.sh",
18+
19+
// Configure tool-specific properties.
20+
"customizations": {
21+
"vscode": {
22+
"extensions": [
23+
"ms-toolsai.jupyter",
24+
"ms-python.python",
25+
"mechatroner.rainbow-csv"
26+
],
27+
// Use Python from conda
28+
"settings": {},
29+
// Use bash
30+
"terminal.integrated.defaultProfile.linux": "bash"
31+
}
32+
}
33+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
34+
// "remoteUser": "root"
35+
}

.github/workflows/build_website.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
push:
77
branches:
88
- main
9+
pull_request:
10+
branches:
11+
- main
912
workflow_dispatch:
1013

1114
# This job installs dependencies, builds the website and pushes it to `gh-pages`
@@ -33,3 +36,11 @@ jobs:
3336
github_token: ${{ secrets.GITHUB_TOKEN }}
3437
publish_dir: ./_build
3538
# cname: website.com
39+
- if: ${{ github.ref != 'refs/heads/main' }}
40+
name: Save to gh-pages
41+
uses: peaceiris/actions-gh-pages@v4
42+
with:
43+
github_token: ${{ secrets.GITHUB_TOKEN }}
44+
publish_dir: ./_build
45+
# hide dev version of website in subfolder
46+
destination_dir: dev

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
_build
2+
jupyter_execute
23
*.docx

1_1_matplotlib.ipynb

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "01419663",
6+
"metadata": {},
7+
"source": [
8+
"# Matplotlib\n",
9+
"- A comprehensive library for creating static, animated, and interactive visualizations in Python.\n",
10+
"- Built on NumPy arrays and designed to work with the broader SciPy stack."
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "0748b014",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"import matplotlib as mpl\n",
21+
"import matplotlib.pyplot as plt\n",
22+
"import numpy as np"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"id": "348c1b11",
28+
"metadata": {},
29+
"source": [
30+
"## Basic Line Plot\n",
31+
"From Getting Started."
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"id": "37ad8b55",
38+
"metadata": {},
39+
"outputs": [],
40+
"source": [
41+
"fig, ax = plt.subplots() # Create a figure containing a single Axes.\n",
42+
"ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the Axes.\n",
43+
"plt.show() # Show the figure."
44+
]
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"id": "dd74e2cc",
49+
"metadata": {
50+
"lines_to_next_cell": 0
51+
},
52+
"source": [
53+
"![Anatomy of a matplotlib figure](https://matplotlib.org/stable/_images/anatomy.png)"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"id": "2d86fcd1",
60+
"metadata": {},
61+
"outputs": [],
62+
"source": [
63+
"fig, ax = plt.subplots() # Create a figure containing a single Axes.\n",
64+
"ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the Axes.\n",
65+
"ax.grid()\n",
66+
"plt.show()"
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"id": "0595fdfd",
72+
"metadata": {},
73+
"source": [
74+
"Customize ticks\n",
75+
"- [Ticker API](https://matplotlib.org/stable/api/ticker_api.html)"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": null,
81+
"id": "0817796f",
82+
"metadata": {},
83+
"outputs": [],
84+
"source": [
85+
"ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))\n",
86+
"# ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))\n",
87+
"ax.get_figure()"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"id": "80d2be97",
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"mu, sigma = 115, 15\n",
98+
"x = mu + sigma * np.random.randn(10000)\n",
99+
"fig, ax = plt.subplots(figsize=(5, 2.7), layout=\"constrained\")\n",
100+
"# the histogram of the data\n",
101+
"n, bins, patches = ax.hist(x, 50, density=True, facecolor=\"C0\", alpha=0.75)\n",
102+
"\n",
103+
"ax.set_xlabel(\"Length [cm]\")\n",
104+
"ax.set_ylabel(\"Probability\")\n",
105+
"ax.set_title(\"Aardvark lengths\\n (not really)\")\n",
106+
"ax.text(75, 0.025, r\"$\\mu=115,\\ \\sigma=15$\")\n",
107+
"ax.axis([55, 175, 0, 0.03])\n",
108+
"ax.grid(True)"
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"execution_count": null,
114+
"id": "d78dc9ce",
115+
"metadata": {},
116+
"outputs": [],
117+
"source": [
118+
"# ## Proteomics data example"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": null,
124+
"id": "c290244b",
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"import pathlib\n",
129+
"\n",
130+
"import pandas as pd\n",
131+
"\n",
132+
"dir_data = pathlib.Path(\"data\")\n",
133+
"df = pd.read_csv(dir_data / \"proteins\" / \"proteins.csv\", index_col=0)\n",
134+
"df"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": null,
140+
"id": "1787a67d",
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"x = df.iloc[0]\n",
145+
"x"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
151+
"id": "8e27f422",
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"ax = x.hist()"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"id": "5e525e25",
162+
"metadata": {
163+
"lines_to_next_cell": 2
164+
},
165+
"outputs": [],
166+
"source": [
167+
"fig, ax = plt.subplots()\n",
168+
"n, bins, patches = ax.hist(x, bins=30, alpha=0.7, color=\"C0\")"
169+
]
170+
},
171+
{
172+
"cell_type": "markdown",
173+
"id": "6360d4cd",
174+
"metadata": {},
175+
"source": [
176+
"## Available styles\n",
177+
"Choose your preferred style with it's defaults\n",
178+
"[here](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html)\n",
179+
"\n",
180+
"```python\n",
181+
"plt.style.use('ggplot')\n",
182+
"```\n",
183+
"\n",
184+
"![ggplot](https://matplotlib.org/stable/_images/sphx_glr_style_sheets_reference_008_2_00x.png)\n",
185+
"![seaborn_v0_8-bright](https://matplotlib.org/stable/_images/sphx_glr_style_sheets_reference_012_2_00x.png)\n",
186+
"![seaborn_v0_8-white](https://matplotlib.org/stable/_images/sphx_glr_style_sheets_reference_025_2_00x.png)"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": null,
192+
"id": "f32690b4",
193+
"metadata": {},
194+
"outputs": [],
195+
"source": [
196+
"with plt.style.context(\"ggplot\"):\n",
197+
" fig, ax = plt.subplots()\n",
198+
" n, bins, patches = ax.hist(x, bins=30, alpha=0.7)"
199+
]
200+
},
201+
{
202+
"cell_type": "code",
203+
"execution_count": null,
204+
"id": "41efb93a",
205+
"metadata": {},
206+
"outputs": [],
207+
"source": []
208+
}
209+
],
210+
"metadata": {
211+
"jupytext": {
212+
"cell_metadata_filter": "-all",
213+
"main_language": "python",
214+
"notebook_metadata_filter": "-all"
215+
}
216+
},
217+
"nbformat": 4,
218+
"nbformat_minor": 5
219+
}

1_1_matplotlib.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# %% [markdown]
2+
# # Matplotlib
3+
# - A comprehensive library for creating static, animated, and interactive visualizations in Python.
4+
# - Built on NumPy arrays and designed to work with the broader SciPy stack.
5+
6+
# %%
7+
import matplotlib as mpl
8+
import matplotlib.pyplot as plt
9+
import numpy as np
10+
11+
# %% [markdown]
12+
# ## Basic Line Plot
13+
# From Getting Started.
14+
15+
# %%
16+
fig, ax = plt.subplots() # Create a figure containing a single Axes.
17+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the Axes.
18+
plt.show() # Show the figure.
19+
20+
# %% [markdown]
21+
# ![Anatomy of a matplotlib figure](https://matplotlib.org/stable/_images/anatomy.png)
22+
# %%
23+
fig, ax = plt.subplots() # Create a figure containing a single Axes.
24+
ax.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the Axes.
25+
ax.grid()
26+
plt.show()
27+
28+
# %% [markdown]
29+
# Customize ticks
30+
# - [Ticker API](https://matplotlib.org/stable/api/ticker_api.html)
31+
32+
# %%
33+
ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))
34+
# ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(integer=True))
35+
ax.get_figure()
36+
37+
# %%
38+
mu, sigma = 115, 15
39+
x = mu + sigma * np.random.randn(10000)
40+
fig, ax = plt.subplots(figsize=(5, 2.7), layout="constrained")
41+
# the histogram of the data
42+
n, bins, patches = ax.hist(x, 50, density=True, facecolor="C0", alpha=0.75)
43+
44+
ax.set_xlabel("Length [cm]")
45+
ax.set_ylabel("Probability")
46+
ax.set_title("Aardvark lengths\n (not really)")
47+
ax.text(75, 0.025, r"$\mu=115,\ \sigma=15$")
48+
ax.axis([55, 175, 0, 0.03])
49+
ax.grid(True)
50+
51+
# %%
52+
# ## Proteomics data example
53+
54+
# %%
55+
import pathlib
56+
57+
import pandas as pd
58+
59+
dir_data = pathlib.Path("data")
60+
df = pd.read_csv(dir_data / "proteins" / "proteins.csv", index_col=0)
61+
df
62+
63+
# %%
64+
x = df.iloc[0]
65+
x
66+
67+
# %%
68+
ax = x.hist()
69+
70+
# %%
71+
fig, ax = plt.subplots()
72+
n, bins, patches = ax.hist(x, bins=30, alpha=0.7, color="C0")
73+
74+
75+
# %% [markdown]
76+
# ## Available styles
77+
# Choose your preferred style with it's defaults
78+
# [here](https://matplotlib.org/stable/gallery/style_sheets/style_sheets_reference.html)
79+
#
80+
# ```python
81+
# plt.style.use('ggplot')
82+
# ```
83+
#
84+
# ![ggplot](https://matplotlib.org/stable/_images/sphx_glr_style_sheets_reference_008_2_00x.png)
85+
# ![seaborn_v0_8-bright](https://matplotlib.org/stable/_images/sphx_glr_style_sheets_reference_012_2_00x.png)
86+
# ![seaborn_v0_8-white](https://matplotlib.org/stable/_images/sphx_glr_style_sheets_reference_025_2_00x.png)
87+
88+
# %%
89+
with plt.style.context("ggplot"):
90+
fig, ax = plt.subplots()
91+
n, bins, patches = ax.hist(x, bins=30, alpha=0.7)
92+
93+
# %%

0 commit comments

Comments
 (0)