Skip to content

Fix docs : change from issue #12191, remove "if 1:" blocks in examples #12260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions examples/axisartist/demo_axisline_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion examples/axisartist/demo_curvelinear_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/axisartist/demo_curvelinear_grid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
22 changes: 17 additions & 5 deletions examples/event_handling/pick_event_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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
#
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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()
2 changes: 1 addition & 1 deletion examples/misc/demo_agg_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
73 changes: 36 additions & 37 deletions examples/misc/patheffect_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
165 changes: 82 additions & 83 deletions examples/text_labels_and_annotations/demo_annotation_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion examples/text_labels_and_annotations/demo_text_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def draw(self, renderer=None):
mpatches.PathPatch.draw(self, renderer)


if 1:
if __name__ == "__main__":

usetex = plt.rcParams["text.usetex"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down