Skip to content

Update annotate_explain.py #25674

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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
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
29 changes: 25 additions & 4 deletions galleries/examples/userdemo/annotate_explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,41 @@

import matplotlib.patches as mpatches

# Create a figure with four subplots arranged in a 2x2 grid
fig, axs = plt.subplots(2, 2)

# Define the x and y coordinates of two points
x1, y1 = 0.3, 0.3
x2, y2 = 0.7, 0.7

# First subplot: connect arrow style
ax = axs.flat[0]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)
ax.plot([x1, x2], [y1, y2], ".") # plot the two points
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2) # create ellipse patch
ax.add_artist(el) # add the ellipse patch to the plot

# add an arrow connecting the two points with the connect style
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
arrowprops=dict(arrowstyle="-",
color="0.5",
patchB=None,
patchB=None, # Do not use any patch
shrinkB=0,
connectionstyle="arc3,rad=0.3",
),
)

# add a text label to the subplot
ax.text(.05, .95, "connect", transform=ax.transAxes, ha="left", va="top")

# Second subplot: clip arrow style
ax = axs.flat[1]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)

# add an arrow connecting the two points with the clip style
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
Expand All @@ -45,10 +56,13 @@
)
ax.text(.05, .95, "clip", transform=ax.transAxes, ha="left", va="top")

# Third subplot: shrink arrow style
ax = axs.flat[2]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)

# add an arrow connecting the two points with the shrink style
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
Expand All @@ -61,10 +75,13 @@
)
ax.text(.05, .95, "shrink", transform=ax.transAxes, ha="left", va="top")

# Fourth subplot: fancy arrow style
ax = axs.flat[3]
ax.plot([x1, x2], [y1, y2], ".")
el = mpatches.Ellipse((x1, y1), 0.3, 0.4, angle=30, alpha=0.2)
ax.add_artist(el)

# Add an arrow connecting the two points with the fancy style
ax.annotate("",
xy=(x1, y1), xycoords='data',
xytext=(x2, y2), textcoords='data',
Expand All @@ -75,9 +92,13 @@
connectionstyle="arc3,rad=0.3",
),
)

# Add text to the plot to label the arrow as "connect"
ax.text(.05, .95, "mutate", transform=ax.transAxes, ha="left", va="top")

# Set the limits of the plot and remove the tick marks
for ax in axs.flat:
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[], aspect=1)

# Show the plot
plt.show()