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

Skip to content

Simplify "artist reference" example. #25018

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 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
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
120 changes: 34 additions & 86 deletions examples/shapes_and_collections/artist_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,25 @@
Reference for Matplotlib artists
================================

This example displays several of Matplotlib's graphics primitives (artists)
drawn using matplotlib API. A full list of artists and the documentation is
available at :ref:`the artist API <artist-api>`.
This example displays several of Matplotlib's graphics primitives (artists).
A full list of artists is documented at :ref:`the artist API <artist-api>`.

See also :doc:`/gallery/shapes_and_collections/patch_collection`, which groups
all artists into a single `.PatchCollection` instead.

Copyright (c) 2010, Bartosz Telenczuk
BSD License
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath

import matplotlib as mpl
import matplotlib.lines as mlines
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection


def label(xy, text):
y = xy[1] - 0.15 # shift y-value for label so that it's below the artist
plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14)


fig, ax = plt.subplots()
# create 3x3 grid to plot the artists
grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T

patches = []

# add a circle
circle = mpatches.Circle(grid[0], 0.1, ec="none")
patches.append(circle)
label(grid[0], "Circle")

# add a rectangle
rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none")
patches.append(rect)
label(grid[1], "Rectangle")

# add a wedge
wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none")
patches.append(wedge)
label(grid[2], "Wedge")

# add a Polygon
polygon = mpatches.RegularPolygon(grid[3], 5, radius=0.1)
patches.append(polygon)
label(grid[3], "Polygon")

# add an ellipse
ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1)
patches.append(ellipse)
label(grid[4], "Ellipse")

# add an arrow
arrow = mpatches.Arrow(grid[5, 0] - 0.05, grid[5, 1] - 0.05, 0.1, 0.1,
width=0.1)
patches.append(arrow)
label(grid[5], "Arrow")
import matplotlib.path as mpath
import matplotlib.pyplot as plt

# add a path patch
# Prepare the data for the PathPatch below.
Path = mpath.Path
path_data = [
codes, verts = zip(*[
(Path.MOVETO, [0.018, -0.11]),
(Path.CURVE4, [-0.031, -0.051]),
(Path.CURVE4, [-0.115, 0.073]),
Expand All @@ -71,35 +30,28 @@ def label(xy, text):
(Path.CURVE4, [0.043, 0.121]),
(Path.CURVE4, [0.075, -0.005]),
(Path.CURVE4, [0.035, -0.027]),
(Path.CLOSEPOLY, [0.018, -0.11])]
codes, verts = zip(*path_data)
path = mpath.Path(verts + grid[6], codes)
patch = mpatches.PathPatch(path)
patches.append(patch)
label(grid[6], "PathPatch")

# add a fancy box
fancybox = mpatches.FancyBboxPatch(
grid[7] - [0.025, 0.05], 0.05, 0.1,
boxstyle=mpatches.BoxStyle("Round", pad=0.02))
patches.append(fancybox)
label(grid[7], "FancyBboxPatch")

# add a line
x, y = ([-0.06, 0.0, 0.1], [0.05, -0.05, 0.05])
line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3)
label(grid[8], "Line2D")

colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3)
collection.set_array(colors)
ax.add_collection(collection)
ax.add_line(line)

plt.axis('equal')
plt.axis('off')
plt.tight_layout()

(Path.CLOSEPOLY, [0.018, -0.11])])

artists = [
mpatches.Circle((0, 0), 0.1, ec="none"),
mpatches.Rectangle((-0.025, -0.05), 0.05, 0.1, ec="none"),
mpatches.Wedge((0, 0), 0.1, 30, 270, ec="none"),
mpatches.RegularPolygon((0, 0), 5, radius=0.1),
mpatches.Ellipse((0, 0), 0.2, 0.1),
mpatches.Arrow(-0.05, -0.05, 0.1, 0.1, width=0.1),
mpatches.PathPatch(mpath.Path(verts, codes), ec="none"),
mpatches.FancyBboxPatch((-0.025, -0.05), 0.05, 0.1, ec="none",
boxstyle=mpatches.BoxStyle("Round", pad=0.02)),
mlines.Line2D([-0.06, 0.0, 0.1], [0.05, -0.05, 0.05], lw=5),
]

axs = plt.figure(figsize=(6, 6), layout="constrained").subplots(3, 3)
for i, (ax, artist) in enumerate(zip(axs.flat, artists)):
artist.set(color=mpl.colormaps["hsv"](i / len(artists)))
ax.add_artist(artist)
ax.set(title=type(artist).__name__,
aspect=1, xlim=(-.2, .2), ylim=(-.2, .2))
ax.set_axis_off()
plt.show()

#############################################################################
Expand All @@ -122,8 +74,4 @@ def label(xy, text):
# - `matplotlib.patches.PathPatch`
# - `matplotlib.patches.FancyBboxPatch`
# - `matplotlib.patches.RegularPolygon`
# - `matplotlib.collections`
# - `matplotlib.collections.PatchCollection`
# - `matplotlib.cm.ScalarMappable.set_array`
# - `matplotlib.axes.Axes.add_collection`
# - `matplotlib.axes.Axes.add_line`
# - `matplotlib.axes.Axes.add_artist`
3 changes: 3 additions & 0 deletions examples/shapes_and_collections/patch_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
============================

This example demonstrates how to use `.collections.PatchCollection`.

See also :doc:`/gallery/shapes_and_collections/artist_reference`, which instead
adds each artist separately to its own axes.
"""

import numpy as np
Expand Down