diff --git a/examples/axisartist/demo_axisline_style.py b/examples/axisartist/demo_axisline_style.py index 0ddabcb15cb9..fc61375147e4 100644 --- a/examples/axisartist/demo_axisline_style.py +++ b/examples/axisartist/demo_axisline_style.py @@ -10,23 +10,23 @@ import matplotlib.pyplot as plt import numpy as np -if 1: - fig = plt.figure() - ax = SubplotZero(fig, 111) - fig.add_subplot(ax) - for direction in ["xzero", "yzero"]: - # adds arrows at the ends of each axis - ax.axis[direction].set_axisline_style("-|>") +fig = plt.figure() +ax = SubplotZero(fig, 111) +fig.add_subplot(ax) - # adds X and Y-axis from the origin - ax.axis[direction].set_visible(True) +for direction in ["xzero", "yzero"]: + # adds arrows at the ends of each axis + ax.axis[direction].set_axisline_style("-|>") - for direction in ["left", "right", "bottom", "top"]: - # hides borders - ax.axis[direction].set_visible(False) + # adds X and Y-axis from the origin + ax.axis[direction].set_visible(True) - x = np.linspace(-0.5, 1., 100) - ax.plot(x, np.sin(x*np.pi)) +for direction in ["left", "right", "bottom", "top"]: + # hides borders + ax.axis[direction].set_visible(False) - plt.show() +x = np.linspace(-0.5, 1., 100) +ax.plot(x, np.sin(x*np.pi)) + +plt.show() diff --git a/examples/axisartist/demo_curvelinear_grid.py b/examples/axisartist/demo_curvelinear_grid.py index 8fb4ef6b9d95..512ca31bb99c 100644 --- a/examples/axisartist/demo_curvelinear_grid.py +++ b/examples/axisartist/demo_curvelinear_grid.py @@ -131,7 +131,7 @@ def curvelinear_test2(fig): ax1.grid(True, zorder=0) -if 1: +if __name__ == "__main__": fig = plt.figure(figsize=(7, 4)) curvelinear_test1(fig) diff --git a/examples/axisartist/demo_curvelinear_grid2.py b/examples/axisartist/demo_curvelinear_grid2.py index ca9fdbdc784f..4fb2872bd0f5 100644 --- a/examples/axisartist/demo_curvelinear_grid2.py +++ b/examples/axisartist/demo_curvelinear_grid2.py @@ -64,7 +64,7 @@ def inv_tr(x, y): grid_helper.grid_finder.grid_locator2._nbins = 6 -if 1: +if __name__ == "__main__": fig = plt.figure(figsize=(7, 4)) curvelinear_test1(fig) plt.show() diff --git a/examples/event_handling/pick_event_demo.py b/examples/event_handling/pick_event_demo.py index 4f2a924e1d23..7198bff484a5 100644 --- a/examples/event_handling/pick_event_demo.py +++ b/examples/event_handling/pick_event_demo.py @@ -74,7 +74,9 @@ def pick_handler(event): import numpy as np from numpy.random import rand -if 1: # simple picking, lines, rectangles and text + +def pick_simple(): + # simple picking, lines, rectangles and text fig, (ax1, ax2) = plt.subplots(2, 1) ax1.set_title('click on points, rectangles or text', picker=True) ax1.set_ylabel('ylabel', picker=True, bbox=dict(facecolor='red')) @@ -101,7 +103,9 @@ def onpick1(event): fig.canvas.mpl_connect('pick_event', onpick1) -if 1: # picking with a custom hit test function + +def pick_custom_hit(): + # picking with a custom hit test function # you can define custom pickers by setting picker to a callable # function. The function has the signature # @@ -142,7 +146,8 @@ def onpick2(event): fig.canvas.mpl_connect('pick_event', onpick2) -if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection) +def pick_scatter_plot(): + # picking on a scatter plot (matplotlib.collections.RegularPolyCollection) x, y, c, s = rand(4, 100) @@ -155,7 +160,9 @@ def onpick3(event): #fig.savefig('pscoll.eps') fig.canvas.mpl_connect('pick_event', onpick3) -if 1: # picking images (matplotlib.image.AxesImage) + +def pick_image(): + # picking images (matplotlib.image.AxesImage) fig, ax = plt.subplots() im1 = ax.imshow(rand(10, 5), extent=(1, 2, 1, 2), picker=True) im2 = ax.imshow(rand(5, 10), extent=(3, 4, 1, 2), picker=True) @@ -173,4 +180,9 @@ def onpick4(event): fig.canvas.mpl_connect('pick_event', onpick4) -plt.show() +if __name__ == '__main__': + pick_simple() + pick_custom_hit() + pick_scatter_plot() + pick_image() + plt.show() diff --git a/examples/misc/demo_agg_filter.py b/examples/misc/demo_agg_filter.py index 6a1d2c4e6588..7635566cc028 100644 --- a/examples/misc/demo_agg_filter.py +++ b/examples/misc/demo_agg_filter.py @@ -307,7 +307,7 @@ def light_filter_pie(ax): shadow.set_zorder(pies[0][0].get_zorder() - 0.1) -if 1: +if __name__ == "__main__": plt.figure(figsize=(6, 6)) plt.subplots_adjust(left=0.05, right=0.95) diff --git a/examples/misc/patheffect_demo.py b/examples/misc/patheffect_demo.py index 60c24b7ea6b5..4455d63cecf6 100644 --- a/examples/misc/patheffect_demo.py +++ b/examples/misc/patheffect_demo.py @@ -8,40 +8,39 @@ import matplotlib.patheffects as PathEffects import numpy as np -if 1: - plt.figure(figsize=(8, 3)) - ax1 = plt.subplot(131) - ax1.imshow([[1, 2], [2, 3]]) - txt = ax1.annotate("test", (1., 1.), (0., 0), - arrowprops=dict(arrowstyle="->", - connectionstyle="angle3", lw=2), - size=20, ha="center", - path_effects=[PathEffects.withStroke(linewidth=3, - foreground="w")]) - txt.arrow_patch.set_path_effects([ - PathEffects.Stroke(linewidth=5, foreground="w"), - PathEffects.Normal()]) - - pe = [PathEffects.withStroke(linewidth=3, - foreground="w")] - ax1.grid(True, linestyle="-", path_effects=pe) - - ax2 = plt.subplot(132) - arr = np.arange(25).reshape((5, 5)) - ax2.imshow(arr) - cntr = ax2.contour(arr, colors="k") - - plt.setp(cntr.collections, path_effects=[ - PathEffects.withStroke(linewidth=3, foreground="w")]) - - clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True) - plt.setp(clbls, path_effects=[ - PathEffects.withStroke(linewidth=3, foreground="w")]) - - # shadow as a path effect - ax3 = plt.subplot(133) - p1, = ax3.plot([0, 1], [0, 1]) - leg = ax3.legend([p1], ["Line 1"], fancybox=True, loc='upper left') - leg.legendPatch.set_path_effects([PathEffects.withSimplePatchShadow()]) - - plt.show() +plt.figure(figsize=(8, 3)) +ax1 = plt.subplot(131) +ax1.imshow([[1, 2], [2, 3]]) +txt = ax1.annotate("test", (1., 1.), (0., 0), + arrowprops=dict(arrowstyle="->", + connectionstyle="angle3", lw=2), + size=20, ha="center", + path_effects=[PathEffects.withStroke(linewidth=3, + foreground="w")]) +txt.arrow_patch.set_path_effects([ + PathEffects.Stroke(linewidth=5, foreground="w"), + PathEffects.Normal()]) + +pe = [PathEffects.withStroke(linewidth=3, + foreground="w")] +ax1.grid(True, linestyle="-", path_effects=pe) + +ax2 = plt.subplot(132) +arr = np.arange(25).reshape((5, 5)) +ax2.imshow(arr) +cntr = ax2.contour(arr, colors="k") + +plt.setp(cntr.collections, path_effects=[ + PathEffects.withStroke(linewidth=3, foreground="w")]) + +clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True) +plt.setp(clbls, path_effects=[ + PathEffects.withStroke(linewidth=3, foreground="w")]) + +# shadow as a path effect +ax3 = plt.subplot(133) +p1, = ax3.plot([0, 1], [0, 1]) +leg = ax3.legend([p1], ["Line 1"], fancybox=True, loc='upper left') +leg.legendPatch.set_path_effects([PathEffects.withSimplePatchShadow()]) + +plt.show() diff --git a/examples/text_labels_and_annotations/demo_annotation_box.py b/examples/text_labels_and_annotations/demo_annotation_box.py index 0518d7d68b64..9ffbc65b021a 100644 --- a/examples/text_labels_and_annotations/demo_annotation_box.py +++ b/examples/text_labels_and_annotations/demo_annotation_box.py @@ -13,86 +13,85 @@ from matplotlib.cbook import get_sample_data -if 1: - fig, ax = plt.subplots() - - # Define a 1st position to annotate (display it with a marker) - xy = (0.5, 0.7) - ax.plot(xy[0], xy[1], ".r") - - # Annotate the 1st position with a text box ('Test 1') - offsetbox = TextArea("Test 1", minimumdescent=False) - - ab = AnnotationBbox(offsetbox, xy, - xybox=(-20, 40), - xycoords='data', - boxcoords="offset points", - arrowprops=dict(arrowstyle="->")) - ax.add_artist(ab) - - # Annotate the 1st position with another text box ('Test') - offsetbox = TextArea("Test", minimumdescent=False) - - ab = AnnotationBbox(offsetbox, xy, - xybox=(1.02, xy[1]), - xycoords='data', - boxcoords=("axes fraction", "data"), - box_alignment=(0., 0.5), - arrowprops=dict(arrowstyle="->")) - ax.add_artist(ab) - - # Define a 2nd position to annotate (don't display with a marker this time) - xy = [0.3, 0.55] - - # Annotate the 2nd position with a circle patch - da = DrawingArea(20, 20, 0, 0) - p = Circle((10, 10), 10) - da.add_artist(p) - - ab = AnnotationBbox(da, xy, - xybox=(1.02, xy[1]), - xycoords='data', - boxcoords=("axes fraction", "data"), - box_alignment=(0., 0.5), - arrowprops=dict(arrowstyle="->")) - - ax.add_artist(ab) - - # Annotate the 2nd position with an image (a generated array of pixels) - arr = np.arange(100).reshape((10, 10)) - im = OffsetImage(arr, zoom=2) - im.image.axes = ax - - ab = AnnotationBbox(im, xy, - xybox=(-50., 50.), - xycoords='data', - boxcoords="offset points", - pad=0.3, - arrowprops=dict(arrowstyle="->")) - - ax.add_artist(ab) - - # Annotate the 2nd position with another image (a Grace Hopper portrait) - fn = get_sample_data("grace_hopper.png", asfileobj=False) - arr_img = plt.imread(fn, format='png') - - imagebox = OffsetImage(arr_img, zoom=0.2) - imagebox.image.axes = ax - - ab = AnnotationBbox(imagebox, xy, - xybox=(120., -80.), - xycoords='data', - boxcoords="offset points", - pad=0.5, - arrowprops=dict( - arrowstyle="->", - connectionstyle="angle,angleA=0,angleB=90,rad=3") - ) - - ax.add_artist(ab) - - # Fix the display limits to see everything - ax.set_xlim(0, 1) - ax.set_ylim(0, 1) - - plt.show() +fig, ax = plt.subplots() + +# Define a 1st position to annotate (display it with a marker) +xy = (0.5, 0.7) +ax.plot(xy[0], xy[1], ".r") + +# Annotate the 1st position with a text box ('Test 1') +offsetbox = TextArea("Test 1", minimumdescent=False) + +ab = AnnotationBbox(offsetbox, xy, + xybox=(-20, 40), + xycoords='data', + boxcoords="offset points", + arrowprops=dict(arrowstyle="->")) +ax.add_artist(ab) + +# Annotate the 1st position with another text box ('Test') +offsetbox = TextArea("Test", minimumdescent=False) + +ab = AnnotationBbox(offsetbox, xy, + xybox=(1.02, xy[1]), + xycoords='data', + boxcoords=("axes fraction", "data"), + box_alignment=(0., 0.5), + arrowprops=dict(arrowstyle="->")) +ax.add_artist(ab) + +# Define a 2nd position to annotate (don't display with a marker this time) +xy = [0.3, 0.55] + +# Annotate the 2nd position with a circle patch +da = DrawingArea(20, 20, 0, 0) +p = Circle((10, 10), 10) +da.add_artist(p) + +ab = AnnotationBbox(da, xy, + xybox=(1.02, xy[1]), + xycoords='data', + boxcoords=("axes fraction", "data"), + box_alignment=(0., 0.5), + arrowprops=dict(arrowstyle="->")) + +ax.add_artist(ab) + +# Annotate the 2nd position with an image (a generated array of pixels) +arr = np.arange(100).reshape((10, 10)) +im = OffsetImage(arr, zoom=2) +im.image.axes = ax + +ab = AnnotationBbox(im, xy, + xybox=(-50., 50.), + xycoords='data', + boxcoords="offset points", + pad=0.3, + arrowprops=dict(arrowstyle="->")) + +ax.add_artist(ab) + +# Annotate the 2nd position with another image (a Grace Hopper portrait) +fn = get_sample_data("grace_hopper.png", asfileobj=False) +arr_img = plt.imread(fn, format='png') + +imagebox = OffsetImage(arr_img, zoom=0.2) +imagebox.image.axes = ax + +ab = AnnotationBbox(imagebox, xy, + xybox=(120., -80.), + xycoords='data', + boxcoords="offset points", + pad=0.5, + arrowprops=dict( + arrowstyle="->", + connectionstyle="angle,angleA=0,angleB=90,rad=3") + ) + +ax.add_artist(ab) + +# Fix the display limits to see everything +ax.set_xlim(0, 1) +ax.set_ylim(0, 1) + +plt.show() diff --git a/examples/text_labels_and_annotations/demo_text_path.py b/examples/text_labels_and_annotations/demo_text_path.py index 69dc32386c9b..f3df20ca57a5 100644 --- a/examples/text_labels_and_annotations/demo_text_path.py +++ b/examples/text_labels_and_annotations/demo_text_path.py @@ -56,7 +56,7 @@ def draw(self, renderer=None): mpatches.PathPatch.draw(self, renderer) -if 1: +if __name__ == "__main__": usetex = plt.rcParams["text.usetex"] diff --git a/examples/text_labels_and_annotations/demo_text_rotation_mode.py b/examples/text_labels_and_annotations/demo_text_rotation_mode.py index 4eb8ae528b43..0d161aada8c5 100644 --- a/examples/text_labels_and_annotations/demo_text_rotation_mode.py +++ b/examples/text_labels_and_annotations/demo_text_rotation_mode.py @@ -42,7 +42,7 @@ def test_rotation_mode(fig, mode, subplot_location): i += 1 -if 1: +if __name__ == "__main__": import matplotlib.pyplot as plt fig = plt.figure(figsize=(5.5, 4)) test_rotation_mode(fig, "default", 121)