Skip to content

Commit bb448bd

Browse files
committed
📝 hint on color palette picking using C0....C9
1 parent 1b46ec0 commit bb448bd

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

1_1_matplotlib.ipynb

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"import numpy as np\n",
2727
"import pandas as pd\n",
2828
"\n",
29-
"mpl.rcParams['pdf.fonttype'] = 42\n",
30-
"mpl.rcParams['ps.fonttype'] = 42"
29+
"mpl.rcParams[\"pdf.fonttype\"] = 42\n",
30+
"mpl.rcParams[\"ps.fonttype\"] = 42"
3131
]
3232
},
3333
{
@@ -105,7 +105,13 @@
105105
"x = mu + sigma * np.random.randn(10000)\n",
106106
"fig, ax = plt.subplots(figsize=(5, 2.7), layout=\"constrained\")\n",
107107
"# the histogram of the data\n",
108-
"n, bins, patches = ax.hist(x, 50, density=True, facecolor=\"C0\", alpha=0.75)\n",
108+
"n, bins, patches = ax.hist(\n",
109+
" x,\n",
110+
" 50,\n",
111+
" density=True,\n",
112+
" facecolor=\"C0\", # first color in color palette\n",
113+
" alpha=0.75,\n",
114+
")\n",
109115
"\n",
110116
"ax.set_xlabel(\"Length [cm]\")\n",
111117
"ax.set_ylabel(\"Probability\")\n",
@@ -141,7 +147,7 @@
141147
"Y2 = 1 + np.cos(1 + X / 0.75) / 2\n",
142148
"Y3 = np.random.uniform(Y1, Y2, len(X))\n",
143149
"\n",
144-
"data = {'X': X, 'red_line': Y1, 'blue_line': Y2, 'circles': Y3}\n",
150+
"data = {\"X\": X, \"red_line\": Y1, \"blue_line\": Y2, \"circles\": Y3}\n",
145151
"data = pd.DataFrame(data)\n",
146152
"data.head()"
147153
]
@@ -175,19 +181,25 @@
175181
"ax.set_xlim(0, 4)\n",
176182
"ax.set_ylim(0, 4)\n",
177183
"\n",
178-
"ax.tick_params(which='major', width=1.0, length=10, labelsize=14)\n",
179-
"ax.tick_params(which='minor', width=1.0, length=5, labelsize=10,\n",
180-
" labelcolor='0.25')\n",
184+
"ax.tick_params(which=\"major\", width=1.0, length=10, labelsize=14)\n",
185+
"ax.tick_params(which=\"minor\", width=1.0, length=5, labelsize=10, labelcolor=\"0.25\")\n",
181186
"\n",
182-
"ax.grid(linestyle=\"--\", linewidth=0.5, color='.25', zorder=-10)\n",
187+
"ax.grid(linestyle=\"--\", linewidth=0.5, color=\".25\", zorder=-10)\n",
183188
"\n",
184-
"ax.plot(X, Y1, c='C0', lw=2.5, label=\"Blue signal\", zorder=10)\n",
185-
"ax.plot(X, Y2, c='C1', lw=2.5, label=\"Orange signal\")\n",
186-
"ax.plot(X[::3], Y3[::3], linewidth=0, markersize=9,\n",
187-
" marker='s', markerfacecolor='none', markeredgecolor='C4',\n",
188-
" markeredgewidth=2.5)\n",
189+
"ax.plot(X, Y1, c=\"C0\", lw=2.5, label=\"Blue signal\", zorder=10)\n",
190+
"ax.plot(X, Y2, c=\"C1\", lw=2.5, label=\"Orange signal\")\n",
191+
"ax.plot(\n",
192+
" X[::3],\n",
193+
" Y3[::3],\n",
194+
" linewidth=0,\n",
195+
" markersize=9,\n",
196+
" marker=\"s\",\n",
197+
" markerfacecolor=\"none\",\n",
198+
" markeredgecolor=\"C4\", # color 5 in color palette\n",
199+
" markeredgewidth=2.5,\n",
200+
")\n",
189201
"\n",
190-
"ax.set_title(\"Anatomy of a figure\", fontsize=20, verticalalignment='bottom')\n",
202+
"ax.set_title(\"Anatomy of a figure\", fontsize=20, verticalalignment=\"bottom\")\n",
191203
"ax.set_xlabel(\"x Axis label\", fontsize=14)\n",
192204
"ax.set_ylabel(\"y Axis label\", fontsize=14)\n",
193205
"ax.legend(loc=\"upper right\", fontsize=14)"
@@ -332,7 +344,7 @@
332344
"outputs": [],
333345
"source": [
334346
"fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7.4, 4))\n",
335-
"axes = axes.flatten() # in case of more than one dimension (safety snippet for you)\n",
347+
"axes = axes.flatten() # in case of more than one dimension (safety snippet for you)\n",
336348
"ax = axes[0]\n",
337349
"n, bins, patches = ax.hist(x, bins=30, alpha=0.7, color=\"C0\")\n",
338350
"ax = axes[1]\n",

1_1_matplotlib.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import numpy as np
1414
import pandas as pd
1515

16-
mpl.rcParams['pdf.fonttype'] = 42
17-
mpl.rcParams['ps.fonttype'] = 42
16+
mpl.rcParams["pdf.fonttype"] = 42
17+
mpl.rcParams["ps.fonttype"] = 42
1818

1919
# %% [markdown]
2020
# ## Basic Line Plot
@@ -49,7 +49,13 @@
4949
x = mu + sigma * np.random.randn(10000)
5050
fig, ax = plt.subplots(figsize=(5, 2.7), layout="constrained")
5151
# the histogram of the data
52-
n, bins, patches = ax.hist(x, 50, density=True, facecolor="C0", alpha=0.75)
52+
n, bins, patches = ax.hist(
53+
x,
54+
50,
55+
density=True,
56+
facecolor="C0", # first color in color palette
57+
alpha=0.75,
58+
)
5359

5460
ax.set_xlabel("Length [cm]")
5561
ax.set_ylabel("Probability")
@@ -73,7 +79,7 @@
7379
Y2 = 1 + np.cos(1 + X / 0.75) / 2
7480
Y3 = np.random.uniform(Y1, Y2, len(X))
7581

76-
data = {'X': X, 'red_line': Y1, 'blue_line': Y2, 'circles': Y3}
82+
data = {"X": X, "red_line": Y1, "blue_line": Y2, "circles": Y3}
7783
data = pd.DataFrame(data)
7884
data.head()
7985

@@ -95,19 +101,25 @@
95101
ax.set_xlim(0, 4)
96102
ax.set_ylim(0, 4)
97103

98-
ax.tick_params(which='major', width=1.0, length=10, labelsize=14)
99-
ax.tick_params(which='minor', width=1.0, length=5, labelsize=10,
100-
labelcolor='0.25')
101-
102-
ax.grid(linestyle="--", linewidth=0.5, color='.25', zorder=-10)
103-
104-
ax.plot(X, Y1, c='C0', lw=2.5, label="Blue signal", zorder=10)
105-
ax.plot(X, Y2, c='C1', lw=2.5, label="Orange signal")
106-
ax.plot(X[::3], Y3[::3], linewidth=0, markersize=9,
107-
marker='s', markerfacecolor='none', markeredgecolor='C4',
108-
markeredgewidth=2.5)
109-
110-
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment='bottom')
104+
ax.tick_params(which="major", width=1.0, length=10, labelsize=14)
105+
ax.tick_params(which="minor", width=1.0, length=5, labelsize=10, labelcolor="0.25")
106+
107+
ax.grid(linestyle="--", linewidth=0.5, color=".25", zorder=-10)
108+
109+
ax.plot(X, Y1, c="C0", lw=2.5, label="Blue signal", zorder=10)
110+
ax.plot(X, Y2, c="C1", lw=2.5, label="Orange signal")
111+
ax.plot(
112+
X[::3],
113+
Y3[::3],
114+
linewidth=0,
115+
markersize=9,
116+
marker="s",
117+
markerfacecolor="none",
118+
markeredgecolor="C4", # color 5 in color palette
119+
markeredgewidth=2.5,
120+
)
121+
122+
ax.set_title("Anatomy of a figure", fontsize=20, verticalalignment="bottom")
111123
ax.set_xlabel("x Axis label", fontsize=14)
112124
ax.set_ylabel("y Axis label", fontsize=14)
113125
ax.legend(loc="upper right", fontsize=14)
@@ -180,7 +192,7 @@
180192

181193
# %%
182194
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(7.4, 4))
183-
axes = axes.flatten() # in case of more than one dimension (safety snippet for you)
195+
axes = axes.flatten() # in case of more than one dimension (safety snippet for you)
184196
ax = axes[0]
185197
n, bins, patches = ax.hist(x, bins=30, alpha=0.7, color="C0")
186198
ax = axes[1]

0 commit comments

Comments
 (0)