From c7f61f51148c9575321e9140a2c0d35c97ae0415 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Wed, 17 Nov 2021 22:45:22 -0800 Subject: [PATCH 1/5] combine all tips scripts into one - can help make things a bit more DRY - remove unused tip-post-processing.py script --- scripts/tip-color-range.py | 24 ---- scripts/tip-colorbar.py | 25 ---- scripts/tip-dotted.py | 21 --- scripts/tip-dual-axis.py | 25 ---- scripts/tip-font-family.py | 50 -------- scripts/tip-hatched.py | 40 ------ scripts/tip-multiline.py | 24 ---- scripts/tip-outline.py | 27 ---- scripts/tip-post-processing.py | 27 ---- scripts/tip-transparency.py | 25 ---- scripts/tips.py | 226 +++++++++++++++++++++++++++++++++ 11 files changed, 226 insertions(+), 288 deletions(-) delete mode 100644 scripts/tip-color-range.py delete mode 100644 scripts/tip-colorbar.py delete mode 100644 scripts/tip-dotted.py delete mode 100644 scripts/tip-dual-axis.py delete mode 100644 scripts/tip-font-family.py delete mode 100644 scripts/tip-hatched.py delete mode 100644 scripts/tip-multiline.py delete mode 100644 scripts/tip-outline.py delete mode 100644 scripts/tip-post-processing.py delete mode 100644 scripts/tip-transparency.py create mode 100644 scripts/tips.py diff --git a/scripts/tip-color-range.py b/scripts/tip-color-range.py deleted file mode 100644 index b8b43eb..0000000 --- a/scripts/tip-color-range.py +++ /dev/null @@ -1,24 +0,0 @@ -# ----------------------------------------------------------------------------- -# Matplotlib cheat sheet -# Released under the BSD License -# ----------------------------------------------------------------------------- - -# Scripts to generate all the basic plots -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt - -fig = plt.figure(figsize=(2, 2)) -mpl.rcParams['axes.linewidth'] = 1.5 -d = 0.01 - -ax = fig.add_axes([d, d, 1-2*d, 1-2*d], xticks=[], yticks=[]) - -X = np.random.seed(1) -X = np.random.randn(1000, 4) -cmap = plt.get_cmap("Oranges") -colors = [cmap(i) for i in [.1, .3, .5, .7]] -ax.hist(X, 2, density=True, histtype='bar', color=colors) - -plt.savefig("../figures/tip-color-range.pdf") -# plt.show() diff --git a/scripts/tip-colorbar.py b/scripts/tip-colorbar.py deleted file mode 100644 index 2a46357..0000000 --- a/scripts/tip-colorbar.py +++ /dev/null @@ -1,25 +0,0 @@ -# ----------------------------------------------------------------------------- -# Matplotlib cheat sheet -# Released under the BSD License -# ----------------------------------------------------------------------------- - -# Scripts to generate all the basic plots -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -import matplotlib.patheffects as path_effects - -fig = plt.figure(figsize=(2.15, 2)) -mpl.rcParams['axes.linewidth'] = 1.5 -d = 0.01 -ax = fig.add_axes([d, d, 1-2*d, 1-2*d], xticks=[], yticks=[]) - -np.random.seed(1) -Z = np.random.uniform(0, 1, (8, 8)) -cmap = plt.get_cmap("Oranges") -im = ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) -cb = fig.colorbar(im, fraction=0.046, pad=0.04) -cb.set_ticks([]) - -plt.savefig("../figures/tip-colorbar.pdf") -# plt.show() diff --git a/scripts/tip-dotted.py b/scripts/tip-dotted.py deleted file mode 100644 index ebfae99..0000000 --- a/scripts/tip-dotted.py +++ /dev/null @@ -1,21 +0,0 @@ -# ----------------------------------------------------------------------------- -# Matplotlib cheat sheet -# Released under the BSD License -# ----------------------------------------------------------------------------- - -# Scripts to generate all the basic plots -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -fig = plt.figure(figsize=(5, .25)) - -ax = fig.add_axes([0, 0, 1, 1], frameon=False, - xticks=[], yticks=[], xlim=[0, 1], ylim=[-.5, 1.5]) - -epsilon=1e-12 -plt.plot([0, 1], [0, 0], "black", clip_on=False, lw=8, - ls=(.5, (epsilon, 1)), dash_capstyle="round") -plt.plot([0, 1], [1, 1], "black", clip_on=False, lw=8, - ls=(-.5, (epsilon, 2)), dash_capstyle="round") -plt.savefig("../figures/tip-dotted.pdf") -# plt.show() diff --git a/scripts/tip-dual-axis.py b/scripts/tip-dual-axis.py deleted file mode 100644 index 74677eb..0000000 --- a/scripts/tip-dual-axis.py +++ /dev/null @@ -1,25 +0,0 @@ -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['axes.linewidth'] = 1.5 - - -fig = plt.figure(figsize=(2, 2)) -d = 0.01 -ax1 = fig.add_axes([d, d, 1-2*d, 1-2*d], label="cartesian") -ax2 = fig.add_axes([d, d, 1-2*d, 1-2*d], projection="polar", label="polar") - -ax1.set_xticks([]) # np.linspace(0.0, 0.4, 5)) -ax1.set_yticks([]) # np.linspace(0.0, 1.0, 11)) - -ax2.set_rorigin(0) -ax2.set_thetamax(90) -ax2.set_ylim(0.5, 1.0) -ax2.set_xticks(np.linspace(0, np.pi/2, 10)) -ax2.set_yticks(np.linspace(0.5, 1.0, 5)) - -ax2.set_xticklabels([]) -ax2.set_yticklabels([]) - -plt.savefig("../figures/tip-dual-axis.pdf") -# plt.show() diff --git a/scripts/tip-font-family.py b/scripts/tip-font-family.py deleted file mode 100644 index 8689b3b..0000000 --- a/scripts/tip-font-family.py +++ /dev/null @@ -1,50 +0,0 @@ -# ---------------------------------------------------------------------------- -# Title: Scientific Visualisation - Python & Matplotlib -# Author: Nicolas P. Rougier -# License: BSD -# ---------------------------------------------------------------------------- -import numpy as np -import matplotlib.pyplot as plt -import matplotlib.ticker as ticker - -# Setup a plot such that only the bottom spine is shown - - -def setup(ax): - ax.spines['right'].set_color('none') - ax.spines['left'].set_color('none') - ax.yaxis.set_major_locator(ticker.NullLocator()) - ax.spines['top'].set_color('none') - - ax.spines['bottom'].set_position("center") - - ax.xaxis.set_ticks_position('bottom') - ax.tick_params(which='major', width=1.00) - ax.tick_params(which='major', length=5) - ax.tick_params(which='minor', width=0.75) - ax.tick_params(which='minor', length=2.5) - ax.set_xlim(0, 5) - ax.set_ylim(0, 1) - ax.patch.set_alpha(0.0) - - -fig = plt.figure(figsize=(5, .5)) -fig.patch.set_alpha(0.0) -n = 1 - -fontsize = 18 -ax = plt.subplot(n, 1, 1) -ax.tick_params(axis='both', which='minor', labelsize=6) -setup(ax) -ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0)) -ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.2)) -ax.xaxis.set_major_formatter(ticker.ScalarFormatter()) -ax.xaxis.set_minor_formatter(ticker.ScalarFormatter()) -ax.tick_params(axis='x', which='minor', rotation=0) - -for tick in ax.get_xticklabels(which='both'): - tick.set_fontname("Roboto Condensed") - -plt.tight_layout() -plt.savefig("../figures/tip-font-family.pdf", transparent=True) -# plt.show() diff --git a/scripts/tip-hatched.py b/scripts/tip-hatched.py deleted file mode 100644 index c8a4ec3..0000000 --- a/scripts/tip-hatched.py +++ /dev/null @@ -1,40 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt - -cmap = plt.get_cmap("Oranges") -color1, color2 = cmap(0.3), cmap(0.5) - -plt.rcParams['hatch.color'] = color1 -plt.rcParams['hatch.linewidth'] = 8 - -fig = plt.figure(figsize=(2, 2)) -ax = plt.subplot() -np.random.seed(123) - -x1, y1 = 3*np.arange(2), np.random.randint(25, 50, 2) -x2, y2 = x1+1, np.random.randint(25, 75, 2) - -ax.bar(x1, y1, color=color2) -for i in range(len(x1)): - plt.annotate("%d%%" % y1[i], (x1[i], y1[i]), xytext=(0, 1), - fontsize="x-small", color=color2, - textcoords="offset points", va="bottom", ha="center") - -ax.bar(x2, y2, color=color2, hatch="/" ) -for i in range(len(x2)): - plt.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0, 1), - fontsize="x-small", color=color2, - textcoords="offset points", va="bottom", ha="center") - -ax.set_yticks([]) -ax.set_xticks(0.5+np.arange(0, 6, 3)) -ax.set_xticklabels(["2018", "2019"]) -ax.tick_params('x', length=0, labelsize="small", which='major') - -ax.spines['right'].set_visible(False) -ax.spines['left'].set_visible(False) -ax.spines['top'].set_visible(False) - -plt.tight_layout() -plt.savefig("../figures/tip-hatched.pdf") -# plt.show() diff --git a/scripts/tip-multiline.py b/scripts/tip-multiline.py deleted file mode 100644 index 1b62b76..0000000 --- a/scripts/tip-multiline.py +++ /dev/null @@ -1,24 +0,0 @@ -# ---------------------------------------------------------------------------- -# Author: Nicolas P. Rougier -# License: BSD -# ---------------------------------------------------------------------------- -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rcParams['axes.linewidth'] = 1.5 - -fig = plt.figure(figsize=(8, 1.5)) -dx, dy = 0.0025, 0.01 -ax = fig.add_axes([dx, dy, 1-2*dx, 1-2*dy], frameon=False) -X, Y = [], [] -for x in np.linspace(0.01, 10*np.pi-0.01, 100): - X.extend([x, x, None]) - Y.extend([0, np.sin(x), None]) -print(X[:10], Y[:10]) -plt.plot(X, Y, "black") -plt.xticks([]), plt.yticks([]) -plt.xlim(-0.25, 10*np.pi+.25) -plt.ylim(-1.5, 1.5) -plt.tight_layout() -plt.savefig("../figures/tip-multiline.pdf", dpi=100) -# plt.show() diff --git a/scripts/tip-outline.py b/scripts/tip-outline.py deleted file mode 100644 index e376149..0000000 --- a/scripts/tip-outline.py +++ /dev/null @@ -1,27 +0,0 @@ -# ----------------------------------------------------------------------------- -# Matplotlib cheat sheet -# Released under the BSD License -# ----------------------------------------------------------------------------- - -# Scripts to generate all the basic plots -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -import matplotlib.patheffects as path_effects - -fig = plt.figure(figsize=(2, 2)) -mpl.rcParams['axes.linewidth'] = 1.5 -d = 0.01 - -ax = fig.add_axes([d, d, 1-2*d, 1-2*d], xticks=[], yticks=[]) - -np.random.seed(1) -Z = np.random.uniform(0, 1, (8, 8)) -cmap = plt.get_cmap("Oranges") -ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) - -text = ax.text(0.5, 0.1, "Label", transform=ax.transAxes, - color=cmap(0.9), size=32, weight="bold", ha="center", va="bottom") -text.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), - path_effects.Normal()]) -plt.savefig("../figures/tip-outline.pdf") diff --git a/scripts/tip-post-processing.py b/scripts/tip-post-processing.py deleted file mode 100644 index 4086d03..0000000 --- a/scripts/tip-post-processing.py +++ /dev/null @@ -1,27 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -from matplotlib.figure import Figure -from matplotlib.backends.backend_agg import FigureCanvas -from scipy.ndimage import gaussian_filter - -# First pass for drop-shadow -fig = Figure(figsize=(6, 1.5)) -canvas = FigureCanvas(fig) -ax = fig.add_axes([0, 0, 1, 1], frameon=False, - xlim=[0, 1], xticks=[], ylim=[0, 1], yticks=[]) -ax.text(0.5, 0.5, "Matplotlib", transform=ax.transAxes, - ha="center", va="center", size=64, color="black") -canvas.draw() -Z = np.array(canvas.renderer.buffer_rgba())[:, :, 0] -Z = gaussian_filter(Z, sigma=9) - -# Second pass for text + drop-shadow -fig = plt.figure(figsize=(6, 1.5)) -ax = fig.add_axes([0, 0, 1, 1], frameon=False, - xlim=[0, 1], xticks=[], ylim=[0, 1], yticks=[]) -ax.imshow(Z, extent=[0, 1, 0, 1], cmap=plt.cm.gray, alpha=0.65, aspect='auto') -ax.text(0.5, 0.5, "Matplotlib", transform=ax.transAxes, - ha="center", va="center", size=64, color="black") - -plt.savefig("../figures/tip-post-processing.pdf", dpi=600) -# plt.show() diff --git a/scripts/tip-transparency.py b/scripts/tip-transparency.py deleted file mode 100644 index 2d9abc6..0000000 --- a/scripts/tip-transparency.py +++ /dev/null @@ -1,25 +0,0 @@ -# ----------------------------------------------------------------------------- -# Matplotlib cheat sheet -# Released under the BSD License -# ----------------------------------------------------------------------------- -import numpy as np -import matplotlib as mpl -import matplotlib.pyplot as plt -mpl.rc('axes', linewidth=1.5) - -np.random.seed(123) - -fig = plt.figure(figsize=(2, 2), dpi=100) -margin = 0.01 -ax = fig.add_axes([margin, margin, 1-2*margin, 1-2*margin]) -n = 500 -X = np.random.normal(0, 0.25, n) -Y = np.random.normal(0, 0.25, n) -ax.scatter(X, Y, s=50, c="k", lw=2) -ax.scatter(X, Y, s=50, c="w", lw=0) -ax.scatter(X, Y, s=40, c="C1", lw=0, alpha=0.1) - -ax.set_xlim([-1, 1]), ax.set_xticks([]), -ax.set_ylim([-1, 1]), ax.set_yticks([]) -plt.savefig("../figures/tip-transparency.pdf") -# plt.show() diff --git a/scripts/tips.py b/scripts/tips.py new file mode 100644 index 0000000..69574ee --- /dev/null +++ b/scripts/tips.py @@ -0,0 +1,226 @@ +# ----------------------------------------------------------------------------- +# Matplotlib cheat sheet +# Released under the BSD License +# ----------------------------------------------------------------------------- + +# Script to generate the tips +import numpy as np +import matplotlib as mpl +import matplotlib.pyplot as plt +import matplotlib.ticker as ticker +import matplotlib.patheffects as path_effects + + +# color range +# ----------------------------------------------------------------------------- +fig = plt.figure(figsize=(2,2)) +mpl.rcParams['axes.linewidth'] = 1.5 +d = 0.01 + +ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[]) + +X = np.random.seed(1) +X = np.random.randn(1000, 4) +cmap = plt.get_cmap("Oranges") +colors = [cmap(i) for i in [.1,.3,.5,.7]] +ax.hist(X, 2, density=True, histtype='bar', color=colors) + +plt.savefig("../figures/tip-color-range.pdf") + + +# colorbar +# ----------------------------------------------------------------------------- +fig = plt.figure(figsize=(2.15,2)) +mpl.rcParams['axes.linewidth'] = 1.5 +d = 0.01 +ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[]) + +np.random.seed(1) +Z = np.random.uniform(0,1,(8,8)) +cmap = plt.get_cmap("Oranges") +im = ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) +cb = fig.colorbar(im, fraction=0.046, pad=0.04) +cb.set_ticks([]) + +plt.savefig("../figures/tip-colorbar.pdf") + + +# dotted +# ----------------------------------------------------------------------------- +fig = plt.figure(figsize=(5,.25)) + +ax = fig.add_axes([0,0,1,1], frameon=False, + xticks=[], yticks=[], xlim=[0,1], ylim=[-.5,1.5]) + +epsilon=1e-12 +plt.plot([0,1], [0,0], "black", clip_on=False, lw=8, + ls=(.5,(epsilon, 1)), dash_capstyle="round") +plt.plot([0,1], [1,1], "black", clip_on=False, lw=8, + ls=(-.5,(epsilon, 2)), dash_capstyle="round") +plt.savefig("../figures/tip-dotted.pdf") + + +# dual axis +# ----------------------------------------------------------------------------- +mpl.rcParams['axes.linewidth'] = 1.5 + + +fig = plt.figure(figsize=(2,2)) +d = 0.01 +ax1 = fig.add_axes([d,d,1-2*d,1-2*d], label="cartesian") +ax2 = fig.add_axes([d,d,1-2*d,1-2*d], projection="polar", label="polar") + +ax1.set_xticks([]) #np.linspace(0.0, 0.4, 5)) +ax1.set_yticks([]) #np.linspace(0.0, 1.0, 11)) + +ax2.set_rorigin(0) +ax2.set_thetamax(90) +ax2.set_ylim(0.5,1.0) +ax2.set_xticks(np.linspace(0, np.pi/2, 10)) +ax2.set_yticks(np.linspace(0.5, 1.0, 5)) + +ax2.set_xticklabels([]) +ax2.set_yticklabels([]) + +plt.savefig("../figures/tip-dual-axis.pdf") + + +# font family +# ----------------------------------------------------------------------------- +# Setup a plot such that only the bottom spine is shown +def setup(ax): + ax.spines['right'].set_color('none') + ax.spines['left'].set_color('none') + ax.yaxis.set_major_locator(ticker.NullLocator()) + ax.spines['top'].set_color('none') + + ax.spines['bottom'].set_position("center") + + ax.xaxis.set_ticks_position('bottom') + ax.tick_params(which='major', width=1.00) + ax.tick_params(which='major', length=5) + ax.tick_params(which='minor', width=0.75) + ax.tick_params(which='minor', length=2.5) + ax.set_xlim(0, 5) + ax.set_ylim(0, 1) + ax.patch.set_alpha(0.0) + + +fig = plt.figure(figsize=(5, .5)) +fig.patch.set_alpha(0.0) +n = 1 + +fontsize = 18 +ax = plt.subplot(n, 1, 1) +ax.tick_params(axis='both', which='minor', labelsize=6) +setup(ax) +ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0)) +ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.2)) +ax.xaxis.set_major_formatter(ticker.ScalarFormatter()) +ax.xaxis.set_minor_formatter(ticker.ScalarFormatter()) +ax.tick_params(axis='x', which='minor', rotation=0) + +for tick in ax.get_xticklabels(which='both'): + tick.set_fontname("Roboto Condensed") + +plt.tight_layout() +plt.savefig("../figures/tip-font-family.pdf", transparent=True) + + +# hatched +# ----------------------------------------------------------------------------- +cmap = plt.get_cmap("Oranges") +color1, color2 = cmap(0.3), cmap(0.5) + +plt.rcParams['hatch.color'] = color1 +plt.rcParams['hatch.linewidth'] = 8 + +fig = plt.figure(figsize=(2,2)) +ax = plt.subplot() +np.random.seed(123) + +x1,y1 = 3*np.arange(2), np.random.randint(25,50,2) +x2,y2 = x1+1, np.random.randint(25,75,2) + +ax.bar(x1, y1, color=color2) +for i in range(len(x1)): + plt.annotate("%d%%" % y1[i], (x1[i], y1[i]), xytext=(0,1), + fontsize="x-small", color=color2, + textcoords="offset points", va="bottom", ha="center") + +ax.bar(x2, y2, color=color2, hatch="/" ) +for i in range(len(x2)): + plt.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0,1), + fontsize="x-small", color=color2, + textcoords="offset points", va="bottom", ha="center") + +ax.set_yticks([]) +ax.set_xticks(0.5+np.arange(0,6,3)) +ax.set_xticklabels(["2018", "2019"]) +ax.tick_params('x', length=0, labelsize="small", which='major') + +ax.spines['right'].set_visible(False) +ax.spines['left'].set_visible(False) +ax.spines['top'].set_visible(False) + +plt.tight_layout() +plt.savefig("../figures/tip-hatched.pdf") + + +# multiline +# ----------------------------------------------------------------------------- +mpl.rcParams['axes.linewidth'] = 1.5 + +fig = plt.figure(figsize=(8,1.5)) +dx,dy = 0.0025, 0.01 +ax = fig.add_axes([dx, dy, 1-2*dx, 1-2*dy], frameon=False) +X,Y = [], [] +for x in np.linspace(0.01, 10*np.pi-0.01, 100): + X.extend([x, x,None]) + Y.extend([0, np.sin(x), None]) +print(X[:10], Y[:10]) +plt.plot(X, Y, "black") +plt.xticks([]), plt.yticks([]) +plt.xlim(-0.25, 10*np.pi+.25) +plt.ylim(-1.5, 1.5) +plt.tight_layout() +plt.savefig("../figures/tip-multiline.pdf", dpi=100) + + +# outline +# ----------------------------------------------------------------------------- +fig = plt.figure(figsize=(2,2)) +mpl.rcParams['axes.linewidth'] = 1.5 +d = 0.01 + +ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[]) + +np.random.seed(1) +Z = np.random.uniform(0,1,(8,8)) +cmap = plt.get_cmap("Oranges") +ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) + +text = ax.text(0.5, 0.1, "Label", transform=ax.transAxes, + color=cmap(0.9), size=32, weight="bold", ha="center", va="bottom") +text.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), + path_effects.Normal()]) +plt.savefig("../figures/tip-outline.pdf") + + +# transparency +# ----------------------------------------------------------------------------- +mpl.rc('axes', linewidth=1.5) + +fig = plt.figure(figsize=(2, 2), dpi=100) +margin = 0.01 +ax = fig.add_axes([margin, margin, 1-2*margin, 1-2*margin]) +n = 500 +X = np.random.normal(0, 0.25, n) +Y = np.random.normal(0, 0.25, n) +ax.scatter(X, Y, s=50, c="k", lw=2) +ax.scatter(X, Y, s=50, c="w", lw=0) +ax.scatter(X, Y, s=40, c="C1", lw=0, alpha=0.1) + +ax.set_xlim([-1, 1]), ax.set_xticks([]), +ax.set_ylim([-1, 1]), ax.set_yticks([]) +plt.savefig("../figures/tip-transparency.pdf") From 03e015b51e4c93f7a94097f540a2f0ceee9d8d67 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Wed, 17 Nov 2021 22:53:25 -0800 Subject: [PATCH 2/5] refactor tips --- scripts/tips.py | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/scripts/tips.py b/scripts/tips.py index 69574ee..d8cb1c9 100644 --- a/scripts/tips.py +++ b/scripts/tips.py @@ -7,17 +7,19 @@ import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt -import matplotlib.ticker as ticker import matplotlib.patheffects as path_effects +mpl.rcParams['axes.linewidth'] = 1.5 + +margin = 0.01 +rect = [margin, margin, 1-2*margin, 1-2*margin] + + # color range # ----------------------------------------------------------------------------- fig = plt.figure(figsize=(2,2)) -mpl.rcParams['axes.linewidth'] = 1.5 -d = 0.01 - -ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[]) +ax = fig.add_axes(rect, xticks=[], yticks=[]) X = np.random.seed(1) X = np.random.randn(1000, 4) @@ -31,9 +33,7 @@ # colorbar # ----------------------------------------------------------------------------- fig = plt.figure(figsize=(2.15,2)) -mpl.rcParams['axes.linewidth'] = 1.5 -d = 0.01 -ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[]) +ax = fig.add_axes(rect, xticks=[], yticks=[]) np.random.seed(1) Z = np.random.uniform(0,1,(8,8)) @@ -62,13 +62,9 @@ # dual axis # ----------------------------------------------------------------------------- -mpl.rcParams['axes.linewidth'] = 1.5 - - fig = plt.figure(figsize=(2,2)) -d = 0.01 -ax1 = fig.add_axes([d,d,1-2*d,1-2*d], label="cartesian") -ax2 = fig.add_axes([d,d,1-2*d,1-2*d], projection="polar", label="polar") +ax1 = fig.add_axes(rect, label="cartesian") +ax2 = fig.add_axes(rect, projection="polar", label="polar") ax1.set_xticks([]) #np.linspace(0.0, 0.4, 5)) ax1.set_yticks([]) #np.linspace(0.0, 1.0, 11)) @@ -91,9 +87,10 @@ def setup(ax): ax.spines['right'].set_color('none') ax.spines['left'].set_color('none') - ax.yaxis.set_major_locator(ticker.NullLocator()) + ax.yaxis.set_major_locator(mpl.ticker.NullLocator()) ax.spines['top'].set_color('none') + ax.spines['bottom'].set_linewidth(1) ax.spines['bottom'].set_position("center") ax.xaxis.set_ticks_position('bottom') @@ -114,10 +111,10 @@ def setup(ax): ax = plt.subplot(n, 1, 1) ax.tick_params(axis='both', which='minor', labelsize=6) setup(ax) -ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0)) -ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.2)) -ax.xaxis.set_major_formatter(ticker.ScalarFormatter()) -ax.xaxis.set_minor_formatter(ticker.ScalarFormatter()) +ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(1.0)) +ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(0.2)) +ax.xaxis.set_major_formatter(mpl.ticker.ScalarFormatter()) +ax.xaxis.set_minor_formatter(mpl.ticker.ScalarFormatter()) ax.tick_params(axis='x', which='minor', rotation=0) for tick in ax.get_xticklabels(which='both'): @@ -169,8 +166,6 @@ def setup(ax): # multiline # ----------------------------------------------------------------------------- -mpl.rcParams['axes.linewidth'] = 1.5 - fig = plt.figure(figsize=(8,1.5)) dx,dy = 0.0025, 0.01 ax = fig.add_axes([dx, dy, 1-2*dx, 1-2*dy], frameon=False) @@ -190,10 +185,8 @@ def setup(ax): # outline # ----------------------------------------------------------------------------- fig = plt.figure(figsize=(2,2)) -mpl.rcParams['axes.linewidth'] = 1.5 -d = 0.01 -ax = fig.add_axes([d,d,1-2*d,1-2*d], xticks=[], yticks=[]) +ax = fig.add_axes(rect, xticks=[], yticks=[]) np.random.seed(1) Z = np.random.uniform(0,1,(8,8)) @@ -209,12 +202,10 @@ def setup(ax): # transparency # ----------------------------------------------------------------------------- -mpl.rc('axes', linewidth=1.5) - fig = plt.figure(figsize=(2, 2), dpi=100) -margin = 0.01 -ax = fig.add_axes([margin, margin, 1-2*margin, 1-2*margin]) +ax = fig.add_axes(rect) n = 500 +np.random.seed(5) X = np.random.normal(0, 0.25, n) Y = np.random.normal(0, 0.25, n) ax.scatter(X, Y, s=50, c="k", lw=2) From 7d9ab774594c531f89aaaa78d5900d239a7869e2 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Thu, 18 Nov 2021 00:22:44 -0800 Subject: [PATCH 3/5] fix whitespace and other formatting --- scripts/tips.py | 56 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/scripts/tips.py b/scripts/tips.py index d8cb1c9..2ac9878 100644 --- a/scripts/tips.py +++ b/scripts/tips.py @@ -18,13 +18,13 @@ # color range # ----------------------------------------------------------------------------- -fig = plt.figure(figsize=(2,2)) +fig = plt.figure(figsize=(2, 2)) ax = fig.add_axes(rect, xticks=[], yticks=[]) -X = np.random.seed(1) +np.random.seed(1) X = np.random.randn(1000, 4) cmap = plt.get_cmap("Oranges") -colors = [cmap(i) for i in [.1,.3,.5,.7]] +colors = [cmap(i) for i in [0.1, 0.3, 0.5, 0.7]] ax.hist(X, 2, density=True, histtype='bar', color=colors) plt.savefig("../figures/tip-color-range.pdf") @@ -32,11 +32,11 @@ # colorbar # ----------------------------------------------------------------------------- -fig = plt.figure(figsize=(2.15,2)) +fig = plt.figure(figsize=(2.15, 2)) ax = fig.add_axes(rect, xticks=[], yticks=[]) np.random.seed(1) -Z = np.random.uniform(0,1,(8,8)) +Z = np.random.uniform(0, 1, (8, 8)) cmap = plt.get_cmap("Oranges") im = ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) cb = fig.colorbar(im, fraction=0.046, pad=0.04) @@ -47,10 +47,11 @@ # dotted # ----------------------------------------------------------------------------- -fig = plt.figure(figsize=(5,.25)) +fig = plt.figure(figsize=(5, 0.25)) -ax = fig.add_axes([0,0,1,1], frameon=False, - xticks=[], yticks=[], xlim=[0,1], ylim=[-.5,1.5]) +ax = fig.add_axes( + [0, 0, 1, 1], frameon=False, xticks=[], yticks=[], xlim=[0, 1], ylim=[-0.5, 1.5] +) epsilon=1e-12 plt.plot([0,1], [0,0], "black", clip_on=False, lw=8, @@ -62,16 +63,16 @@ # dual axis # ----------------------------------------------------------------------------- -fig = plt.figure(figsize=(2,2)) +fig = plt.figure(figsize=(2, 2)) ax1 = fig.add_axes(rect, label="cartesian") ax2 = fig.add_axes(rect, projection="polar", label="polar") -ax1.set_xticks([]) #np.linspace(0.0, 0.4, 5)) -ax1.set_yticks([]) #np.linspace(0.0, 1.0, 11)) +ax1.set_xticks([]) # np.linspace(0.0, 0.4, 5)) +ax1.set_yticks([]) # np.linspace(0.0, 1.0, 11)) ax2.set_rorigin(0) ax2.set_thetamax(90) -ax2.set_ylim(0.5,1.0) +ax2.set_ylim(0.5, 1.0) ax2.set_xticks(np.linspace(0, np.pi/2, 10)) ax2.set_yticks(np.linspace(0.5, 1.0, 5)) @@ -103,7 +104,7 @@ def setup(ax): ax.patch.set_alpha(0.0) -fig = plt.figure(figsize=(5, .5)) +fig = plt.figure(figsize=(5, 0.5)) fig.patch.set_alpha(0.0) n = 1 @@ -132,12 +133,12 @@ def setup(ax): plt.rcParams['hatch.color'] = color1 plt.rcParams['hatch.linewidth'] = 8 -fig = plt.figure(figsize=(2,2)) +fig = plt.figure(figsize=(2, 2)) ax = plt.subplot() np.random.seed(123) -x1,y1 = 3*np.arange(2), np.random.randint(25,50,2) -x2,y2 = x1+1, np.random.randint(25,75,2) +x1, y1 = 3 * np.arange(2), np.random.randint(25, 50, 2) +x2, y2 = x1 + 1, np.random.randint(25, 75, 2) ax.bar(x1, y1, color=color2) for i in range(len(x1)): @@ -145,14 +146,14 @@ def setup(ax): fontsize="x-small", color=color2, textcoords="offset points", va="bottom", ha="center") -ax.bar(x2, y2, color=color2, hatch="/" ) +ax.bar(x2, y2, color=color2, hatch="/") for i in range(len(x2)): plt.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0,1), fontsize="x-small", color=color2, textcoords="offset points", va="bottom", ha="center") ax.set_yticks([]) -ax.set_xticks(0.5+np.arange(0,6,3)) +ax.set_xticks(0.5 + np.arange(0, 6, 3)) ax.set_xticklabels(["2018", "2019"]) ax.tick_params('x', length=0, labelsize="small", which='major') @@ -166,17 +167,17 @@ def setup(ax): # multiline # ----------------------------------------------------------------------------- -fig = plt.figure(figsize=(8,1.5)) -dx,dy = 0.0025, 0.01 +fig = plt.figure(figsize=(8, 1.5)) +dx, dy = 0.0025, 0.01 ax = fig.add_axes([dx, dy, 1-2*dx, 1-2*dy], frameon=False) -X,Y = [], [] +X, Y = [], [] for x in np.linspace(0.01, 10*np.pi-0.01, 100): - X.extend([x, x,None]) + X.extend([x, x, None]) Y.extend([0, np.sin(x), None]) print(X[:10], Y[:10]) plt.plot(X, Y, "black") plt.xticks([]), plt.yticks([]) -plt.xlim(-0.25, 10*np.pi+.25) +plt.xlim(-0.25, 10*np.pi+0.25) plt.ylim(-1.5, 1.5) plt.tight_layout() plt.savefig("../figures/tip-multiline.pdf", dpi=100) @@ -184,12 +185,11 @@ def setup(ax): # outline # ----------------------------------------------------------------------------- -fig = plt.figure(figsize=(2,2)) - +fig = plt.figure(figsize=(2, 2)) ax = fig.add_axes(rect, xticks=[], yticks=[]) np.random.seed(1) -Z = np.random.uniform(0,1,(8,8)) +Z = np.random.uniform(0, 1, (8, 8)) cmap = plt.get_cmap("Oranges") ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) @@ -208,8 +208,8 @@ def setup(ax): np.random.seed(5) X = np.random.normal(0, 0.25, n) Y = np.random.normal(0, 0.25, n) -ax.scatter(X, Y, s=50, c="k", lw=2) -ax.scatter(X, Y, s=50, c="w", lw=0) +ax.scatter(X, Y, s=50, c="k", lw=2) +ax.scatter(X, Y, s=50, c="w", lw=0) ax.scatter(X, Y, s=40, c="C1", lw=0, alpha=0.1) ax.set_xlim([-1, 1]), ax.set_xticks([]), From a5a1d75bf364891f2d179ab4c78e90a40000aa2a Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Thu, 18 Nov 2021 18:33:09 -0800 Subject: [PATCH 4/5] refactor tips further - replace calls to `plt` with `fig` or `ax` equivalents --- scripts/tips.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/scripts/tips.py b/scripts/tips.py index 2ac9878..6822e32 100644 --- a/scripts/tips.py +++ b/scripts/tips.py @@ -27,7 +27,7 @@ colors = [cmap(i) for i in [0.1, 0.3, 0.5, 0.7]] ax.hist(X, 2, density=True, histtype='bar', color=colors) -plt.savefig("../figures/tip-color-range.pdf") +fig.savefig("../figures/tip-color-range.pdf") # colorbar @@ -42,7 +42,7 @@ cb = fig.colorbar(im, fraction=0.046, pad=0.04) cb.set_ticks([]) -plt.savefig("../figures/tip-colorbar.pdf") +fig.savefig("../figures/tip-colorbar.pdf") # dotted @@ -54,11 +54,11 @@ ) epsilon=1e-12 -plt.plot([0,1], [0,0], "black", clip_on=False, lw=8, +ax.plot([0,1], [0,0], "black", clip_on=False, lw=8, ls=(.5,(epsilon, 1)), dash_capstyle="round") -plt.plot([0,1], [1,1], "black", clip_on=False, lw=8, +ax.plot([0,1], [1,1], "black", clip_on=False, lw=8, ls=(-.5,(epsilon, 2)), dash_capstyle="round") -plt.savefig("../figures/tip-dotted.pdf") +fig.savefig("../figures/tip-dotted.pdf") # dual axis @@ -79,7 +79,7 @@ ax2.set_xticklabels([]) ax2.set_yticklabels([]) -plt.savefig("../figures/tip-dual-axis.pdf") +fig.savefig("../figures/tip-dual-axis.pdf") # font family @@ -121,8 +121,8 @@ def setup(ax): for tick in ax.get_xticklabels(which='both'): tick.set_fontname("Roboto Condensed") -plt.tight_layout() -plt.savefig("../figures/tip-font-family.pdf", transparent=True) +fig.tight_layout() +fig.savefig("../figures/tip-font-family.pdf", transparent=True) # hatched @@ -130,8 +130,8 @@ def setup(ax): cmap = plt.get_cmap("Oranges") color1, color2 = cmap(0.3), cmap(0.5) -plt.rcParams['hatch.color'] = color1 -plt.rcParams['hatch.linewidth'] = 8 +mpl.rcParams['hatch.color'] = color1 +mpl.rcParams['hatch.linewidth'] = 8 fig = plt.figure(figsize=(2, 2)) ax = plt.subplot() @@ -142,13 +142,13 @@ def setup(ax): ax.bar(x1, y1, color=color2) for i in range(len(x1)): - plt.annotate("%d%%" % y1[i], (x1[i], y1[i]), xytext=(0,1), + ax.annotate("%d%%" % y1[i], (x1[i], y1[i]), xytext=(0,1), fontsize="x-small", color=color2, textcoords="offset points", va="bottom", ha="center") ax.bar(x2, y2, color=color2, hatch="/") for i in range(len(x2)): - plt.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0,1), + ax.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0,1), fontsize="x-small", color=color2, textcoords="offset points", va="bottom", ha="center") @@ -161,8 +161,8 @@ def setup(ax): ax.spines['left'].set_visible(False) ax.spines['top'].set_visible(False) -plt.tight_layout() -plt.savefig("../figures/tip-hatched.pdf") +fig.tight_layout() +fig.savefig("../figures/tip-hatched.pdf") # multiline @@ -175,12 +175,12 @@ def setup(ax): X.extend([x, x, None]) Y.extend([0, np.sin(x), None]) print(X[:10], Y[:10]) -plt.plot(X, Y, "black") -plt.xticks([]), plt.yticks([]) -plt.xlim(-0.25, 10*np.pi+0.25) -plt.ylim(-1.5, 1.5) -plt.tight_layout() -plt.savefig("../figures/tip-multiline.pdf", dpi=100) +ax.plot(X, Y, "black") +ax.set_xticks([]), ax.set_yticks([]) +ax.set_xlim(-0.25, 10*np.pi+0.25) +ax.set_ylim(-1.5, 1.5) +fig.tight_layout() +fig.savefig("../figures/tip-multiline.pdf", dpi=100) # outline @@ -197,7 +197,7 @@ def setup(ax): color=cmap(0.9), size=32, weight="bold", ha="center", va="bottom") text.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), path_effects.Normal()]) -plt.savefig("../figures/tip-outline.pdf") +fig.savefig("../figures/tip-outline.pdf") # transparency @@ -214,4 +214,4 @@ def setup(ax): ax.set_xlim([-1, 1]), ax.set_xticks([]), ax.set_ylim([-1, 1]), ax.set_yticks([]) -plt.savefig("../figures/tip-transparency.pdf") +fig.savefig("../figures/tip-transparency.pdf") From 291f79ef24dc13e4bc0abbc9b06ed53f77515911 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Mon, 22 Nov 2021 23:32:07 -0800 Subject: [PATCH 5/5] fixes for PEP-8 --- scripts/tips.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/scripts/tips.py b/scripts/tips.py index 6822e32..e8b196a 100644 --- a/scripts/tips.py +++ b/scripts/tips.py @@ -54,10 +54,10 @@ ) epsilon=1e-12 -ax.plot([0,1], [0,0], "black", clip_on=False, lw=8, - ls=(.5,(epsilon, 1)), dash_capstyle="round") -ax.plot([0,1], [1,1], "black", clip_on=False, lw=8, - ls=(-.5,(epsilon, 2)), dash_capstyle="round") +ax.plot([0, 1], [0, 0], "black", clip_on=False, lw=8, + ls=(.5, (epsilon, 1)), dash_capstyle="round") +ax.plot([0, 1], [1, 1], "black", clip_on=False, lw=8, + ls=(-.5, (epsilon, 2)), dash_capstyle="round") fig.savefig("../figures/tip-dotted.pdf") @@ -142,15 +142,15 @@ def setup(ax): ax.bar(x1, y1, color=color2) for i in range(len(x1)): - ax.annotate("%d%%" % y1[i], (x1[i], y1[i]), xytext=(0,1), - fontsize="x-small", color=color2, - textcoords="offset points", va="bottom", ha="center") + ax.annotate("%d%%" % y1[i], (x1[i], y1[i]), xytext=(0, 1), + fontsize="x-small", color=color2, + textcoords="offset points", va="bottom", ha="center") ax.bar(x2, y2, color=color2, hatch="/") for i in range(len(x2)): - ax.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0,1), - fontsize="x-small", color=color2, - textcoords="offset points", va="bottom", ha="center") + ax.annotate("%d%%" % y2[i], (x2[i], y2[i]), xytext=(0, 1), + fontsize="x-small", color=color2, + textcoords="offset points", va="bottom", ha="center") ax.set_yticks([]) ax.set_xticks(0.5 + np.arange(0, 6, 3)) @@ -194,7 +194,7 @@ def setup(ax): ax.imshow(Z, interpolation="nearest", cmap=cmap, vmin=0, vmax=2) text = ax.text(0.5, 0.1, "Label", transform=ax.transAxes, - color=cmap(0.9), size=32, weight="bold", ha="center", va="bottom") + color=cmap(0.9), size=32, weight="bold", ha="center", va="bottom") text.set_path_effects([path_effects.Stroke(linewidth=5, foreground='white'), path_effects.Normal()]) fig.savefig("../figures/tip-outline.pdf")