Skip to content

Commit f70bcb7

Browse files
committed
Simplify "artist reference" example.
In particular, add each artist directly to its own axes, instead of shifting each of them by some offset and grouping all of them together in a PatchCollection. Note that this makes the example no longer showcase the use of PatchCollection, but at least based on the docstring that was not the intent of the example anyways, and PatchCollection is showcased elsewhere (in the patch_collection.py example, in particular).
1 parent f5ff5d7 commit f70bcb7

File tree

2 files changed

+37
-86
lines changed

2 files changed

+37
-86
lines changed

Diff for: examples/shapes_and_collections/artist_reference.py

+34-86
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,25 @@
33
Reference for Matplotlib artists
44
================================
55
6-
This example displays several of Matplotlib's graphics primitives (artists)
7-
drawn using matplotlib API. A full list of artists and the documentation is
8-
available at :ref:`the artist API <artist-api>`.
6+
This example displays several of Matplotlib's graphics primitives (artists).
7+
A full list of artists is documented at :ref:`the artist API <artist-api>`.
8+
9+
See also :doc:`/gallery/shapes_and_collections/patch_collection`, which groups
10+
all artists into a single `.PatchCollection` instead.
911
1012
Copyright (c) 2010, Bartosz Telenczuk
1113
BSD License
1214
"""
13-
import matplotlib.pyplot as plt
14-
import numpy as np
15-
import matplotlib.path as mpath
15+
16+
import matplotlib as mpl
1617
import matplotlib.lines as mlines
1718
import matplotlib.patches as mpatches
18-
from matplotlib.collections import PatchCollection
19-
20-
21-
def label(xy, text):
22-
y = xy[1] - 0.15 # shift y-value for label so that it's below the artist
23-
plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14)
24-
25-
26-
fig, ax = plt.subplots()
27-
# create 3x3 grid to plot the artists
28-
grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T
29-
30-
patches = []
31-
32-
# add a circle
33-
circle = mpatches.Circle(grid[0], 0.1, ec="none")
34-
patches.append(circle)
35-
label(grid[0], "Circle")
36-
37-
# add a rectangle
38-
rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")
39-
patches.append(rect)
40-
label(grid[1], "Rectangle")
41-
42-
# add a wedge
43-
wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none")
44-
patches.append(wedge)
45-
label(grid[2], "Wedge")
46-
47-
# add a Polygon
48-
polygon = mpatches.RegularPolygon(grid[3], 5, radius=0.1)
49-
patches.append(polygon)
50-
label(grid[3], "Polygon")
51-
52-
# add an ellipse
53-
ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)
54-
patches.append(ellipse)
55-
label(grid[4], "Ellipse")
56-
57-
# add an arrow
58-
arrow = mpatches.Arrow(grid[5, 0] - 0.05, grid[5, 1] - 0.05, 0.1, 0.1,
59-
width=0.1)
60-
patches.append(arrow)
61-
label(grid[5], "Arrow")
19+
import matplotlib.path as mpath
20+
import matplotlib.pyplot as plt
6221

63-
# add a path patch
22+
# Prepare the data for the PathPatch below.
6423
Path = mpath.Path
65-
path_data = [
24+
codes, verts = zip(*[
6625
(Path.MOVETO, [0.018, -0.11]),
6726
(Path.CURVE4, [-0.031, -0.051]),
6827
(Path.CURVE4, [-0.115, 0.073]),
@@ -71,35 +30,28 @@ def label(xy, text):
7130
(Path.CURVE4, [0.043, 0.121]),
7231
(Path.CURVE4, [0.075, -0.005]),
7332
(Path.CURVE4, [0.035, -0.027]),
74-
(Path.CLOSEPOLY, [0.018, -0.11])]
75-
codes, verts = zip(*path_data)
76-
path = mpath.Path(verts + grid[6], codes)
77-
patch = mpatches.PathPatch(path)
78-
patches.append(patch)
79-
label(grid[6], "PathPatch")
80-
81-
# add a fancy box
82-
fancybox = mpatches.FancyBboxPatch(
83-
grid[7] - [0.025, 0.05], 0.05, 0.1,
84-
boxstyle=mpatches.BoxStyle("Round", pad=0.02))
85-
patches.append(fancybox)
86-
label(grid[7], "FancyBboxPatch")
87-
88-
# add a line
89-
x, y = ([-0.06, 0.0, 0.1], [0.05, -0.05, 0.05])
90-
line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)
91-
label(grid[8], "Line2D")
92-
93-
colors = np.linspace(0, 1, len(patches))
94-
collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)
95-
collection.set_array(colors)
96-
ax.add_collection(collection)
97-
ax.add_line(line)
98-
99-
plt.axis('equal')
100-
plt.axis('off')
101-
plt.tight_layout()
102-
33+
(Path.CLOSEPOLY, [0.018, -0.11])])
34+
35+
artists = [
36+
mpatches.Circle((0, 0), 0.1, ec="none"),
37+
mpatches.Rectangle((-0.025, -0.05), 0.05, 0.1, ec="none"),
38+
mpatches.Wedge((0, 0), 0.1, 30, 270, ec="none"),
39+
mpatches.RegularPolygon((0, 0), 5, radius=0.1),
40+
mpatches.Ellipse((0, 0), 0.2, 0.1),
41+
mpatches.Arrow(-0.05, -0.05, 0.1, 0.1, width=0.1),
42+
mpatches.PathPatch(mpath.Path(verts, codes), ec="none"),
43+
mpatches.FancyBboxPatch((-0.025, -0.05), 0.05, 0.1, ec="none",
44+
boxstyle=mpatches.BoxStyle("Round", pad=0.02)),
45+
mlines.Line2D([-0.06, 0.0, 0.1], [0.05, -0.05, 0.05], lw=5),
46+
]
47+
48+
axs = plt.figure(figsize=(6, 6), layout="constrained").subplots(3, 3)
49+
for i, (ax, artist) in enumerate(zip(axs.flat, artists)):
50+
artist.set(color=mpl.colormaps["hsv"](i / len(artists)))
51+
ax.add_artist(artist)
52+
ax.set(title=type(artist).__name__,
53+
aspect=1, xlim=(-.2, .2), ylim=(-.2, .2))
54+
ax.set_axis_off()
10355
plt.show()
10456

10557
#############################################################################
@@ -122,8 +74,4 @@ def label(xy, text):
12274
# - `matplotlib.patches.PathPatch`
12375
# - `matplotlib.patches.FancyBboxPatch`
12476
# - `matplotlib.patches.RegularPolygon`
125-
# - `matplotlib.collections`
126-
# - `matplotlib.collections.PatchCollection`
127-
# - `matplotlib.cm.ScalarMappable.set_array`
128-
# - `matplotlib.axes.Axes.add_collection`
129-
# - `matplotlib.axes.Axes.add_line`
77+
# - `matplotlib.axes.Axes.add_artist`

Diff for: examples/shapes_and_collections/patch_collection.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
============================
55
66
This example demonstrates how to use `.collections.PatchCollection`.
7+
8+
See also :doc:`/gallery/shapes_and_collections/artist_reference`, which instead
9+
adds each artist separately to its own axes.
710
"""
811

912
import numpy as np

0 commit comments

Comments
 (0)