diff --git a/Makefile b/Makefile index 530fc09..4a7642a 100644 --- a/Makefile +++ b/Makefile @@ -14,10 +14,19 @@ 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 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 .PHONY: cheatsheets cheatsheets: 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 ------------------------------------------------------------------ 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 025378a..4d86ba0 100644 --- a/scripts/advanced-plots.py +++ b/scripts/advanced-plots.py @@ -3,31 +3,37 @@ # 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 -d = 0.01 -ax = fig.add_axes([d, d, 1-2*d, 1-2*d]) + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', +]) + + +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", 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/advanced-step.pdf") -ax.clear() +ax.step(X, Y, color="C1") +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, @@ -35,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, @@ -57,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) @@ -144,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/annotation-connection-styles.py b/scripts/annotation-connection-styles.py index 64fa300..81908d7 100644 --- a/scripts/annotation-connection-styles.py +++ b/scripts/annotation-connection-styles.py @@ -1,6 +1,16 @@ +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', +]) +mpl.rc('lines', markersize=3) + + def demo_con_style(ax, connectionstyle): x1, y1 = 0.3, 0.2 x2, y2 = 0.8, 0.6 @@ -8,30 +18,27 @@ 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=5, shrinkB=5, + arrowprops=dict(arrowstyle="->", lw=0.5, color="0.5", + shrinkA=3, shrinkB=3, 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 d7a0c35..5dd8598 100644 --- a/scripts/basic-plots.py +++ b/scripts/basic-plots.py @@ -3,83 +3,80 @@ # 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 -d = 0.01 -ax = fig.add_axes([d, d, 1 - 2 * d, 1 - 2 * d]) + +mpl.style.use([ + pathlib.Path(__file__).parent/'../styles/base.mplstyle', + pathlib.Path(__file__).parent/'../styles/plotlet.mplstyle', +]) + + +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", 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/basic-plot.pdf") -ax.clear() - -# Basic line plot (color)blaPwd +ax.plot(X, Y, color="C1") +ax.grid() +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", 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/basic-plot-color.pdf") -ax.clear() +Y = 4 + 2*np.sin(2*X) +ax.plot(X, Y, color="black") +ax.grid() +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(linewidth=0.125) -plt.savefig("../figures/basic-scatter.pdf") -ax.clear() +ax.grid() +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(linewidth=0.125) -plt.savefig("../figures/basic-bar.pdf") -ax.clear() +ax.grid() +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) @@ -87,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(linewidth=0.125, color="0.75") -plt.savefig("../figures/basic-fill.pdf") -ax.clear() +ax.grid(color="0.75") +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(linewidth=0.125, color="0.75") -plt.savefig("../figures/basic-quiver.pdf") -ax.clear() +ax.grid(color="0.75") +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..c3bec1d 100644 --- a/scripts/extents.py +++ b/scripts/extents.py @@ -2,17 +2,24 @@ # 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)) +(fig, axs) = plt.subplots(figsize=(8, 5), nrows=2, ncols=2) -ax = fig.add_subplot(2, 2, 1) +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]) @@ -23,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(2, 2, 3) +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]) @@ -35,8 +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(2, 2, 4) +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([]) @@ -46,10 +52,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 = 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([]) @@ -59,8 +63,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/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/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/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/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-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 new file mode 100644 index 0000000..ea0c81e --- /dev/null +++ b/styles/base.mplstyle @@ -0,0 +1,5 @@ +figure.constrained_layout.use: True +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 new file mode 100644 index 0000000..e637a9c --- /dev/null +++ b/styles/plotlet-grid.mplstyle @@ -0,0 +1,7 @@ +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 new file mode 100644 index 0000000..ff46026 --- /dev/null +++ b/styles/plotlet.mplstyle @@ -0,0 +1,21 @@ +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 + +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 new file mode 100644 index 0000000..ba56aa8 --- /dev/null +++ b/styles/ticks.mplstyle @@ -0,0 +1,20 @@ +savefig.transparent: True + +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.size: 2 +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