Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 67b7b04

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 67b7b04

File tree

1 file changed

+31
-86
lines changed

1 file changed

+31
-86
lines changed

examples/shapes_and_collections/artist_reference.py

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,22 @@
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>`.
98
109
Copyright (c) 2010, Bartosz Telenczuk
1110
BSD License
1211
"""
13-
import matplotlib.pyplot as plt
14-
import numpy as np
15-
import matplotlib.path as mpath
12+
13+
import matplotlib as mpl
1614
import matplotlib.lines as mlines
1715
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")
16+
import matplotlib.path as mpath
17+
import matplotlib.pyplot as plt
6218

63-
# add a path patch
19+
# Prepare the data for the PathPatch below.
6420
Path = mpath.Path
65-
path_data = [
21+
codes, verts = zip(*[
6622
(Path.MOVETO, [0.018, -0.11]),
6723
(Path.CURVE4, [-0.031, -0.051]),
6824
(Path.CURVE4, [-0.115, 0.073]),
@@ -71,35 +27,28 @@ def label(xy, text):
7127
(Path.CURVE4, [0.043, 0.121]),
7228
(Path.CURVE4, [0.075, -0.005]),
7329
(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-
30+
(Path.CLOSEPOLY, [0.018, -0.11])])
31+
32+
artists = [
33+
mpatches.Circle((0, 0), 0.1, ec="none"),
34+
mpatches.Rectangle((-0.025, -0.05), 0.05, 0.1, ec="none"),
35+
mpatches.Wedge((0, 0), 0.1, 30, 270, ec="none"),
36+
mpatches.RegularPolygon((0, 0), 5, radius=0.1),
37+
mpatches.Ellipse((0, 0), 0.2, 0.1),
38+
mpatches.Arrow(-0.05, -0.05, 0.1, 0.1, width=0.1),
39+
mpatches.PathPatch(mpath.Path(verts, codes), ec="none"),
40+
mpatches.FancyBboxPatch((-0.025, -0.05), 0.05, 0.1, ec="none",
41+
boxstyle=mpatches.BoxStyle("Round", pad=0.02)),
42+
mlines.Line2D([-0.06, 0.0, 0.1], [0.05, -0.05, 0.05], lw=5),
43+
]
44+
45+
axs = plt.figure(figsize=(6, 6), layout="constrained").subplots(3, 3)
46+
for i, (ax, artist) in enumerate(zip(axs.flat, artists)):
47+
artist.set(color=mpl.colormaps["hsv"](i / len(artists)))
48+
ax.add_artist(artist)
49+
ax.set(title=type(artist).__name__,
50+
aspect=1, xlim=(-.2, .2), ylim=(-.2, .2))
51+
ax.set_axis_off()
10352
plt.show()
10453

10554
#############################################################################
@@ -122,8 +71,4 @@ def label(xy, text):
12271
# - `matplotlib.patches.PathPatch`
12372
# - `matplotlib.patches.FancyBboxPatch`
12473
# - `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`
74+
# - `matplotlib.axes.Axes.add_artist`

0 commit comments

Comments
 (0)