From aec0df4f91adced09f977bfc35b5635c37531233 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Sat, 23 Oct 2021 13:29:56 -0700 Subject: [PATCH 1/7] make cheatsheets column width exactly 57 mm Notes: The A4 long dimension is 297 mm. With 2.5 mm left and right margins and 7/4 mm spacing between the 5 columns (4 gaps), this gives (297 - 2.5*2 - 4*7/4) / 5 = 57 mm wide columns. --- cheatsheets.tex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cheatsheets.tex b/cheatsheets.tex index d1b46f4..c75b7d8 100644 --- a/cheatsheets.tex +++ b/cheatsheets.tex @@ -104,7 +104,7 @@ \setlength\parindent{0pt} \setlength{\tabcolsep}{2pt} \baselineskip=0pt -\setlength\columnsep{0.5em} +\setlength\columnsep{1.75mm} % --- Macros ------------------------------------------------------------------ From b20eb400d417933da0f381c1a7816923ef9bc017 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Sun, 14 Nov 2021 10:54:51 -0800 Subject: [PATCH 2/7] start refactoring using style sheets --- scripts/advanced-plots.py | 14 ++++++++------ scripts/basic-plots.py | 30 ++++++++++++++++-------------- styles/plotlet.mplstyle | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 styles/plotlet.mplstyle diff --git a/scripts/advanced-plots.py b/scripts/advanced-plots.py index 025378a..73f3527 100644 --- a/scripts/advanced-plots.py +++ b/scripts/advanced-plots.py @@ -3,15 +3,17 @@ # Released under the BSD License # ----------------------------------------------------------------------------- -# Scripts to generate all the basic plots +# Script to generate all the advanced plots +import pathlib + import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt -fig = plt.figure(figsize=(0.4, 0.4)) -mpl.rcParams['axes.linewidth'] = 0.5 -mpl.rcParams['xtick.major.size'] = 0.0 -mpl.rcParams['ytick.major.size'] = 0.0 + +mpl.style.use(pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle') + +fig = plt.figure() d = 0.01 ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) @@ -19,7 +21,7 @@ # ----------------------------------------------------------------------------- X = np.linspace(0, 10, 16) Y = 4 + 2*np.sin(2*X) -ax.step(X, Y, color="C1", linewidth=0.75) +ax.step(X, Y, color="C1") ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.grid(linewidth=0.125) diff --git a/scripts/basic-plots.py b/scripts/basic-plots.py index d7a0c35..1fbe48f 100644 --- a/scripts/basic-plots.py +++ b/scripts/basic-plots.py @@ -3,15 +3,17 @@ # Released under the BSD License # ----------------------------------------------------------------------------- -# Scripts to generate all the basic plots +# Script to generate all the basic plots +import pathlib + import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt -fig = plt.figure(figsize=(0.4, 0.4)) -mpl.rcParams['axes.linewidth'] = 0.5 -mpl.rcParams['xtick.major.size'] = 0.0 -mpl.rcParams['ytick.major.size'] = 0.0 + +mpl.style.use(pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle') + +fig = plt.figure() d = 0.01 ax = fig.add_axes([d, d, 1 - 2 * d, 1 - 2 * d]) @@ -19,10 +21,10 @@ # ----------------------------------------------------------------------------- X = np.linspace(0, 10, 100) Y = 4 + 2*np.sin(2*X) -ax.plot(X, Y, color="C1", linewidth=0.75) +ax.plot(X, Y, color="C1") ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) +ax.grid() plt.savefig("../figures/basic-plot.pdf") ax.clear() @@ -30,11 +32,11 @@ # ----------------------------------------------------------------------------- X = np.linspace(0, 10, 100) -Y = 4 + 2 * np.sin(2 * X) -ax.plot(X, Y, color="black", linewidth=0.75) +Y = 4 + 2*np.sin(2*X) +ax.plot(X, Y, color="black") ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) +ax.grid() plt.savefig("../figures/basic-plot-color.pdf") ax.clear() @@ -47,7 +49,7 @@ edgecolor="white", facecolor="C1", linewidth=0.25) ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) +ax.grid() plt.savefig("../figures/basic-scatter.pdf") ax.clear() @@ -61,7 +63,7 @@ ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) +ax.grid() plt.savefig("../figures/basic-bar.pdf") ax.clear() @@ -142,7 +144,7 @@ ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125, color="0.75") +ax.grid(color="0.75") plt.savefig("../figures/basic-fill.pdf") ax.clear() @@ -157,6 +159,6 @@ ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125, color="0.75") +ax.grid(color="0.75") plt.savefig("../figures/basic-quiver.pdf") ax.clear() diff --git a/styles/plotlet.mplstyle b/styles/plotlet.mplstyle new file mode 100644 index 0000000..b86d240 --- /dev/null +++ b/styles/plotlet.mplstyle @@ -0,0 +1,15 @@ +figure.figsize: 0.4, 0.4 + +figure.constrained_layout.h_pad: 0.01 +figure.constrained_layout.w_pad: 0.01 +figure.constrained_layout.hspace: 0.01 +figure.constrained_layout.wspace: 0.01 + +axes.linewidth: 0.5 + +grid.linewidth: 0.2 + +lines.linewidth: 0.75 + +xtick.major.size: 0.0 +ytick.major.size: 0.0 From 5ea8b48b8d8fdd0ce2bb63f9d788566166b14b98 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Sat, 23 Oct 2021 14:15:56 -0700 Subject: [PATCH 3/7] continue refactoring using style sheets - add base style with constrained_layout enabled - add style for the ticks examples - add style for a grid of figures - resize figures to a maximum width of 57 mm - small tweaks to arrow connections to accommodate change in figure size --- scripts/advanced-plots.py | 6 ++- scripts/annotation-arrow-styles.py | 19 ++++----- scripts/annotation-connection-styles.py | 21 ++++++---- scripts/basic-plots.py | 6 ++- scripts/interpolations.py | 20 +++++++--- scripts/projections.py | 27 +++++++------ scripts/tick-formatters.py | 49 ++++++++++------------- scripts/tick-locators.py | 53 ++++++++++--------------- styles/base.mplstyle | 5 +++ styles/plotlet-grid.mplstyle | 10 +++++ styles/ticks.mplstyle | 17 ++++++++ 11 files changed, 136 insertions(+), 97 deletions(-) create mode 100644 styles/base.mplstyle create mode 100644 styles/plotlet-grid.mplstyle create mode 100644 styles/ticks.mplstyle diff --git a/scripts/advanced-plots.py b/scripts/advanced-plots.py index 73f3527..3ad36c0 100644 --- a/scripts/advanced-plots.py +++ b/scripts/advanced-plots.py @@ -11,7 +11,11 @@ import matplotlib.pyplot as plt -mpl.style.use(pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle') +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', +]) + fig = plt.figure() d = 0.01 diff --git a/scripts/annotation-arrow-styles.py b/scripts/annotation-arrow-styles.py index 648a4c4..046c3e1 100644 --- a/scripts/annotation-arrow-styles.py +++ b/scripts/annotation-arrow-styles.py @@ -1,16 +1,17 @@ +import pathlib + +import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.patches as mpatches -styles = mpatches.ArrowStyle.get_styles() - -def demo_con_style(ax, connectionstyle): - ax.text(.05, .95, connectionstyle.replace(",", ",\n"), - family="Source Code Pro", - transform=ax.transAxes, ha="left", va="top", size="x-small") +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle', +]) -(fig, axes) = plt.subplots(4, 4, figsize=(4, 2.5), frameon=False) +(fig, axes) = plt.subplots(4, 4, figsize=(5.7/2.54, 1.5), frameon=True) for ax in axes.flatten(): ax.axis("off") for i, (ax, style) in enumerate(zip(axes.flatten(), mpatches.ArrowStyle.get_styles())): @@ -21,13 +22,13 @@ def demo_con_style(ax, connectionstyle): xy=(x0, y0), xycoords='data', xytext=(x1, y1), textcoords='data', arrowprops=dict(arrowstyle=style, + mutation_scale=10, color="black", shrinkA=5, shrinkB=5, patchA=None, patchB=None, connectionstyle="arc3,rad=0")) ax.text( (x1+x0)/2, y0-0.2, style, transform=ax.transAxes, - family="Source Code Pro", ha="center", va="top") + ha="center", va="top", fontsize=8) plt.savefig("../figures/annotation-arrow-styles.pdf") -# plt.show() diff --git a/scripts/annotation-connection-styles.py b/scripts/annotation-connection-styles.py index 64fa300..4382cbb 100644 --- a/scripts/annotation-connection-styles.py +++ b/scripts/annotation-connection-styles.py @@ -1,6 +1,15 @@ +import pathlib + +import matplotlib as mpl import matplotlib.pyplot as plt +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle', +]) + + def demo_con_style(ax, connectionstyle): x1, y1 = 0.3, 0.2 x2, y2 = 0.8, 0.6 @@ -9,29 +18,27 @@ def demo_con_style(ax, connectionstyle): xy=(x1, y1), xycoords='data', xytext=(x2, y2), textcoords='data', arrowprops=dict(arrowstyle="->", color="0.5", - shrinkA=5, shrinkB=5, + shrinkA=2, shrinkB=2, patchA=None, patchB=None, connectionstyle=connectionstyle), ) ax.text(.05, .95, connectionstyle.replace(",", ",\n"), - family="Source Code Pro", - transform=ax.transAxes, ha="left", va="top", size="x-small") + transform=ax.transAxes, ha="left", va="top", size=4) -fig, axs = plt.subplots(3, 3, figsize=(5, 5)) +fig, axs = plt.subplots(3, 3, figsize=(5.7/2.54, 5.7/2.54)) demo_con_style(axs[0, 0], "arc3,rad=0") demo_con_style(axs[0, 1], "arc3,rad=0.3") demo_con_style(axs[0, 2], "angle3,angleA=0,angleB=90") demo_con_style(axs[1, 0], "angle,angleA=-90,angleB=180,rad=0") -demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=25") -demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=40,rad=0") +demo_con_style(axs[1, 1], "angle,angleA=-90,angleB=180,rad=10") +demo_con_style(axs[1, 2], "arc,angleA=-90,angleB=0,armA=0,armB=20,rad=0") demo_con_style(axs[2, 0], "bar,fraction=0.3") demo_con_style(axs[2, 1], "bar,fraction=-0.3") demo_con_style(axs[2, 2], "bar,angle=180,fraction=-0.2") for ax in axs.flat: ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1) -fig.tight_layout(pad=0.2) plt.savefig("../figures/annotation-connection-styles.pdf") # plt.show() diff --git a/scripts/basic-plots.py b/scripts/basic-plots.py index 1fbe48f..64e07fc 100644 --- a/scripts/basic-plots.py +++ b/scripts/basic-plots.py @@ -11,7 +11,11 @@ import matplotlib.pyplot as plt -mpl.style.use(pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle') +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', +]) + fig = plt.figure() d = 0.01 diff --git a/scripts/interpolations.py b/scripts/interpolations.py index 2a135ef..783d26c 100644 --- a/scripts/interpolations.py +++ b/scripts/interpolations.py @@ -1,6 +1,16 @@ +import pathlib + +import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle', +]) + + methods = [None, 'none', 'nearest', 'bilinear', 'bicubic', 'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser', 'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc', 'lanczos'] @@ -9,14 +19,14 @@ Z = np.random.uniform(0, 1, (3, 3)) -fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(4.5, 9), +fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(5.7/2.54, 5.7/2.54*2), + # fig, axs = plt.subplots(nrows=6, ncols=3, figsize=(4.5,9), subplot_kw={'xticks': [], 'yticks': []}) for ax, interp_method in zip(axs.flat, methods): ax.imshow(Z, interpolation=interp_method, cmap='viridis', - extent=[0, 9, 0, 9], rasterized=True) - ax.text(4.5, 1, str(interp_method), weight="bold", color="white", size=12, - transform=ax.transData, ha="center", va="center") + extent=[0, 1, 0, 1], rasterized=True) + ax.text(0.5, 0.1, str(interp_method), weight="bold", color="white", size=6, + ha="center", va="center") -plt.tight_layout() plt.savefig("../figures/interpolations.pdf", dpi=600) # plt.show() diff --git a/scripts/projections.py b/scripts/projections.py index a262050..fde46cb 100644 --- a/scripts/projections.py +++ b/scripts/projections.py @@ -2,12 +2,20 @@ # Matplotlib cheat sheet # Released under the BSD License # ----------------------------------------------------------------------------- +import pathlib + import numpy as np import cartopy import matplotlib as mpl import matplotlib.pyplot as plt +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', +]) + + CARTOPY_SOURCE_TEMPLATE = 'https://naturalearth.s3.amazonaws.com/{resolution}_{category}/ne_{resolution}_{name}.zip' @@ -21,26 +29,22 @@ # Polar plot # ----------------------------------------------------------------------------- -mpl.rc('axes', linewidth=4.0) -fig = plt.figure(figsize=(4, 4)) -b = 0.025 -ax = fig.add_axes([b, b, 1-2*b, 1-2*b], projection="polar") +(fig, ax) = plt.subplots(subplot_kw={'projection': 'polar'}) T = np.linspace(0, 3*2*np.pi, 500) R = np.linspace(0, 2, len(T)) -ax.plot(T, R, color="C1", linewidth=6) +ax.plot(T, R, color="C1") ax.set_xticks(np.linspace(0, 2*np.pi, 2*8)) ax.set_xticklabels([]) ax.set_yticks(np.linspace(0, 2, 8)) ax.set_yticklabels([]) ax.set_ylim(0, 2) -ax.grid(linewidth=1) +ax.grid(linewidth=0.2) plt.savefig("../figures/projection-polar.pdf") fig.clear() # 3D plot # ----------------------------------------------------------------------------- -mpl.rc('axes', linewidth=2.0) -ax = fig.add_axes([0, .1, 1, .9], projection="3d") +(fig, ax) = plt.subplots(subplot_kw={'projection': '3d'}) r = np.linspace(0, 1.25, 50) p = np.linspace(0, 2*np.pi, 50) R, P = np.meshgrid(r, p) @@ -58,10 +62,9 @@ # Cartopy plot # ----------------------------------------------------------------------------- -mpl.rc('axes', linewidth=3.0) -b = 0.025 -ax = fig.add_axes([b, b, 1-2*b, 1-2*b], frameon=False, - projection=cartopy.crs.Orthographic()) +fig = plt.figure() +ax = fig.add_subplot(frameon=False, + projection=cartopy.crs.Orthographic()) ax.add_feature(cartopy.feature.LAND, zorder=0, facecolor="C1", edgecolor="0.0", linewidth=0) plt.savefig("../figures/projection-cartopy.pdf") diff --git a/scripts/tick-formatters.py b/scripts/tick-formatters.py index fe5bb40..c652846 100644 --- a/scripts/tick-formatters.py +++ b/scripts/tick-formatters.py @@ -3,33 +3,35 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- +import pathlib + +import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.ticker as ticker + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/ticks.mplstyle', +]) + + # Setup a plot such that only the bottom spine is shown def setup(ax): """Set up Axes with just an x-Axis.""" - 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.xaxis.set_ticks_position('bottom') - ax.tick_params(which='major', width=1.00, length=5) - ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) ax.set_xlim(0, 5) ax.set_ylim(0, 1) ax.patch.set_alpha(0.0) -fig = plt.figure(figsize=(8, 5)) +fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54)) fig.patch.set_alpha(0.0) n = 7 -fontsize = 18 -family = "Source Code Pro" - # Null formatter ax = fig.add_subplot(n, 1, 1) setup(ax) @@ -37,8 +39,7 @@ def setup(ax): ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(ticker.NullFormatter()) -ax.text(0.0, 0.1, "ticker.NullFormatter()", family=family, - fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.NullFormatter()", transform=ax.transAxes) # Fixed formatter ax = fig.add_subplot(n, 1, 2) @@ -47,8 +48,7 @@ def setup(ax): ax.xaxis.set_major_locator(ticker.FixedLocator(range(6))) majors = ["zero", "one", "two", "three", "four", "five"] ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors)) -ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", transform=ax.transAxes) # FuncFormatter can be used as a decorator @@ -63,8 +63,7 @@ def major_formatter(x, pos): ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(major_formatter) -ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', transform=ax.transAxes) # FormatStr formatter @@ -73,8 +72,7 @@ def major_formatter(x, pos): ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<")) -ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", transform=ax.transAxes) # Scalar formatter ax = fig.add_subplot(n, 1, 5) @@ -82,8 +80,7 @@ def major_formatter(x, pos): ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) -ax.text(0.0, 0.1, "ticker.ScalarFormatter()", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.ScalarFormatter()", transform=ax.transAxes) # StrMethod formatter ax = fig.add_subplot(n, 1, 6) @@ -91,8 +88,7 @@ def major_formatter(x, pos): ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}")) -ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", transform=ax.transAxes) # Percent formatter ax = fig.add_subplot(n, 1, 7) @@ -100,12 +96,7 @@ def major_formatter(x, pos): ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5)) -ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", - family=family, fontsize=fontsize, transform=ax.transAxes) - -# Push the top of the top axes outside the figure because we only show the -# bottom spine. -fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) +ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", transform=ax.transAxes) -plt.savefig("../figures/tick-formatters.pdf", transparent=True) +plt.savefig("../figures/tick-formatters.pdf") # plt.show() diff --git a/scripts/tick-locators.py b/scripts/tick-locators.py index 276f8a3..c6521c0 100644 --- a/scripts/tick-locators.py +++ b/scripts/tick-locators.py @@ -3,50 +3,47 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- +import pathlib + import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.ticker as ticker + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/ticks.mplstyle', +]) + + # 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.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=(8, 5)) +fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54)) fig.patch.set_alpha(0.0) n = 8 -fontsize = 18 -family = "Source Code Pro" - # Null Locator ax = plt.subplot(n, 1, 1) setup(ax) ax.xaxis.set_major_locator(ticker.NullLocator()) ax.xaxis.set_minor_locator(ticker.NullLocator()) -ax.text(0.0, 0.1, "ticker.NullLocator()", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.NullLocator()", transform=ax.transAxes) # Multiple Locator ax = plt.subplot(n, 1, 2) setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) -ax.text(0.0, 0.1, "ticker.MultipleLocator(0.5)", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.MultipleLocator(0.5)", transform=ax.transAxes) # Fixed Locator ax = plt.subplot(n, 1, 3) @@ -55,40 +52,35 @@ def setup(ax): ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) minors = np.linspace(0, 1, 11)[1:-1] ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) -ax.text(0.0, 0.1, "ticker.FixedLocator([0, 1, 5])", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.FixedLocator([0, 1, 5])", transform=ax.transAxes) # Linear Locator ax = plt.subplot(n, 1, 4) setup(ax) ax.xaxis.set_major_locator(ticker.LinearLocator(3)) ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) -ax.text(0.0, 0.1, "ticker.LinearLocator(numticks=3)", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.LinearLocator(numticks=3)", transform=ax.transAxes) # Index Locator ax = plt.subplot(n, 1, 5) setup(ax) ax.plot(range(0, 5), [0]*5, color='white') ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) -ax.text(0.0, 0.1, "ticker.IndexLocator(base=0.5, offset=0.25)", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.IndexLocator(base=0.5, offset=0.25)", transform=ax.transAxes) # Auto Locator ax = plt.subplot(n, 1, 6) setup(ax) ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) -ax.text(0.0, 0.1, "ticker.AutoLocator()", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.AutoLocator()", transform=ax.transAxes) # MaxN Locator ax = plt.subplot(n, 1, 7) setup(ax) ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) -ax.text(0.0, 0.1, "ticker.MaxNLocator(n=4)", - family=family, fontsize=fontsize, transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.MaxNLocator(n=4)", transform=ax.transAxes) # Log Locator ax = plt.subplot(n, 1, 8) @@ -96,12 +88,7 @@ def setup(ax): ax.set_xlim(10**3, 10**10) ax.set_xscale('log') ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) -ax.text(0.0, 0.1, "ticker.LogLocator(base=10, numticks=15)", - family=family, fontsize=fontsize, transform=ax.transAxes) - -# Push the top of the top axes outside the figure because we only show the -# bottom spine. -plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) +ax.text(0.0, 0.1, "ticker.LogLocator(base=10, numticks=15)", transform=ax.transAxes) -plt.savefig("../figures/tick-locators.pdf", transparent=True) +plt.savefig("../figures/tick-locators.pdf") # plt.show() diff --git a/styles/base.mplstyle b/styles/base.mplstyle new file mode 100644 index 0000000..77cbdce --- /dev/null +++ b/styles/base.mplstyle @@ -0,0 +1,5 @@ +figure.constrained_layout.use: True +figure.constrained_layout.h_pad: 0 +figure.constrained_layout.w_pad: 0 +figure.constrained_layout.hspace: 0 +figure.constrained_layout.wspace: 0 diff --git a/styles/plotlet-grid.mplstyle b/styles/plotlet-grid.mplstyle new file mode 100644 index 0000000..c4133c3 --- /dev/null +++ b/styles/plotlet-grid.mplstyle @@ -0,0 +1,10 @@ +figure.constrained_layout.h_pad: 0.04 +figure.constrained_layout.w_pad: 0.04 +figure.constrained_layout.hspace: 0.04 +figure.constrained_layout.wspace: 0.04 + +font.family: Source Code Pro +font.size: 5 + +xtick.major.size: 0.0 +ytick.major.size: 0.0 diff --git a/styles/ticks.mplstyle b/styles/ticks.mplstyle new file mode 100644 index 0000000..a2fb14f --- /dev/null +++ b/styles/ticks.mplstyle @@ -0,0 +1,17 @@ +savefig.transparent: True + +font.family: Source Code Pro +font.size: 5 + +axes.linewidth: 0.5 +axes.spines.right: False +axes.spines.top: False +axes.spines.left: False +axes.spines.bottom: True + +xtick.labelsize: 3 +xtick.major.pad: 1 +xtick.major.width: 0.2 +xtick.major.size: 2 +xtick.minor.width: 0.1 +xtick.minor.size: 1 From aeeeb3b11457bd6bee82a056f56565451f38fa36 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Tue, 23 Nov 2021 21:19:21 -0800 Subject: [PATCH 4/7] finish (for now) refactoring using style sheets - diminish scope of pdfcrop use - migrate more scripts to use the base mplstyle - continue migration to subplots usage - refactor to use common subplot_kw where possible - add style sheet for the sine plots - make more DRY --- Makefile | 14 ++-- scripts/adjustements.py | 71 ++++++++++++------- scripts/advanced-plots.py | 76 ++++++++------------ scripts/anatomy.py | 12 +++- scripts/basic-plots.py | 74 +++++++------------ scripts/colornames.py | 24 +++---- scripts/extents.py | 24 ++++--- scripts/legend.py | 27 +++++-- scripts/plot-variations.py | 118 +++++++++++++------------------ scripts/sine.py | 97 ++++++++++++------------- scripts/tick-formatters.py | 74 ++++++++----------- scripts/tick-locators.py | 67 ++++++++---------- scripts/tick-multiple-locator.py | 46 ++++-------- styles/base.mplstyle | 8 +-- styles/plotlet-grid.mplstyle | 7 +- styles/plotlet.mplstyle | 6 ++ styles/sine-plot.mplstyle | 24 +++++++ styles/ticks.mplstyle | 11 +-- 18 files changed, 378 insertions(+), 402 deletions(-) create mode 100644 styles/sine-plot.mplstyle diff --git a/Makefile b/Makefile index 530fc09..f193644 100644 --- a/Makefile +++ b/Makefile @@ -14,10 +14,16 @@ logos: figures: # generate the figures cd scripts && for script in *.py; do echo $$script; MPLBACKEND="agg" python $$script; done - # crop the figures - cd figures && for figure in *.pdf; do echo $$figure; pdfcrop $$figure $$figure; done - # regenerate some figures that should not be cropped - cd scripts && MPLBACKEND="agg" python styles.py + # crop some of the figures + cd figures && pdfcrop adjustments.pdf adjustments.pdf + cd figures && pdfcrop annotate.pdf annotate.pdf + cd figures && pdfcrop anatomy.pdf anatomy.pdf + cd figures && pdfcrop colornames.pdf colornames.pdf + cd figures && pdfcrop fonts.pdf fonts.pdf + cd figures && pdfcrop markers.pdf markers.pdf + cd figures && pdfcrop text-alignments.pdf text-alignments.pdf + cd figures && pdfcrop tip-font-family.pdf tip-font-family.pdf + cd figures && pdfcrop tip-hatched.pdf tip-hatched.pdf .PHONY: cheatsheets cheatsheets: diff --git a/scripts/adjustements.py b/scripts/adjustements.py index 807474f..f433a69 100644 --- a/scripts/adjustements.py +++ b/scripts/adjustements.py @@ -2,62 +2,82 @@ # Matplotlib cheat sheet # Released under the BSD License # ----------------------------------------------------------------------------- +import pathlib import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection -fig = plt.figure(figsize=(4.25, 4.25 * 95/115)) -ax = fig.add_axes([0, 0, 1, 1], frameon=False, aspect=1, - xlim=(0-5, 100+10), ylim=(-10, 80+5), xticks=[], yticks=[]) +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', +]) +mpl.rc('font', size=4) +mpl.rc('lines', linewidth=0.5) +mpl.rc('patch', linewidth=0.5) + + +subplots_kw = dict( + figsize=(5.7/2.54, 5.7/2.54 * 95/115), + subplot_kw=dict( + frameon=False, + aspect=1, + xlim=(0-5, 100+10), + ylim=(-10, 80+5), + xticks=[], + yticks=[], + ), +) + +(fig, ax) = plt.subplots(**subplots_kw) box = mpatches.FancyBboxPatch( (0, 0), 100, 83, mpatches.BoxStyle("Round", pad=0, rounding_size=2), - linewidth=1., facecolor="0.9", edgecolor="black") + facecolor="0.9", edgecolor="black") ax.add_artist(box) box = mpatches.FancyBboxPatch( (0, 0), 100, 75, mpatches.BoxStyle("Round", pad=0, rounding_size=0), - linewidth=1., facecolor="white", edgecolor="black") + facecolor="white", edgecolor="black") ax.add_artist(box) box = mpatches.Rectangle( (5, 5), 45, 30, zorder=10, - linewidth=1.0, facecolor="white", edgecolor="black") + facecolor="white", edgecolor="black") ax.add_artist(box) box = mpatches.Rectangle( (5, 40), 45, 30, zorder=10, - linewidth=1.0, facecolor="white", edgecolor="black") + facecolor="white", edgecolor="black") ax.add_artist(box) box = mpatches.Rectangle( (55, 5), 40, 65, zorder=10, - linewidth=1.0, facecolor="white", edgecolor="black") + facecolor="white", edgecolor="black") ax.add_artist(box) # Window button X, Y = [5, 10, 15], [79, 79, 79] -plt.scatter(X, Y, s=75, zorder=10, - edgecolor="black", facecolor="white", linewidth=1) +plt.scatter(X, Y, s=20, zorder=10, + edgecolor="black", facecolor="white") # Window size extension X, Y = [0, 0], [0, -8] -plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) +plt.plot(X, Y, color="black", linestyle=":", clip_on=False) X, Y = [100, 100], [0, -8] -plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) +plt.plot(X, Y, color="black", linestyle=":", clip_on=False) X, Y = [100, 108], [0, 0] -plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) +plt.plot(X, Y, color="black", linestyle=":", clip_on=False) X, Y = [100, 108], [75, 75] -plt.plot(X, Y, color="black", linestyle=":", linewidth=1, clip_on=False) +plt.plot(X, Y, color="black", linestyle=":", clip_on=False) def ext_arrow(p0, p1, p2, p3): @@ -69,7 +89,7 @@ def ext_arrow(p0, p1, p2, p3): ax.arrow(*p3, *(p2-p3), zorder=20, linewidth=0, length_includes_head=True, width=.4, head_width=2, head_length=2, color="black") - plt.plot([p1[0], p2[0]], [p1[1], p2[1]], linewidth=.9, color="black") + plt.plot([p1[0], p2[0]], [p1[1], p2[1]], linewidth=.5, color="black") def int_arrow(p0, p1): @@ -85,45 +105,44 @@ def int_arrow(p0, p1): x = 0 y = 10 ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) ) -ax.text(x+9.5, y, "left", ha="left", va="center", size="x-small", zorder=20) +ax.text(x+9.5, y, "left", ha="left", va="center", zorder=20) x += 50 ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) ) -ax.text(x-4.5, y, "wspace", ha="right", va="center", size="x-small", zorder=20) +ax.text(x-4.5, y, "wspace", ha="right", va="center", zorder=20) x += 45 ext_arrow( (x-4, y), (x, y), (x+5, y), (x+9, y) ) -ax.text(x-4.5, y, "right", ha="right", va="center", size="x-small", zorder=20) +ax.text(x-4.5, y, "right", ha="right", va="center", zorder=20) y = 0 x = 25 ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) ) -ax.text(x, y+9.5, "bottom", ha="center", va="bottom", size="x-small", zorder=20) +ax.text(x, y+9.5, "bottom", ha="center", va="bottom", zorder=20) y += 35 ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) ) -ax.text(x, y-4.5, "hspace", ha="center", va="top", size="x-small", zorder=20) +ax.text(x, y-4.5, "hspace", ha="center", va="top", zorder=20) y += 35 ext_arrow( (x, y-4), (x, y), (x, y+5), (x, y+9) ) -ax.text(x, y-4.5, "top", ha="center", va="top", size="x-small", zorder=20) +ax.text(x, y-4.5, "top", ha="center", va="top", zorder=20) int_arrow((0, -5), (100, -5)) ax.text(50, -5, "figure width", backgroundcolor="white", zorder=30, - ha="center", va="center", size="x-small") + ha="center", va="center") int_arrow((105, 0), (105, 75)) ax.text(105, 75/2, "figure height", backgroundcolor="white", zorder=30, - rotation="vertical", ha="center", va="center", size="x-small") + rotation="vertical", ha="center", va="center") int_arrow((55, 62.5), (95, 62.5)) ax.text(75, 62.5, "axes width", backgroundcolor="white", zorder=30, - ha="center", va="center", size="x-small") + ha="center", va="center") int_arrow((62.5, 5), (62.5, 70)) ax.text(62.5, 35, "axes height", backgroundcolor="white", zorder=30, - rotation="vertical", ha="center", va="center", size="x-small") + rotation="vertical", ha="center", va="center") plt.savefig("../figures/adjustments.pdf") -# plt.show() diff --git a/scripts/advanced-plots.py b/scripts/advanced-plots.py index 3ad36c0..4d86ba0 100644 --- a/scripts/advanced-plots.py +++ b/scripts/advanced-plots.py @@ -17,23 +17,23 @@ ]) -fig = plt.figure() -d = 0.01 -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) +subplot_kw = dict( + xlim=(0, 8), xticks=np.arange(1, 8), + ylim=(0, 8), yticks=np.arange(1, 8), +) # Step plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 16) Y = 4 + 2*np.sin(2*X) ax.step(X, Y, color="C1") -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-step.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-step.pdf") # Violin plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(10) D = np.random.normal((3, 5, 4), (0.75, 1.00, 0.75), (200, 3)) VP = ax.violinplot(D, [2, 4, 6], widths=1.5, @@ -41,15 +41,13 @@ for body in VP['bodies']: body.set_facecolor('C1') body.set_alpha(1) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-violin.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-violin.pdf") # Boxplot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(10) D = np.random.normal((3, 5, 4), (1.25, 1.00, 1.25), (100, 3)) VP = ax.boxplot(D, positions=[2, 4, 6], widths=1.5, patch_artist=True, @@ -63,85 +61,74 @@ "linewidth": 0.75}, capprops={"color": "C1", "linewidth": 0.75}) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-boxplot.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-boxplot.pdf") # Barbs plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X = [[2, 4, 6]] Y = [[1.5, 3, 2]] U = -np.ones((1, 3)) * 0 V = -np.ones((1, 3)) * np.linspace(50, 100, 3) ax.barbs(X, Y, U, V, barbcolor="C1", flagcolor="C1", length=5, linewidth=0.5) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-barbs.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-barbs.pdf") # Event plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X = [2, 4, 6] D = np.random.gamma(4, size=(3, 50)) ax.eventplot(D, colors="C1", orientation="vertical", lineoffsets=X, linewidth=0.25) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-event.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-event.pdf") # Errorbar plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X = [2, 4, 6] Y = [4, 5, 4] E = np.random.uniform(0.5, 1.5, 3) ax.errorbar(X, Y, E, color="C1", linewidth=0.75, capsize=1) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-errorbar.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-errorbar.pdf") # Hexbin plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X = np.random.uniform(1.5, 6.5, 100) Y = np.random.uniform(1.5, 6.5, 100) C = np.random.uniform(0, 1, 10000) ax.hexbin(X, Y, C, gridsize=4, linewidth=0.25, edgecolor="white", cmap=plt.get_cmap("Wistia"), alpha=1.0) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-hexbin.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-hexbin.pdf") # Hist plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X = 4 + np.random.normal(0, 1.5, 200) ax.hist(X, bins=8, facecolor="C1", linewidth=0.25, edgecolor="white") -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 80), ax.set_yticks(np.arange(1, 80, 10)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-hist.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-hist.pdf") # Xcorr plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(3) Y = np.random.uniform(-4, 4, 250) X = np.random.uniform(-4, 4, 250) @@ -150,6 +137,5 @@ ax.set_xlim(-8, 8), ax.set_xticks(np.arange(-8, 8, 2)) ax.set_ylim(-.25, .25), ax.set_yticks(np.linspace(-.25, .25, 9)) ax.set_axisbelow(True) -ax.grid(linewidth=0.125) -plt.savefig("../figures/advanced-xcorr.pdf") -ax.clear() +ax.grid() +fig.savefig("../figures/advanced-xcorr.pdf") diff --git a/scripts/anatomy.py b/scripts/anatomy.py index 4a32544..a79037e 100644 --- a/scripts/anatomy.py +++ b/scripts/anatomy.py @@ -3,10 +3,19 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- +import pathlib + import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt from matplotlib.ticker import AutoMinorLocator, MultipleLocator, FuncFormatter + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', +]) + + np.random.seed(123) X = np.linspace(0.5, 3.5, 100) @@ -14,8 +23,7 @@ Y2 = 1+np.cos(1+X/0.75)/2 Y3 = np.random.uniform(Y1, Y2, len(X)) -fig = plt.figure(figsize=(8, 8)) -ax = fig.add_subplot(1, 1, 1, aspect=1) +(fig, ax) = plt.subplots(figsize=(8, 8), subplot_kw=dict(aspect=1)) def minor_tick(x, pos): diff --git a/scripts/basic-plots.py b/scripts/basic-plots.py index 64e07fc..5dd8598 100644 --- a/scripts/basic-plots.py +++ b/scripts/basic-plots.py @@ -17,75 +17,66 @@ ]) -fig = plt.figure() -d = 0.01 -ax = fig.add_axes([d, d, 1 - 2 * d, 1 - 2 * d]) +subplot_kw = dict( + xlim=(0, 8), xticks=np.arange(1, 8), + ylim=(0, 8), yticks=np.arange(1, 8), +) # Basic line plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 4 + 2*np.sin(2*X) ax.plot(X, Y, color="C1") -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.grid() -plt.savefig("../figures/basic-plot.pdf") -ax.clear() - -# Basic line plot (color)blaPwd +fig.savefig("../figures/basic-plot.pdf") +# Basic line plot (color) # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 4 + 2*np.sin(2*X) ax.plot(X, Y, color="black") -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.grid() -plt.savefig("../figures/basic-plot-color.pdf") -ax.clear() +fig.savefig("../figures/basic-plot-color.pdf") # Basic scatter plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(3) X = 4 + np.random.normal(0, 1.25, 24) Y = 4 + np.random.normal(0, 1.25, len(X)) ax.scatter(X, Y, 5, zorder=10, edgecolor="white", facecolor="C1", linewidth=0.25) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.grid() -plt.savefig("../figures/basic-scatter.pdf") -ax.clear() +fig.savefig("../figures/basic-scatter.pdf") # Basic bar plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(3) X = 0.5 + np.arange(8) Y = np.random.uniform(2, 7, len(X)) ax.bar(X, Y, bottom=0, width=1, edgecolor="white", facecolor="C1", linewidth=0.25) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) ax.grid() -plt.savefig("../figures/basic-bar.pdf") -ax.clear() +fig.savefig("../figures/basic-bar.pdf") # Basic imshow plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(3) Z = np.zeros((8, 8, 4)) Z[:, :] = mpl.colors.to_rgba("C1") Z[..., 3] = np.random.uniform(0.25, 1.0, (8, 8)) ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest") -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.grid(linewidth=0.25, color="white") -plt.savefig("../figures/basic-imshow.pdf") -ax.clear() +fig.savefig("../figures/basic-imshow.pdf") # Basic pcolormesh plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X, Y = np.meshgrid(np.linspace(-3, 3, 256), np.linspace(-3, 3, 256)) Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2) @@ -93,76 +84,65 @@ plt.pcolormesh(X, Y, Z, cmap='Oranges', shading='auto') ax.set_xlim(-3, 3), ax.set_xticks(np.arange(-3, 4)) ax.set_ylim(-3, 3), ax.set_yticks(np.arange(-3, 4)) -plt.savefig("../figures/basic-pcolormesh.pdf") -ax.clear() +fig.savefig("../figures/basic-pcolormesh.pdf") # Basic contour plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) colors = np.zeros((5, 4)) colors[:] = mpl.colors.to_rgba("C1") colors[:, 3] = np.linspace(0.15, 0.85, len(colors)) plt.contourf(Z, len(colors), extent=[0, 8, 0, 8], colors=colors) plt.contour(Z, len(colors), extent=[0, 8, 0, 8], colors="white", linewidths=0.125, nchunk=10) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -plt.savefig("../figures/basic-contour.pdf") -ax.clear() +fig.savefig("../figures/basic-contour.pdf") # Basic pie plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = [1, 2, 3, 4] colors = np.zeros((len(X), 4)) colors[:] = mpl.colors.to_rgba("C1") colors[:, 3] = np.linspace(0.25, 0.75, len(X)) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) ax.grid(linewidth=0.25, color="0.75") ax.pie(X, colors=["white"] * len(X), radius=3, center=(4, 4), wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True) ax.pie(X, colors=colors, radius=3, center=(4, 4), wedgeprops={"linewidth": 0.25, "edgecolor": "white"}, frame=True) -plt.savefig("../figures/basic-pie.pdf") -ax.clear() +fig.savefig("../figures/basic-pie.pdf") # Basic text plot # ----------------------------------------------------------------------------- -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) ax.set_axisbelow(True) ax.grid(linewidth=0.25, color="0.75") ax.text(4, 4, "TEXT", color="C1", size=8, weight="bold", ha="center", va="center", rotation=25) -plt.savefig("../figures/basic-text.pdf") -ax.clear() +fig.savefig("../figures/basic-text.pdf") # Basic fill plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) X = np.linspace(0, 8, 16) Y1 = 3 + 4*X/8 + np.random.uniform(0.0, 0.5, len(X)) Y2 = 1 + 2*X/8 + np.random.uniform(0.0, 0.5, len(X)) plt.fill_between(X, Y1, Y2, color="C1", alpha=.5, linewidth=0) plt.plot(X, (Y1+Y2)/2, color="C1", linewidth=0.5) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) ax.grid(color="0.75") -plt.savefig("../figures/basic-fill.pdf") -ax.clear() +fig.savefig("../figures/basic-fill.pdf") # Basic quiver plot # ----------------------------------------------------------------------------- +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) np.random.seed(1) T = np.linspace(0, 2*np.pi, 8) X, Y = 4 + np.cos(T), 4 + np.sin(T) U, V = 1.5*np.cos(T), 1.5*np.sin(T) plt.quiver(X, Y, U, V, color="C1", angles='xy', scale_units='xy', scale=0.5, width=.05) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) ax.set_axisbelow(True) ax.grid(color="0.75") -plt.savefig("../figures/basic-quiver.pdf") -ax.clear() +fig.savefig("../figures/basic-quiver.pdf") diff --git a/scripts/colornames.py b/scripts/colornames.py index 0f0260e..d88d381 100644 --- a/scripts/colornames.py +++ b/scripts/colornames.py @@ -5,22 +5,27 @@ Simple plot example with the named colors and its visual representation. """ -from __future__ import division +import pathlib +import matplotlib as mpl import matplotlib.pyplot as plt -from matplotlib import colors as mcolors -colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS) +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', +]) +mpl.rc('figure.constrained_layout', h_pad=0, w_pad=0, hspace=0, wspace=0) + +colors = dict(mpl.colors.BASE_COLORS, **mpl.colors.CSS4_COLORS) # Sort colors by hue, saturation, value and name. -by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name) +by_hsv = sorted((tuple(mpl.colors.rgb_to_hsv(mpl.colors.to_rgba(color)[:3])), name) for name, color in colors.items()) sorted_names = [name for hsv, name in by_hsv] n = len(sorted_names) ncols = 3 -nrows = n // ncols +1 +nrows = n // ncols fig, ax = plt.subplots(figsize=(4.5, 6)) @@ -30,8 +35,8 @@ w = X / ncols for i, name in enumerate(sorted_names): - col = i // (nrows-1) - row = i % (nrows-1) + col = i // nrows + row = i % nrows y = Y - (row * h) - h xi_line = w * (col + 0.05) @@ -49,9 +54,4 @@ ax.set_ylim(0, Y) ax.set_axis_off() -fig.subplots_adjust(left=0, right=1, - top=1, bottom=0, - hspace=0, wspace=0) - plt.savefig("../figures/colornames.pdf") -# plt.show() diff --git a/scripts/extents.py b/scripts/extents.py index 28a0236..ba424d2 100644 --- a/scripts/extents.py +++ b/scripts/extents.py @@ -2,17 +2,25 @@ # Matplotlib cheat sheet # Released under the BSD License # ----------------------------------------------------------------------------- +import pathlib -# Scripts to generate all the basic plots import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', +]) +mpl.rc('figure.constrained_layout', wspace=0.05) + + Z = np.arange(5*5).reshape(5, 5) fig = plt.figure(figsize=(8, 5)) +gs = fig.add_gridspec(2, 2) -ax = fig.add_subplot(2, 2, 1) +ax = fig.add_subplot(gs[0, 0]) ax.imshow(Z, extent=[0, 10, 0, 5], interpolation="nearest", origin="upper") ax.set_xlim(-1, 11), ax.set_xticks([]) ax.set_ylim(-1, 6), ax.set_yticks([0, 5]) @@ -23,7 +31,7 @@ ax.text(5.0, -0.5, "extent=[0,10,0,5]", ha="center", va="center", color="black", size="large") -ax = fig.add_subplot(2, 2, 3) +ax = fig.add_subplot(gs[1, 0]) ax.imshow(Z, extent=[0, 10, 0, 5], interpolation="nearest", origin="lower") ax.set_xlim(-1, 11), ax.set_xticks([0, 10]) ax.set_ylim(-1, 6), ax.set_yticks([0, 5]) @@ -35,8 +43,7 @@ ax.text(5.0, -0.5, "extent=[0,10,0,5]", ha="center", va="center", color="black", size="large") - -ax = fig.add_subplot(2, 2, 4) +ax = fig.add_subplot(gs[1, 1]) ax.imshow(Z, extent=[10, 0, 0, 5], interpolation="nearest", origin="lower") ax.set_xlim(-1, 11), ax.set_xticks([0, 10]) ax.set_ylim(-1, 6), ax.set_yticks([]) @@ -46,10 +53,8 @@ ha="center", va="center", color="black", size="large") ax.text(5.0, -0.5, "extent=[10,0,0,5]", ha="center", va="center", color="black", size="large") -plt.tight_layout() - -ax = fig.add_subplot(2, 2, 2) +ax = fig.add_subplot(gs[0, 1]) ax.imshow(Z, extent=[10, 0, 0, 5], interpolation="nearest", origin="upper") ax.set_xlim(-1, 11), ax.set_xticks([]) ax.set_ylim(-1, 6), ax.set_yticks([]) @@ -59,8 +64,5 @@ ha="center", va="center", color="black", size="large") ax.text(5.0, -0.5, "extent=[10,0,0,5]", ha="center", va="center", color="black", size="large") -plt.tight_layout() - plt.savefig("../figures/extents.pdf", dpi=600) -# plt.show() diff --git a/scripts/legend.py b/scripts/legend.py index 5269ff9..f75f147 100644 --- a/scripts/legend.py +++ b/scripts/legend.py @@ -2,14 +2,30 @@ # Matplotlib cheat sheet # Released under the BSD License # ----------------------------------------------------------------------------- +import pathlib + import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt -fig = plt.figure(figsize=(4, 4)) -ax = fig.add_axes([0.15, 0.15, .7, .7], frameon=True, aspect=1, - xticks=[], yticks=[]) +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', +]) +mpl.rc('font', size=6) +mpl.rc('lines', markersize=4) + + +subplots_kw = dict( + figsize=(5.7/2.54, 5.7/2.54), + subplot_kw=dict( + aspect=1, frameon=True, + xlim=(0, 1), ylim=(0, 1), + xticks=[], yticks=[], + ), +) + +(fig, ax) = plt.subplots(**subplots_kw) def text(x, y, _text): @@ -47,7 +63,4 @@ def point(x, y): text(1-d, 1+d, "J"), text(0.5, 1+d, "K"), text( d, 1+d, "L") point(1-d+e, 1+d-e), point(0.5, 1+d-e), point(d-e, 1+d-e), -plt.xlim(0, 1), plt.ylim(0, 1) - -plt.savefig("../figures/legend-placement.pdf") -# plt.show() +fig.savefig("../figures/legend-placement.pdf") diff --git a/scripts/plot-variations.py b/scripts/plot-variations.py index ad4cc30..60f9463 100644 --- a/scripts/plot-variations.py +++ b/scripts/plot-variations.py @@ -2,150 +2,128 @@ # Matplotlib cheat sheet # Released under the BSD License # ----------------------------------------------------------------------------- +import pathlib # Scripts to generate all the basic plots import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt -fig = plt.figure(figsize=(0.4, 0.4)) -mpl.rcParams['axes.linewidth'] = 0.5 -mpl.rcParams['xtick.major.size'] = 0.0 -mpl.rcParams['ytick.major.size'] = 0.0 -d = 0.01 + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', +]) +mpl.rc('axes', titlepad=1) + + +subplot_kw = dict( + xlim=(0, 8), xticks=np.arange(1, 8), + ylim=(0, 8), yticks=np.arange(1, 8), +) # Basic line plot (color) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 4+2*np.sin(2*X) ax.plot(X, Y, color="black", linewidth=0.75) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) -plt.savefig("../figures/plot-color.pdf") -fig.clear() +ax.grid() +fig.savefig("../figures/plot-color.pdf") # Basic line plot (linestyle) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 4+2*np.sin(2*X) ax.plot(X, Y, color="C1", linewidth=0.75, linestyle="--") -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) -plt.savefig("../figures/plot-linestyle.pdf") -fig.clear() +ax.grid() +fig.savefig("../figures/plot-linestyle.pdf") # Basic line plot (linewidth) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 4+2*np.sin(2*X) ax.plot(X, Y, color="C1", linewidth=1.5) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) -plt.savefig("../figures/plot-linewidth.pdf") -fig.clear() +ax.grid() +fig.savefig("../figures/plot-linewidth.pdf") # Basic line plot (marker) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 4+2*np.sin(2*X) ax.plot(X, Y, color="C1", linewidth=0.75, marker="o", markevery=5, markersize=2) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) -plt.savefig("../figures/plot-marker.pdf") -fig.clear() +ax.grid() +fig.savefig("../figures/plot-marker.pdf") # Basic line plot (multi) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y1 = 4+2*np.sin(2*X) Y2 = 4+2*np.cos(2*X) ax.plot(X, Y1, color="C1", linewidth=0.75) ax.plot(X, Y2, color="C0", linewidth=0.75) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) -ax.set_ylim(0, 8), ax.set_yticks(np.arange(1, 8)) -ax.grid(linewidth=0.125) -plt.savefig("../figures/plot-multi.pdf") -fig.clear() +ax.grid() +fig.savefig("../figures/plot-multi.pdf") # Basic line plot (vsplit) # ----------------------------------------------------------------------------- -ax1 = fig.add_axes([d, d, 1-2*d, .45-d]) -ax2 = fig.add_axes([d, .55-d, 1-2*d, .45-d]) +(fig, [ax2, ax1]) = plt.subplots(nrows=2, gridspec_kw=dict(hspace=0.2), subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y1 = 2+1*np.sin(2*X) ax1.plot(X, Y1, color="C1", linewidth=0.75) -ax1.set_xlim(0, 8), ax1.set_xticks(np.arange(1, 8)), ax1.set_xticklabels([]) -ax1.set_ylim(0, 4), ax1.set_yticks(np.arange(1, 4)), ax1.set_yticklabels([]) -ax1.grid(linewidth=0.125) +ax1.set_ylim(0, 4), ax1.set_yticks(np.arange(1, 4)) +ax1.grid() ax1.tick_params(axis=u'both', which=u'both', length=0) Y2 = 2+1*np.cos(2*X) ax2.plot(X, Y2, color="C0", linewidth=0.75) -ax2.set_xlim(0, 8), ax2.set_xticks(np.arange(1, 8)), ax2.set_xticklabels([]) -ax2.set_ylim(0, 4), ax2.set_yticks(np.arange(1, 4)), ax2.set_yticklabels([]) -ax2.grid(linewidth=0.125) +ax2.set_ylim(0, 4), ax2.set_yticks(np.arange(1, 4)) +ax2.grid() ax2.tick_params(axis=u'both', which=u'both', length=0) -plt.savefig("../figures/plot-vsplit.pdf") -fig.clear() +fig.savefig("../figures/plot-vsplit.pdf") # Basic line plot (hsplit) # ----------------------------------------------------------------------------- -ax1 = fig.add_axes([d, d, .45-d, 1-2*d]) -ax2 = fig.add_axes([.55-d, d, .45-d, 1-2*d]) +(fig, [ax1, ax2]) = plt.subplots(ncols=2, gridspec_kw=dict(wspace=0.2), subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y1 = 2+1*np.sin(2*X) ax1.plot(Y1, X, color="C1", linewidth=0.75) -ax1.set_xlim(0, 4), ax1.set_xticks(np.arange(1, 4)), ax1.set_xticklabels([]) -ax1.set_ylim(0, 8), ax1.set_yticks(np.arange(1, 8)), ax1.set_yticklabels([]) -ax1.grid(linewidth=0.125) +ax1.set_xlim(0, 4), ax1.set_xticks(np.arange(1, 4)) +ax1.grid() ax1.tick_params(axis=u'both', which=u'both', length=0) Y2 = 2+1*np.cos(2*X) ax2.plot(Y2, X, color="C0", linewidth=0.75) -ax2.set_xlim(0, 4), ax2.set_xticks(np.arange(1, 4)), ax2.set_xticklabels([]) -ax2.set_ylim(0, 8), ax2.set_yticks(np.arange(1, 8)), ax2.set_yticklabels([]) -ax2.grid(linewidth=0.125) +ax2.set_xlim(0, 4), ax2.set_xticks(np.arange(1, 4)) +ax2.grid() ax2.tick_params(axis=u'both', which=u'both', length=0) -plt.savefig("../figures/plot-hsplit.pdf") -fig.clear() +fig.savefig("../figures/plot-hsplit.pdf") # Basic line plot (title) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, d, 1-2*d, .8-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 3+2*np.sin(2*X) ax.plot(X, Y, color="C1", linewidth=0.75) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)) ax.set_ylim(0, 6), ax.set_yticks(np.arange(1, 6)) -ax.grid(linewidth=0.125) -ax.text(4, 6.75, "A Sine wave", transform=ax.transData, clip_on=False, - weight="bold", size=4, ha="center", va="center") -plt.savefig("../figures/plot-title.pdf") -fig.clear() - +ax.grid() +ax.set_title("A Sine wave", size=4, weight="bold") +fig.savefig("../figures/plot-title.pdf") # Basic line plot (xlabel) # ----------------------------------------------------------------------------- -ax = fig.add_axes([d, .2-d, 1-2*d, .8-2*d]) +(fig, ax) = plt.subplots(subplot_kw=subplot_kw) X = np.linspace(0, 10, 100) Y = 3+2*np.sin(2*X) -ax.plot(X, Y, color="C1", linewidth=0.75) -ax.set_xlim(0, 8), ax.set_xticks(np.arange(1, 8)), ax.set_xticklabels([]) +ax.plot(X, Y, color="C1", linewidth=0.75), ax.set_xticklabels([]) ax.set_ylim(0, 6), ax.set_yticks(np.arange(1, 6)), ax.set_yticklabels([]) -ax.grid(linewidth=0.125) +ax.grid() ax.text(4, -1, "Time", transform=ax.transData, clip_on=False, size=3.5, ha="center", va="center") -# plt.show() -plt.savefig("../figures/plot-xlabel.pdf") -fig.clear() +fig.savefig("../figures/plot-xlabel.pdf") diff --git a/scripts/sine.py b/scripts/sine.py index c31c49f..6b30775 100644 --- a/scripts/sine.py +++ b/scripts/sine.py @@ -2,71 +2,68 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- +import pathlib + import numpy as np +import matplotlib as mpl import matplotlib.pyplot as plt + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/sine-plot.mplstyle', +]) + + X = np.linspace(0.1, 10*np.pi, 10000) Y = np.sin(X) -fig = plt.figure(figsize=(8, 2)) -plt.plot(X, Y, color="orange", linewidth=2) -# plt.xticks([]), plt.yticks([]) -plt.tight_layout() -plt.savefig("../figures/sine.pdf", dpi=100) +(fig, ax) = plt.subplots(figsize=(5.7/2.54, 1.2/2.54)) +ax.set_yticks(np.linspace(-1, 1, 5)) +ax.plot(X, Y, color="orange") +plt.savefig("../figures/sine.pdf") -fig = plt.figure(figsize=(7, 1.5)) -plt.plot(X, Y, "C1o:", markevery=500, mec="1.0", lw=2, ms=8.5, mew=2) -# plt.xticks([]), plt.yticks([]) -plt.ylim(-1.5, 1.5) -plt.tight_layout() -plt.savefig("../figures/sine-marker.pdf", dpi=100) +(fig, ax) = plt.subplots(figsize=(5.7/2.54, 1.0/2.54)) +ax.plot(X, Y, "C1o:", markevery=500, mec="1.0") +ax.set_ylim(-1.5, 1.5) +fig.savefig("../figures/sine-marker.pdf") -fig, ax = plt.subplots(figsize=(7, 1.5)) +fig, ax = plt.subplots(figsize=(5.7/2.54, 1.0/2.54)) ax.set_xscale("log") ax.plot(X, Y, "-") -plt.plot(X, Y, "C1o-", markevery=500, mec="1.0", lw=2, ms=8.5, mew=2) -# plt.xticks([]), plt.yticks([]) -plt.ylim(-1.5, 1.5) -plt.tight_layout() -plt.savefig("../figures/sine-logscale.pdf", dpi=100) +ax.plot(X, Y, "C1o-", markevery=500, mec="1.0") +ax.set_ylim(-1.5, 1.5) +fig.savefig("../figures/sine-logscale.pdf") -fig = plt.figure(figsize=(7, 1.5)) -plt.plot(X, Y, "C1", lw=2) -plt.fill_betweenx([-1.5, 1.5], [0], [2*np.pi], color=".9") -plt.text(0, -1, r" Period $\Phi$") -# plt.xticks([]), plt.yticks([]) -plt.ylim(-1.5, 1.5) -plt.tight_layout() -plt.savefig("../figures/sine-period.pdf", dpi=100) -# plt.show() +(fig, ax) = plt.subplots(figsize=(5.7/2.54, 1.0/2.54)) +ax.plot(X, Y, "C1") +ax.fill_betweenx([-1.5, 1.5], [0], [2*np.pi], color=".9") +ax.text(0, -1, r" Period $\Phi$", va="top") +ax.set_ylim(-1.5, 1.5) +fig.savefig("../figures/sine-period.pdf") -fig = plt.figure(figsize=(7, 1.5)) -plt.plot(X, np.sin(X), "C0", lw=2, label="Sine") -plt.plot(X, np.cos(X), "C1", lw=2, label="Cosine") -plt.legend(bbox_to_anchor=(0.0, .9, 1.02, 0.1), - frameon=False, mode="expand", ncol=2, loc="lower left") -plt.title("Sine and Cosine") -plt.xticks([]), plt.yticks([]) -plt.ylim(-1.25, 1.25) -plt.tight_layout() -plt.savefig("../figures/sine-legend.pdf", dpi=100) -# plt.show() +(fig, ax) = plt.subplots(figsize=(5.7/2.54, 1.0/2.54)) +ax.plot(X, np.sin(X), "C0", label="Sine") +ax.plot(X, np.cos(X), "C1", label="Cosine") +ax.legend(bbox_to_anchor=(0.0, .9, 1.02, 0.1), + frameon=False, mode="expand", ncol=2, loc="lower left") +ax.set_title("Sine and Cosine") +ax.set_xticks([]), ax.set_yticks([]) +ax.set_ylim(-1.25, 1.25) +fig.savefig("../figures/sine-legend.pdf") -fig = plt.figure(figsize=(7, 1.5)) +(fig, ax) = plt.subplots(figsize=(5.7/2.54, 1.0/2.54)) X = np.linspace(0, 10*np.pi, 1000) Y = np.sin(X) -plt.plot(X, Y, "C1o-", markevery=50, mec="1.0", lw=2, ms=8.5, mew=2) -# plt.xticks([]), plt.yticks([]) -plt.ylim(-1.5, 1.5) -plt.annotate(" ", (X[200], Y[200]), (X[250], -1), ha="center", va="center", - arrowprops={"arrowstyle" : "->", "color": "C1"}) -plt.annotate("A", (X[250], Y[250]), (X[250], -1), ha="center", va="center", - arrowprops={"arrowstyle" : "->", "color": "C1"}) -plt.annotate(" ", (X[300], Y[300]), (X[250], -1), ha="center", va="center", - arrowprops={"arrowstyle" : "->", "color": "C1"}) -plt.tight_layout() -plt.savefig("../figures/sine-annotate.pdf", dpi=100) -# plt.show() +ax.plot(X, Y, "C1o-", markevery=50, mec="1.0") +ax.set_ylim(-1.5, 1.5) +ax.annotate(" ", (X[200], Y[200]), (X[250], -1), ha="center", va="center", + arrowprops=dict(arrowstyle="->", color="C1", linewidth=0.5, patchA=None, shrinkA=4, shrinkB=0.5)) +ax.annotate("A", (X[250], Y[250]), (X[250], -1), ha="center", va="center", + arrowprops=dict(arrowstyle="->", color="C1", linewidth=0.5, patchA=None, shrinkA=4, shrinkB=0.5)) +ax.annotate(" ", (X[300], Y[300]), (X[250], -1), ha="center", va="center", + arrowprops=dict(arrowstyle="->", color="C1", linewidth=0.5, patchA=None, shrinkA=4, shrinkB=0.5)) +fig.savefig("../figures/sine-annotate.pdf") diff --git a/scripts/tick-formatters.py b/scripts/tick-formatters.py index c652846..07ecfe3 100644 --- a/scripts/tick-formatters.py +++ b/scripts/tick-formatters.py @@ -17,86 +17,70 @@ ]) -# Setup a plot such that only the bottom spine is shown +subplots_kw = dict( + figsize=(5.7/2.54, 3.5/2.54), + nrows=7, + subplot_kw=dict(xlim=(0, 5), ylim=(0, 1)) +) - -def setup(ax): - """Set up Axes with just an x-Axis.""" - ax.yaxis.set_major_locator(ticker.NullLocator()) - ax.set_xlim(0, 5) - ax.set_ylim(0, 1) - ax.patch.set_alpha(0.0) - - -fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54)) -fig.patch.set_alpha(0.0) -n = 7 +(fig, axs) = plt.subplots(**subplots_kw) # Null formatter -ax = fig.add_subplot(n, 1, 1) -setup(ax) +ax = axs[0] ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(ticker.NullFormatter()) -ax.text(0.0, 0.1, "ticker.NullFormatter()", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.NullFormatter()", + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # Fixed formatter -ax = fig.add_subplot(n, 1, 2) -setup(ax) +ax = axs[1] ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_locator(ticker.FixedLocator(range(6))) majors = ["zero", "one", "two", "three", "four", "five"] ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors)) -ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", transform=ax.transAxes) - +ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) -# FuncFormatter can be used as a decorator -@ticker.FuncFormatter -def major_formatter(x, pos): - """Return formatted value with 2 decimal places.""" - return "[%.2f]" % x - - -ax = fig.add_subplot(n, 1, 3) -setup(ax) +# FuncFormatter formatter +ax = axs[2] ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) -ax.xaxis.set_major_formatter(major_formatter) -ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', transform=ax.transAxes) - +ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)) +ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # FormatStr formatter -ax = fig.add_subplot(n, 1, 4) -setup(ax) +ax = axs[3] ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<")) -ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # Scalar formatter -ax = fig.add_subplot(n, 1, 5) -setup(ax) +ax = axs[4] ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) -ax.text(0.0, 0.1, "ticker.ScalarFormatter()", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.ScalarFormatter()", + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # StrMethod formatter -ax = fig.add_subplot(n, 1, 6) -setup(ax) +ax = axs[5] ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}")) -ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # Percent formatter -ax = fig.add_subplot(n, 1, 7) -setup(ax) +ax = axs[6] ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5)) -ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", + fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) plt.savefig("../figures/tick-formatters.pdf") -# plt.show() diff --git a/scripts/tick-locators.py b/scripts/tick-locators.py index c6521c0..1891b30 100644 --- a/scripts/tick-locators.py +++ b/scripts/tick-locators.py @@ -17,78 +17,71 @@ ]) -# Setup a plot such that only the bottom spine is shown +subplots_kw = dict( + figsize=(5.7/2.54, 4/2.54), + nrows=8, + subplot_kw=dict(xlim=(0, 5), ylim=(0, 1)), +) - -def setup(ax): - ax.yaxis.set_major_locator(ticker.NullLocator()) - ax.set_xlim(0, 5) - ax.set_ylim(0, 1) - ax.patch.set_alpha(0.0) - - -fig = plt.figure(figsize=(5.7/2.54, 3.8/2.54)) -fig.patch.set_alpha(0.0) -n = 8 +(fig, axs) = plt.subplots(**subplots_kw) # Null Locator -ax = plt.subplot(n, 1, 1) -setup(ax) +ax = axs[0] ax.xaxis.set_major_locator(ticker.NullLocator()) ax.xaxis.set_minor_locator(ticker.NullLocator()) -ax.text(0.0, 0.1, "ticker.NullLocator()", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.NullLocator()", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Multiple Locator -ax = plt.subplot(n, 1, 2) -setup(ax) +ax = axs[1] ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) -ax.text(0.0, 0.1, "ticker.MultipleLocator(0.5)", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.MultipleLocator(0.5)", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Fixed Locator -ax = plt.subplot(n, 1, 3) -setup(ax) +ax = axs[2] majors = [0, 1, 5] ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) minors = np.linspace(0, 1, 11)[1:-1] ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) -ax.text(0.0, 0.1, "ticker.FixedLocator([0, 1, 5])", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.FixedLocator([0, 1, 5])", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Linear Locator -ax = plt.subplot(n, 1, 4) -setup(ax) +ax = axs[3] ax.xaxis.set_major_locator(ticker.LinearLocator(3)) ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) -ax.text(0.0, 0.1, "ticker.LinearLocator(numticks=3)", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.LinearLocator(numticks=3)", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Index Locator -ax = plt.subplot(n, 1, 5) -setup(ax) +ax = axs[4] ax.plot(range(0, 5), [0]*5, color='white') ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) -ax.text(0.0, 0.1, "ticker.IndexLocator(base=0.5, offset=0.25)", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.IndexLocator(base=0.5, offset=0.25)", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Auto Locator -ax = plt.subplot(n, 1, 6) -setup(ax) +ax = axs[5] ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) -ax.text(0.0, 0.1, "ticker.AutoLocator()", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.AutoLocator()", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # MaxN Locator -ax = plt.subplot(n, 1, 7) -setup(ax) +ax = axs[6] ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) -ax.text(0.0, 0.1, "ticker.MaxNLocator(n=4)", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.MaxNLocator(n=4)", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Log Locator -ax = plt.subplot(n, 1, 8) -setup(ax) +ax = axs[7] ax.set_xlim(10**3, 10**10) ax.set_xscale('log') ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) -ax.text(0.0, 0.1, "ticker.LogLocator(base=10, numticks=15)", transform=ax.transAxes) +ax.text(0.0, 0.1, "ticker.LogLocator(base=10, numticks=15)", + fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) plt.savefig("../figures/tick-locators.pdf") -# plt.show() diff --git a/scripts/tick-multiple-locator.py b/scripts/tick-multiple-locator.py index 771e2fe..c8a799e 100644 --- a/scripts/tick-multiple-locator.py +++ b/scripts/tick-multiple-locator.py @@ -3,51 +3,31 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- +import pathlib + import numpy as np +import matplotlib as mpl 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 +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/ticks.mplstyle', +]) -fontsize = 18 -family = "Source Code Pro" -# Null Locator -ax = plt.subplot(n, 1, 1) -# ax.tick_params(axis='both', which='major', -# labelsize=10, family="Roboto Condensed") -ax.tick_params(axis='both', which='minor', labelsize=6) +subplots_kw = dict( + figsize=(5.7/2.54, 0.4/2.54), + subplot_kw=dict(xlim=(0, 5), ylim=(0, 1)), +) -setup(ax) +(fig, ax) = plt.subplots(**subplots_kw) 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='both', which='major', labelsize=5) ax.tick_params(axis='x', which='minor', rotation=90) -plt.tight_layout() plt.savefig("../figures/tick-multiple-locator.pdf", transparent=True) -# plt.show() diff --git a/styles/base.mplstyle b/styles/base.mplstyle index 77cbdce..ea0c81e 100644 --- a/styles/base.mplstyle +++ b/styles/base.mplstyle @@ -1,5 +1,5 @@ figure.constrained_layout.use: True -figure.constrained_layout.h_pad: 0 -figure.constrained_layout.w_pad: 0 -figure.constrained_layout.hspace: 0 -figure.constrained_layout.wspace: 0 +figure.constrained_layout.h_pad: 0.01 +figure.constrained_layout.w_pad: 0.01 +figure.constrained_layout.hspace: 0.1 +figure.constrained_layout.wspace: 0.1 diff --git a/styles/plotlet-grid.mplstyle b/styles/plotlet-grid.mplstyle index c4133c3..e637a9c 100644 --- a/styles/plotlet-grid.mplstyle +++ b/styles/plotlet-grid.mplstyle @@ -1,10 +1,7 @@ -figure.constrained_layout.h_pad: 0.04 -figure.constrained_layout.w_pad: 0.04 -figure.constrained_layout.hspace: 0.04 -figure.constrained_layout.wspace: 0.04 - font.family: Source Code Pro font.size: 5 +axes.linewidth: 0.5 + xtick.major.size: 0.0 ytick.major.size: 0.0 diff --git a/styles/plotlet.mplstyle b/styles/plotlet.mplstyle index b86d240..ff46026 100644 --- a/styles/plotlet.mplstyle +++ b/styles/plotlet.mplstyle @@ -13,3 +13,9 @@ lines.linewidth: 0.75 xtick.major.size: 0.0 ytick.major.size: 0.0 + +xtick.labeltop: False +xtick.labelbottom: False + +ytick.labelleft: False +ytick.labelright: False diff --git a/styles/sine-plot.mplstyle b/styles/sine-plot.mplstyle new file mode 100644 index 0000000..9829f21 --- /dev/null +++ b/styles/sine-plot.mplstyle @@ -0,0 +1,24 @@ +font.size: 4 + +axes.titlesize: 4 +axes.titlepad: 2 +axes.linewidth: 0.2 + +lines.linewidth: 0.5 +lines.markersize: 3 + +xtick.labelsize: 3 +xtick.major.pad: 1 +xtick.major.width: 0.2 +xtick.major.size: 1 +xtick.minor.width: 0.1 +xtick.minor.size: 0.5 + +ytick.labelsize: 3 +ytick.major.pad: 1 +ytick.major.width: 0.2 +ytick.major.size: 1 +ytick.minor.width: 0.1 +ytick.minor.size: 0.5 + +legend.fontsize: 3 diff --git a/styles/ticks.mplstyle b/styles/ticks.mplstyle index a2fb14f..ba56aa8 100644 --- a/styles/ticks.mplstyle +++ b/styles/ticks.mplstyle @@ -1,6 +1,5 @@ savefig.transparent: True -font.family: Source Code Pro font.size: 5 axes.linewidth: 0.5 @@ -10,8 +9,12 @@ axes.spines.left: False axes.spines.bottom: True xtick.labelsize: 3 -xtick.major.pad: 1 -xtick.major.width: 0.2 xtick.major.size: 2 -xtick.minor.width: 0.1 +xtick.major.width: 0.2 +xtick.major.pad: 2 xtick.minor.size: 1 +xtick.minor.width: 0.2 +xtick.minor.pad: 2 + +ytick.left: False +ytick.labelleft: False From e4c6b67c9520d67442d616a220b5092741586e56 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Mon, 3 Jan 2022 13:04:26 -0800 Subject: [PATCH 5/7] small tweaks to size and spacing identified during review --- scripts/annotation-connection-styles.py | 6 +++--- scripts/tick-formatters.py | 16 ++++++++-------- scripts/tick-locators.py | 18 +++++++++--------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/scripts/annotation-connection-styles.py b/scripts/annotation-connection-styles.py index 4382cbb..81908d7 100644 --- a/scripts/annotation-connection-styles.py +++ b/scripts/annotation-connection-styles.py @@ -8,6 +8,7 @@ pathlib.Path(__file__).parent/'../styles/base.mplstyle', pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle', ]) +mpl.rc('lines', markersize=3) def demo_con_style(ax, connectionstyle): @@ -17,8 +18,8 @@ def demo_con_style(ax, connectionstyle): ax.annotate("", xy=(x1, y1), xycoords='data', xytext=(x2, y2), textcoords='data', - arrowprops=dict(arrowstyle="->", color="0.5", - shrinkA=2, shrinkB=2, + arrowprops=dict(arrowstyle="->", lw=0.5, color="0.5", + shrinkA=3, shrinkB=3, patchA=None, patchB=None, connectionstyle=connectionstyle), ) @@ -41,4 +42,3 @@ def demo_con_style(ax, connectionstyle): ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1) plt.savefig("../figures/annotation-connection-styles.pdf") -# plt.show() diff --git a/scripts/tick-formatters.py b/scripts/tick-formatters.py index 07ecfe3..5439630 100644 --- a/scripts/tick-formatters.py +++ b/scripts/tick-formatters.py @@ -31,7 +31,7 @@ ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(ticker.NullFormatter()) -ax.text(0.0, 0.1, "ticker.NullFormatter()", +ax.text(0.0, 0.2, "ticker.NullFormatter()", fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # Fixed formatter @@ -40,7 +40,7 @@ ax.xaxis.set_major_locator(ticker.FixedLocator(range(6))) majors = ["zero", "one", "two", "three", "four", "five"] ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors)) -ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", +ax.text(0.0, 0.2, "ticker.FixedFormatter(['zero', 'one', 'two', …])", fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # FuncFormatter formatter @@ -48,7 +48,7 @@ ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)) -ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', +ax.text(0.0, 0.2, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # FormatStr formatter @@ -56,7 +56,7 @@ ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<")) -ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", +ax.text(0.0, 0.2, "ticker.FormatStrFormatter('>%d<')", fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # Scalar formatter @@ -64,7 +64,7 @@ ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) -ax.text(0.0, 0.1, "ticker.ScalarFormatter()", +ax.text(0.0, 0.2, "ticker.ScalarFormatter()", fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # StrMethod formatter @@ -72,7 +72,7 @@ ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}")) -ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", +ax.text(0.0, 0.2, "ticker.StrMethodFormatter('{x}')", fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) # Percent formatter @@ -80,7 +80,7 @@ ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5)) -ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", +ax.text(0.0, 0.2, "ticker.PercentFormatter(xmax=5)", fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) -plt.savefig("../figures/tick-formatters.pdf") +fig.savefig("../figures/tick-formatters.pdf") diff --git a/scripts/tick-locators.py b/scripts/tick-locators.py index 1891b30..9964f8a 100644 --- a/scripts/tick-locators.py +++ b/scripts/tick-locators.py @@ -29,14 +29,14 @@ ax = axs[0] ax.xaxis.set_major_locator(ticker.NullLocator()) ax.xaxis.set_minor_locator(ticker.NullLocator()) -ax.text(0.0, 0.1, "ticker.NullLocator()", +ax.text(0.0, 0.2, "ticker.NullLocator()", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Multiple Locator ax = axs[1] ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) -ax.text(0.0, 0.1, "ticker.MultipleLocator(0.5)", +ax.text(0.0, 0.2, "ticker.MultipleLocator(0.5)", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Fixed Locator @@ -45,35 +45,35 @@ ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) minors = np.linspace(0, 1, 11)[1:-1] ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) -ax.text(0.0, 0.1, "ticker.FixedLocator([0, 1, 5])", +ax.text(0.0, 0.2, "ticker.FixedLocator([0, 1, 5])", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Linear Locator ax = axs[3] ax.xaxis.set_major_locator(ticker.LinearLocator(3)) ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) -ax.text(0.0, 0.1, "ticker.LinearLocator(numticks=3)", +ax.text(0.0, 0.2, "ticker.LinearLocator(numticks=3)", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Index Locator ax = axs[4] ax.plot(range(0, 5), [0]*5, color='white') ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) -ax.text(0.0, 0.1, "ticker.IndexLocator(base=0.5, offset=0.25)", +ax.text(0.0, 0.2, "ticker.IndexLocator(base=0.5, offset=0.25)", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Auto Locator ax = axs[5] ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) -ax.text(0.0, 0.1, "ticker.AutoLocator()", +ax.text(0.0, 0.2, "ticker.AutoLocator()", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # MaxN Locator ax = axs[6] ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) -ax.text(0.0, 0.1, "ticker.MaxNLocator(n=4)", +ax.text(0.0, 0.2, "ticker.MaxNLocator(n=4)", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) # Log Locator @@ -81,7 +81,7 @@ ax.set_xlim(10**3, 10**10) ax.set_xscale('log') ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) -ax.text(0.0, 0.1, "ticker.LogLocator(base=10, numticks=15)", +ax.text(0.0, 0.2, "ticker.LogLocator(base=10, numticks=15)", fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) -plt.savefig("../figures/tick-locators.pdf") +fig.savefig("../figures/tick-locators.pdf") From c44d32b93ff91de6f6335f4ae91acab7cd1ebc9f Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Thu, 30 Mar 2023 01:14:05 -0700 Subject: [PATCH 6/7] revert some changes --- Makefile | 3 + scripts/annotation-arrow-styles.py | 19 +++--- scripts/tick-formatters.py | 101 ++++++++++++++++++----------- scripts/tick-locators.py | 96 ++++++++++++++++----------- 4 files changed, 133 insertions(+), 86 deletions(-) diff --git a/Makefile b/Makefile index f193644..4a7642a 100644 --- a/Makefile +++ b/Makefile @@ -17,11 +17,14 @@ figures: # crop some of the figures cd figures && pdfcrop adjustments.pdf adjustments.pdf cd figures && pdfcrop annotate.pdf annotate.pdf + cd figures && pdfcrop annotation-arrow-styles.pdf annotation-arrow-styles.pdf cd figures && pdfcrop anatomy.pdf anatomy.pdf cd figures && pdfcrop colornames.pdf colornames.pdf cd figures && pdfcrop fonts.pdf fonts.pdf cd figures && pdfcrop markers.pdf markers.pdf cd figures && pdfcrop text-alignments.pdf text-alignments.pdf + cd figures && pdfcrop tick-formatters.pdf tick-formatters.pdf + cd figures && pdfcrop tick-locators.pdf tick-locators.pdf cd figures && pdfcrop tip-font-family.pdf tip-font-family.pdf cd figures && pdfcrop tip-hatched.pdf tip-hatched.pdf diff --git a/scripts/annotation-arrow-styles.py b/scripts/annotation-arrow-styles.py index 046c3e1..648a4c4 100644 --- a/scripts/annotation-arrow-styles.py +++ b/scripts/annotation-arrow-styles.py @@ -1,17 +1,16 @@ -import pathlib - -import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.patches as mpatches +styles = mpatches.ArrowStyle.get_styles() + -mpl.style.use([ - pathlib.Path(__file__).parent/'../styles/base.mplstyle', - pathlib.Path(__file__).parent/'../styles/plotlet-grid.mplstyle', -]) +def demo_con_style(ax, connectionstyle): + ax.text(.05, .95, connectionstyle.replace(",", ",\n"), + family="Source Code Pro", + transform=ax.transAxes, ha="left", va="top", size="x-small") -(fig, axes) = plt.subplots(4, 4, figsize=(5.7/2.54, 1.5), frameon=True) +(fig, axes) = plt.subplots(4, 4, figsize=(4, 2.5), frameon=False) for ax in axes.flatten(): ax.axis("off") for i, (ax, style) in enumerate(zip(axes.flatten(), mpatches.ArrowStyle.get_styles())): @@ -22,13 +21,13 @@ xy=(x0, y0), xycoords='data', xytext=(x1, y1), textcoords='data', arrowprops=dict(arrowstyle=style, - mutation_scale=10, color="black", shrinkA=5, shrinkB=5, patchA=None, patchB=None, connectionstyle="arc3,rad=0")) ax.text( (x1+x0)/2, y0-0.2, style, transform=ax.transAxes, - ha="center", va="top", fontsize=8) + family="Source Code Pro", ha="center", va="top") plt.savefig("../figures/annotation-arrow-styles.pdf") +# plt.show() diff --git a/scripts/tick-formatters.py b/scripts/tick-formatters.py index 5439630..fe5bb40 100644 --- a/scripts/tick-formatters.py +++ b/scripts/tick-formatters.py @@ -3,84 +3,109 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- -import pathlib - -import numpy as np -import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.ticker as ticker +# Setup a plot such that only the bottom spine is shown + -mpl.style.use([ - pathlib.Path(__file__).parent/'../styles/base.mplstyle', - pathlib.Path(__file__).parent/'../styles/ticks.mplstyle', -]) +def setup(ax): + """Set up Axes with just an x-Axis.""" + 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.xaxis.set_ticks_position('bottom') + ax.tick_params(which='major', width=1.00, length=5) + ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) + ax.set_xlim(0, 5) + ax.set_ylim(0, 1) + ax.patch.set_alpha(0.0) -subplots_kw = dict( - figsize=(5.7/2.54, 3.5/2.54), - nrows=7, - subplot_kw=dict(xlim=(0, 5), ylim=(0, 1)) -) +fig = plt.figure(figsize=(8, 5)) +fig.patch.set_alpha(0.0) +n = 7 -(fig, axs) = plt.subplots(**subplots_kw) +fontsize = 18 +family = "Source Code Pro" # Null formatter -ax = axs[0] +ax = fig.add_subplot(n, 1, 1) +setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(ticker.NullFormatter()) -ax.text(0.0, 0.2, "ticker.NullFormatter()", - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.NullFormatter()", family=family, + fontsize=fontsize, transform=ax.transAxes) # Fixed formatter -ax = axs[1] +ax = fig.add_subplot(n, 1, 2) +setup(ax) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_locator(ticker.FixedLocator(range(6))) majors = ["zero", "one", "two", "three", "four", "five"] ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors)) -ax.text(0.0, 0.2, "ticker.FixedFormatter(['zero', 'one', 'two', …])", - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.FixedFormatter(['zero', 'one', 'two', …])", + family=family, fontsize=fontsize, transform=ax.transAxes) + + +# FuncFormatter can be used as a decorator +@ticker.FuncFormatter +def major_formatter(x, pos): + """Return formatted value with 2 decimal places.""" + return "[%.2f]" % x -# FuncFormatter formatter -ax = axs[2] + +ax = fig.add_subplot(n, 1, 3) +setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) -ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)) -ax.text(0.0, 0.2, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.xaxis.set_major_formatter(major_formatter) +ax.text(0.0, 0.1, 'ticker.FuncFormatter(lambda x, pos: "[%.2f]" % x)', + family=family, fontsize=fontsize, transform=ax.transAxes) + # FormatStr formatter -ax = axs[3] +ax = fig.add_subplot(n, 1, 4) +setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<")) -ax.text(0.0, 0.2, "ticker.FormatStrFormatter('>%d<')", - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.FormatStrFormatter('>%d<')", + family=family, fontsize=fontsize, transform=ax.transAxes) # Scalar formatter -ax = axs[4] +ax = fig.add_subplot(n, 1, 5) +setup(ax) ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) -ax.text(0.0, 0.2, "ticker.ScalarFormatter()", - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.ScalarFormatter()", + family=family, fontsize=fontsize, transform=ax.transAxes) # StrMethod formatter -ax = axs[5] +ax = fig.add_subplot(n, 1, 6) +setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}")) -ax.text(0.0, 0.2, "ticker.StrMethodFormatter('{x}')", - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.StrMethodFormatter('{x}')", + family=family, fontsize=fontsize, transform=ax.transAxes) # Percent formatter -ax = axs[6] +ax = fig.add_subplot(n, 1, 7) +setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) ax.xaxis.set_major_formatter(ticker.PercentFormatter(xmax=5)) -ax.text(0.0, 0.2, "ticker.PercentFormatter(xmax=5)", - fontfamily="Source Code Pro", transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.PercentFormatter(xmax=5)", + family=family, fontsize=fontsize, transform=ax.transAxes) + +# Push the top of the top axes outside the figure because we only show the +# bottom spine. +fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) -fig.savefig("../figures/tick-formatters.pdf") +plt.savefig("../figures/tick-formatters.pdf", transparent=True) +# plt.show() diff --git a/scripts/tick-locators.py b/scripts/tick-locators.py index 9964f8a..276f8a3 100644 --- a/scripts/tick-locators.py +++ b/scripts/tick-locators.py @@ -3,85 +3,105 @@ # Author: Nicolas P. Rougier # License: BSD # ---------------------------------------------------------------------------- -import pathlib - import numpy as np -import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.ticker as ticker +# Setup a plot such that only the bottom spine is shown + -mpl.style.use([ - pathlib.Path(__file__).parent/'../styles/base.mplstyle', - pathlib.Path(__file__).parent/'../styles/ticks.mplstyle', -]) +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.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) -subplots_kw = dict( - figsize=(5.7/2.54, 4/2.54), - nrows=8, - subplot_kw=dict(xlim=(0, 5), ylim=(0, 1)), -) +fig = plt.figure(figsize=(8, 5)) +fig.patch.set_alpha(0.0) +n = 8 -(fig, axs) = plt.subplots(**subplots_kw) +fontsize = 18 +family = "Source Code Pro" # Null Locator -ax = axs[0] +ax = plt.subplot(n, 1, 1) +setup(ax) ax.xaxis.set_major_locator(ticker.NullLocator()) ax.xaxis.set_minor_locator(ticker.NullLocator()) -ax.text(0.0, 0.2, "ticker.NullLocator()", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.NullLocator()", + family=family, fontsize=fontsize, transform=ax.transAxes) # Multiple Locator -ax = axs[1] +ax = plt.subplot(n, 1, 2) +setup(ax) ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) -ax.text(0.0, 0.2, "ticker.MultipleLocator(0.5)", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.MultipleLocator(0.5)", + family=family, fontsize=fontsize, transform=ax.transAxes) # Fixed Locator -ax = axs[2] +ax = plt.subplot(n, 1, 3) +setup(ax) majors = [0, 1, 5] ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) minors = np.linspace(0, 1, 11)[1:-1] ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) -ax.text(0.0, 0.2, "ticker.FixedLocator([0, 1, 5])", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.FixedLocator([0, 1, 5])", + family=family, fontsize=fontsize, transform=ax.transAxes) # Linear Locator -ax = axs[3] +ax = plt.subplot(n, 1, 4) +setup(ax) ax.xaxis.set_major_locator(ticker.LinearLocator(3)) ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) -ax.text(0.0, 0.2, "ticker.LinearLocator(numticks=3)", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.LinearLocator(numticks=3)", + family=family, fontsize=fontsize, transform=ax.transAxes) # Index Locator -ax = axs[4] +ax = plt.subplot(n, 1, 5) +setup(ax) ax.plot(range(0, 5), [0]*5, color='white') ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) -ax.text(0.0, 0.2, "ticker.IndexLocator(base=0.5, offset=0.25)", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.IndexLocator(base=0.5, offset=0.25)", + family=family, fontsize=fontsize, transform=ax.transAxes) # Auto Locator -ax = axs[5] +ax = plt.subplot(n, 1, 6) +setup(ax) ax.xaxis.set_major_locator(ticker.AutoLocator()) ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) -ax.text(0.0, 0.2, "ticker.AutoLocator()", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.AutoLocator()", + family=family, fontsize=fontsize, transform=ax.transAxes) # MaxN Locator -ax = axs[6] +ax = plt.subplot(n, 1, 7) +setup(ax) ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) -ax.text(0.0, 0.2, "ticker.MaxNLocator(n=4)", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.MaxNLocator(n=4)", + family=family, fontsize=fontsize, transform=ax.transAxes) # Log Locator -ax = axs[7] +ax = plt.subplot(n, 1, 8) +setup(ax) ax.set_xlim(10**3, 10**10) ax.set_xscale('log') ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) -ax.text(0.0, 0.2, "ticker.LogLocator(base=10, numticks=15)", - fontfamily='Source Code Pro', transform=ax.transAxes, in_layout=False) +ax.text(0.0, 0.1, "ticker.LogLocator(base=10, numticks=15)", + family=family, fontsize=fontsize, transform=ax.transAxes) + +# Push the top of the top axes outside the figure because we only show the +# bottom spine. +plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=1.05) -fig.savefig("../figures/tick-locators.pdf") +plt.savefig("../figures/tick-locators.pdf", transparent=True) +# plt.show() From cfa709d50feb0e5dfb4988ebd82ce560c27e4ca6 Mon Sep 17 00:00:00 2001 From: Jamal Mustafa Date: Thu, 30 Mar 2023 17:47:06 -0700 Subject: [PATCH 7/7] replace `add_gridspec` with `plt.subplots` --- scripts/extents.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/scripts/extents.py b/scripts/extents.py index ba424d2..c3bec1d 100644 --- a/scripts/extents.py +++ b/scripts/extents.py @@ -17,10 +17,9 @@ Z = np.arange(5*5).reshape(5, 5) -fig = plt.figure(figsize=(8, 5)) -gs = fig.add_gridspec(2, 2) +(fig, axs) = plt.subplots(figsize=(8, 5), nrows=2, ncols=2) -ax = fig.add_subplot(gs[0, 0]) +ax = axs[0, 0] ax.imshow(Z, extent=[0, 10, 0, 5], interpolation="nearest", origin="upper") ax.set_xlim(-1, 11), ax.set_xticks([]) ax.set_ylim(-1, 6), ax.set_yticks([0, 5]) @@ -31,7 +30,7 @@ ax.text(5.0, -0.5, "extent=[0,10,0,5]", ha="center", va="center", color="black", size="large") -ax = fig.add_subplot(gs[1, 0]) +ax = axs[1, 0] ax.imshow(Z, extent=[0, 10, 0, 5], interpolation="nearest", origin="lower") ax.set_xlim(-1, 11), ax.set_xticks([0, 10]) ax.set_ylim(-1, 6), ax.set_yticks([0, 5]) @@ -43,7 +42,7 @@ ax.text(5.0, -0.5, "extent=[0,10,0,5]", ha="center", va="center", color="black", size="large") -ax = fig.add_subplot(gs[1, 1]) +ax = axs[1, 1] ax.imshow(Z, extent=[10, 0, 0, 5], interpolation="nearest", origin="lower") ax.set_xlim(-1, 11), ax.set_xticks([0, 10]) ax.set_ylim(-1, 6), ax.set_yticks([]) @@ -54,7 +53,7 @@ ax.text(5.0, -0.5, "extent=[10,0,0,5]", ha="center", va="center", color="black", size="large") -ax = fig.add_subplot(gs[0, 1]) +ax = axs[0, 1] ax.imshow(Z, extent=[10, 0, 0, 5], interpolation="nearest", origin="upper") ax.set_xlim(-1, 11), ax.set_xticks([]) ax.set_ylim(-1, 6), ax.set_yticks([])