From 6b842125cdd126b85bb50d1c48b4ce2ad13288fb Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Fri, 25 Jun 2021 00:11:23 +0200 Subject: [PATCH] Cleanup plot types --- plot_types/arrays/contourf.py | 8 ++++---- plot_types/arrays/imshow.py | 6 ++---- plot_types/arrays/pcolormesh.py | 3 ++- plot_types/arrays/quiver.py | 10 +++++----- plot_types/basic/bar.py | 6 +++--- plot_types/basic/pie.py | 6 +++--- plot_types/basic/plot.py | 6 +++--- plot_types/basic/scatter_plot.py | 9 +++++---- plot_types/basic/stem.py | 6 +++--- plot_types/basic/step.py | 6 +++--- plot_types/stats/barbs.py | 7 +++---- plot_types/stats/errorbar_plot.py | 10 +++++----- plot_types/stats/eventplot.py | 4 ++-- plot_types/stats/hexbin.py | 2 +- plot_types/stats/hist2d.py | 2 +- plot_types/stats/hist_plot.py | 6 +++--- plot_types/stats/violin.py | 6 +++--- plot_types/unstructured/tricontour.py | 10 +++++----- plot_types/unstructured/tripcolor.py | 4 ++-- plot_types/unstructured/triplot.py | 4 ++-- 20 files changed, 60 insertions(+), 61 deletions(-) diff --git a/plot_types/arrays/contourf.py b/plot_types/arrays/contourf.py index c279d2b15fbf..66f2b90c8739 100644 --- a/plot_types/arrays/contourf.py +++ b/plot_types/arrays/contourf.py @@ -12,14 +12,14 @@ # make data 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) +Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2) Z = Z - Z.min() -levs = np.linspace(np.min(Z), np.max(Z), 7) +levels = np.linspace(np.min(Z), np.max(Z), 7) # plot fig, ax = plt.subplots() -plt.contourf(X, Y, Z, levels=levs) -plt.contour(X, Y, Z, levels=levs, colors="white", linewidths=0.5) +ax.contourf(X, Y, Z, levels=levels) +ax.contour(X, Y, Z, levels=levels, colors="white", linewidths=0.5) plt.show() diff --git a/plot_types/arrays/imshow.py b/plot_types/arrays/imshow.py index 76d3726b5c15..36a6915606af 100644 --- a/plot_types/arrays/imshow.py +++ b/plot_types/arrays/imshow.py @@ -19,10 +19,8 @@ # plot fig, ax = plt.subplots() +ax.grid(False) -ax.imshow(Z, extent=[0, 8, 0, 8], interpolation="nearest", - cmap=plt.get_cmap('Blues'), vmin=0, vmax=1.6) - -ax.set(xticks=[], yticks=[]) +ax.imshow(Z) plt.show() diff --git a/plot_types/arrays/pcolormesh.py b/plot_types/arrays/pcolormesh.py index 8c409eb93fc0..86d34ffbe6cd 100644 --- a/plot_types/arrays/pcolormesh.py +++ b/plot_types/arrays/pcolormesh.py @@ -27,7 +27,8 @@ # plot fig, ax = plt.subplots() +ax.grid(False) -ax.pcolormesh(X, Y, Z, vmin=0, vmax=1.5, shading='nearest') +ax.pcolormesh(X, Y, Z, vmin=0, vmax=1.5) plt.show() diff --git a/plot_types/arrays/quiver.py b/plot_types/arrays/quiver.py index 0d7b6613ad9a..f1a618850f90 100644 --- a/plot_types/arrays/quiver.py +++ b/plot_types/arrays/quiver.py @@ -11,15 +11,15 @@ plt.style.use('mpl_plot_gallery') # make data -T = np.linspace(0, 2*np.pi, 8) -X, Y = 4 + 1 * np.cos(T), 4 + 1 * np.sin(T) -U, V = 1.5 * np.cos(T), 1.5 * np.sin(T) +phi = np.linspace(0, 2 * np.pi, 8) +x, y = 4 + 1 * np.cos(phi), 4 + 1 * np.sin(phi) +u, v = 1.5 * np.cos(phi), 1.5 * np.sin(phi) # plot fig, ax = plt.subplots() -plt.quiver(X, Y, U, V, color="C0", angles='xy', - scale_units='xy', scale=0.5, width=.05) +ax.quiver(x, y, u, v, color="C0", angles='xy', + scale_units='xy', scale=0.5, width=.05) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/basic/bar.py b/plot_types/basic/bar.py index 240a15fac263..6c933b018935 100644 --- a/plot_types/basic/bar.py +++ b/plot_types/basic/bar.py @@ -11,13 +11,13 @@ # make data: np.random.seed(3) -X = 0.5 + np.arange(8) -Y = np.random.uniform(2, 7, len(X)) +x = 0.5 + np.arange(8) +y = np.random.uniform(2, 7, len(x)) # plot fig, ax = plt.subplots() -ax.bar(X, Y, width=1, edgecolor="white", linewidth=0.7) +ax.bar(x, y, width=1, edgecolor="white", linewidth=0.7) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/basic/pie.py b/plot_types/basic/pie.py index 9beb0d490dc2..e20b6c5001db 100644 --- a/plot_types/basic/pie.py +++ b/plot_types/basic/pie.py @@ -12,12 +12,12 @@ # make data -X = [1, 2, 3, 4] -colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(X))) +x = [1, 2, 3, 4] +colors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(x))) # plot fig, ax = plt.subplots() -ax.pie(X, colors=colors, radius=3, center=(4, 4), +ax.pie(x, colors=colors, radius=3, center=(4, 4), wedgeprops={"linewidth": 1, "edgecolor": "white"}, frame=True) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), diff --git a/plot_types/basic/plot.py b/plot_types/basic/plot.py index 781959918d9c..cd608f2c33f9 100644 --- a/plot_types/basic/plot.py +++ b/plot_types/basic/plot.py @@ -12,13 +12,13 @@ plt.style.use('mpl_plot_gallery') # make data -X = np.linspace(0, 10, 100) -Y = 4 + 2 * np.sin(2 * X) +x = np.linspace(0, 10, 100) +y = 4 + 2 * np.sin(2 * x) # plot fig, ax = plt.subplots() -ax.plot(X, Y, linewidth=2.0) +ax.plot(x, y, linewidth=2.0) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/basic/scatter_plot.py b/plot_types/basic/scatter_plot.py index 064dfef021d3..1b0bbd104ac5 100644 --- a/plot_types/basic/scatter_plot.py +++ b/plot_types/basic/scatter_plot.py @@ -12,15 +12,16 @@ # make the data np.random.seed(3) -X = 4 + np.random.normal(0, 2, 24) -Y = 4 + np.random.normal(0, 2, len(X)) +x = 4 + np.random.normal(0, 2, 24) +y = 4 + np.random.normal(0, 2, len(x)) # size and color: -S = np.random.uniform(15, 80, len(X)) +sizes = np.random.uniform(15, 80, len(x)) +colors = np.random.uniform(15, 80, len(x)) # plot fig, ax = plt.subplots() -ax.scatter(X, Y, s=S, c=-S, cmap=plt.get_cmap('Blues'), vmin=-100, vmax=0) +ax.scatter(x, y, s=sizes, c=colors, vmin=-100, vmax=0) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/basic/stem.py b/plot_types/basic/stem.py index a905495f2c22..22bcdde73861 100644 --- a/plot_types/basic/stem.py +++ b/plot_types/basic/stem.py @@ -12,13 +12,13 @@ # make data np.random.seed(3) -X = 0.5 + np.arange(8) -Y = np.random.uniform(2, 7, len(X)) +x = 0.5 + np.arange(8) +y = np.random.uniform(2, 7, len(x)) # plot fig, ax = plt.subplots() -ax.stem(X, Y, bottom=0, linefmt="-", markerfmt="d") +ax.stem(x, y) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/basic/step.py b/plot_types/basic/step.py index 39b57857959b..4ec9a96544ac 100644 --- a/plot_types/basic/step.py +++ b/plot_types/basic/step.py @@ -12,13 +12,13 @@ # make data np.random.seed(3) -X = 0.5 + np.arange(8) -Y = np.random.uniform(2, 7, len(X)) +x = 0.5 + np.arange(8) +y = np.random.uniform(2, 7, len(x)) # plot fig, ax = plt.subplots() -ax.step(X, Y, linewidth=2.5) +ax.step(x, y, linewidth=2.5) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/stats/barbs.py b/plot_types/stats/barbs.py index 82379a2f56d5..727b41f19244 100644 --- a/plot_types/stats/barbs.py +++ b/plot_types/stats/barbs.py @@ -14,14 +14,13 @@ 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) +U = np.zeros_like(X) +V = -np.ones_like(X) * np.linspace(50, 100, 3) # plot: fig, ax = plt.subplots() -ax.barbs(X, Y, U, V, barbcolor="C0", flagcolor="C0", - length=10, linewidth=1.5) +ax.barbs(X, Y, U, V, barbcolor="C0", flagcolor="C0", length=10, linewidth=1.5) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/stats/errorbar_plot.py b/plot_types/stats/errorbar_plot.py index 8a9a33b8d435..39ffd4af597d 100644 --- a/plot_types/stats/errorbar_plot.py +++ b/plot_types/stats/errorbar_plot.py @@ -1,6 +1,6 @@ """ ========================== -errorbar(X, Y, xerr, yerr) +errorbar(x, y, yerr, xerr) ========================== See `~matplotlib.axes.Axes.errorbar`. @@ -12,14 +12,14 @@ # make data: np.random.seed(1) -X = [2, 4, 6] -Y = [4, 5, 4] -E = np.random.uniform(0.5, 1.5, 3) +x = [2, 4, 6] +y = [4, 5, 4] +yerr = np.random.uniform(0.5, 1.5, 3) # plot: fig, ax = plt.subplots() -ax.errorbar(X, Y, E, linewidth=2, capsize=6) +ax.errorbar(x, y, yerr, linewidth=2, capsize=6) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/stats/eventplot.py b/plot_types/stats/eventplot.py index 65e688dbf2d2..0552d83e599c 100644 --- a/plot_types/stats/eventplot.py +++ b/plot_types/stats/eventplot.py @@ -12,13 +12,13 @@ # make data: np.random.seed(1) -X = [2, 4, 6] +x = [2, 4, 6] D = np.random.gamma(4, size=(3, 50)) # plot: fig, ax = plt.subplots() -ax.eventplot(D, orientation="vertical", lineoffsets=X, linewidth=0.75) +ax.eventplot(D, orientation="vertical", lineoffsets=x, linewidth=0.75) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/stats/hexbin.py b/plot_types/stats/hexbin.py index 5edded809d9e..87a3594ab009 100644 --- a/plot_types/stats/hexbin.py +++ b/plot_types/stats/hexbin.py @@ -13,7 +13,7 @@ # make data: correlated + noise np.random.seed(1) x = np.random.randn(5000) -y = 1.2 * x + np.random.randn(5000)/3 +y = 1.2 * x + np.random.randn(5000) / 3 # plot: fig, ax = plt.subplots() diff --git a/plot_types/stats/hist2d.py b/plot_types/stats/hist2d.py index bf7244290096..90f60eda4a34 100644 --- a/plot_types/stats/hist2d.py +++ b/plot_types/stats/hist2d.py @@ -13,7 +13,7 @@ # make data: correlated + noise np.random.seed(1) x = np.random.randn(5000) -y = 1.2 * x + np.random.randn(5000)/3 +y = 1.2 * x + np.random.randn(5000) / 3 # plot: fig, ax = plt.subplots() diff --git a/plot_types/stats/hist_plot.py b/plot_types/stats/hist_plot.py index f9759c8c61b9..facdfb13cd56 100644 --- a/plot_types/stats/hist_plot.py +++ b/plot_types/stats/hist_plot.py @@ -1,6 +1,6 @@ """ ======= -hist(X) +hist(x) ======= See `~matplotlib.axes.Axes.hist`. @@ -12,12 +12,12 @@ # make data np.random.seed(1) -X = 4 + np.random.normal(0, 1.5, 200) +x = 4 + np.random.normal(0, 1.5, 200) # plot: fig, ax = plt.subplots() -ax.hist(X, bins=8, linewidth=0.5, edgecolor="white") +ax.hist(x, bins=8, linewidth=0.5, edgecolor="white") ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/stats/violin.py b/plot_types/stats/violin.py index 2e2fcade99bb..40b8f19341ab 100644 --- a/plot_types/stats/violin.py +++ b/plot_types/stats/violin.py @@ -17,10 +17,10 @@ # plot: fig, ax = plt.subplots() -VP = ax.violinplot(D, [2, 4, 6], widths=2, - showmeans=False, showmedians=False, showextrema=False) +vp = ax.violinplot(D, [2, 4, 6], widths=2, + showmeans=False, showmedians=False, showextrema=False) # styling: -for body in VP['bodies']: +for body in vp['bodies']: body.set_alpha(0.9) ax.set(xlim=(0, 8), xticks=np.arange(1, 8), ylim=(0, 8), yticks=np.arange(1, 8)) diff --git a/plot_types/unstructured/tricontour.py b/plot_types/unstructured/tricontour.py index 0782da54a7c0..4fb003c34a42 100644 --- a/plot_types/unstructured/tricontour.py +++ b/plot_types/unstructured/tricontour.py @@ -12,13 +12,13 @@ # make structured data 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) +Z = (1 - X/2. + X**5 + Y**3) * np.exp(-X**2 - Y**2) Z = Z - Z.min() # sample it to make unstructured x, y, z np.random.seed(1) -ysamp = np.random.randint(0, high=256, size=250) -xsamp = np.random.randint(0, high=256, size=250) +ysamp = np.random.randint(0, 256, size=250) +xsamp = np.random.randint(0, 256, size=250) y = Y[:, 0][ysamp] x = X[0, :][xsamp] z = Z[ysamp, xsamp] @@ -27,8 +27,8 @@ fig, ax = plt.subplots() ax.plot(x, y, '.k', alpha=0.5) -levs = np.linspace(np.min(Z), np.max(Z), 7) -ax.tricontourf(x, y, z, levels=levs) +levels = np.linspace(np.min(Z), np.max(Z), 7) +ax.tricontourf(x, y, z, levels=levels) ax.set(xlim=(-3, 3), ylim=(-3, 3)) diff --git a/plot_types/unstructured/tripcolor.py b/plot_types/unstructured/tripcolor.py index 2e7d2ade6e41..3c0cadfe440f 100644 --- a/plot_types/unstructured/tripcolor.py +++ b/plot_types/unstructured/tripcolor.py @@ -17,8 +17,8 @@ # sample it to make unstructured x, y, z np.random.seed(1) -ysamp = np.random.randint(0, high=256, size=250) -xsamp = np.random.randint(0, high=256, size=250) +ysamp = np.random.randint(0, 256, size=250) +xsamp = np.random.randint(0, 256, size=250) y = Y[:, 0][ysamp] x = X[0, :][xsamp] z = Z[ysamp, xsamp] diff --git a/plot_types/unstructured/triplot.py b/plot_types/unstructured/triplot.py index 25f16832025d..1d67d7e035c7 100644 --- a/plot_types/unstructured/triplot.py +++ b/plot_types/unstructured/triplot.py @@ -15,8 +15,8 @@ # sample it to make x, y, z np.random.seed(1) -ysamp = np.random.randint(0, high=256, size=250) -xsamp = np.random.randint(0, high=256, size=250) +ysamp = np.random.randint(0, 256, size=250) +xsamp = np.random.randint(0, 256, size=250) y = Y[:, 0][ysamp] x = X[0, :][xsamp]