|
| 1 | +""" |
| 2 | +Reference for matplotlib artists |
| 3 | +
|
| 4 | +This example displays several of matplotlib's graphics primitives (artists) |
| 5 | +drawn using matplotlib API. A full list of artists and the documentation is |
| 6 | +available at http://matplotlib.org/api/artist_api.html. |
| 7 | +
|
| 8 | +Copyright (c) 2010, Bartosz Telenczuk |
| 9 | +BSD License |
| 10 | +""" |
| 11 | +import matplotlib.pyplot as plt; plt.rcdefaults() |
| 12 | +from mpltools import style; style.use('gallery') |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import matplotlib.pyplot as plt |
| 16 | +import matplotlib.path as mpath |
| 17 | +import matplotlib.lines as mlines |
| 18 | +import matplotlib.patches as mpatches |
| 19 | +from matplotlib.collections import PatchCollection |
| 20 | + |
| 21 | + |
| 22 | +def label(xy, text): |
| 23 | + y = xy[1] - 0.15 # shift y-value for label so that it's below the artist |
| 24 | + plt.text(xy[0], y, text, ha="center", family='sans-serif', size=14) |
| 25 | + |
| 26 | + |
| 27 | +fig, ax = plt.subplots() |
| 28 | +# create 3x3 grid to plot the artists |
| 29 | +grid = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1).T |
| 30 | + |
| 31 | +patches = [] |
| 32 | + |
| 33 | +# add a circle |
| 34 | +circle = mpatches.Circle(grid[0], 0.1,ec="none") |
| 35 | +patches.append(circle) |
| 36 | +label(grid[0], "Circle") |
| 37 | + |
| 38 | +# add a rectangle |
| 39 | +rect = mpatches.Rectangle(grid[1] - [0.025, 0.05], 0.05, 0.1, ec="none") |
| 40 | +patches.append(rect) |
| 41 | +label(grid[1], "Rectangle") |
| 42 | + |
| 43 | +# add a wedge |
| 44 | +wedge = mpatches.Wedge(grid[2], 0.1, 30, 270, ec="none") |
| 45 | +patches.append(wedge) |
| 46 | +label(grid[2], "Wedge") |
| 47 | + |
| 48 | +# add a Polygon |
| 49 | +polygon = mpatches.RegularPolygon(grid[3], 5, 0.1) |
| 50 | +patches.append(polygon) |
| 51 | +label(grid[3], "Polygon") |
| 52 | + |
| 53 | +#add an ellipse |
| 54 | +ellipse = mpatches.Ellipse(grid[4], 0.2, 0.1) |
| 55 | +patches.append(ellipse) |
| 56 | +label(grid[4], "Ellipse") |
| 57 | + |
| 58 | +#add an arrow |
| 59 | +arrow = mpatches.Arrow(grid[5, 0]-0.05, grid[5, 1]-0.05, 0.1, 0.1, width=0.1) |
| 60 | +patches.append(arrow) |
| 61 | +label(grid[5], "Arrow") |
| 62 | + |
| 63 | +# add a path patch |
| 64 | +Path = mpath.Path |
| 65 | +path_data = [ |
| 66 | + (Path.MOVETO, [ 0.018, -0.11 ]), |
| 67 | + (Path.CURVE4, [-0.031, -0.051]), |
| 68 | + (Path.CURVE4, [-0.115, 0.073]), |
| 69 | + (Path.CURVE4, [-0.03 , 0.073]), |
| 70 | + (Path.LINETO, [-0.011, 0.039]), |
| 71 | + (Path.CURVE4, [ 0.043, 0.121]), |
| 72 | + (Path.CURVE4, [ 0.075, -0.005]), |
| 73 | + (Path.CURVE4, [ 0.035, -0.027]), |
| 74 | + (Path.CLOSEPOLY, [0.018, -0.11]) |
| 75 | + ] |
| 76 | +codes, verts = zip(*path_data) |
| 77 | +path = mpath.Path(verts + grid[6], codes) |
| 78 | +patch = mpatches.PathPatch(path) |
| 79 | +patches.append(patch) |
| 80 | +label(grid[6], "PathPatch") |
| 81 | + |
| 82 | +# add a fancy box |
| 83 | +fancybox = mpatches.FancyBboxPatch( |
| 84 | + grid[7] - [0.025, 0.05], 0.05, 0.1, |
| 85 | + boxstyle=mpatches.BoxStyle("Round", pad=0.02)) |
| 86 | +patches.append(fancybox) |
| 87 | +label(grid[7], "FancyBoxPatch") |
| 88 | + |
| 89 | +# add a line |
| 90 | +x,y = np.array([[-0.06, 0.0, 0.1], [0.05, -0.05, 0.05]]) |
| 91 | +line = mlines.Line2D(x + grid[8, 0], y + grid[8, 1], lw=5., alpha=0.3) |
| 92 | +label(grid[8], "Line2D") |
| 93 | + |
| 94 | +colors = np.linspace(0, 1, len(patches)) |
| 95 | +collection = PatchCollection(patches, cmap=plt.cm.hsv, alpha=0.3) |
| 96 | +collection.set_array(np.array(colors)) |
| 97 | +ax.add_collection(collection) |
| 98 | +ax.add_line(line) |
| 99 | + |
| 100 | +plt.subplots_adjust(left=0, right=1, bottom=0, top=1) |
| 101 | +plt.axis('equal') |
| 102 | +plt.axis('off') |
| 103 | + |
| 104 | +plt.show() |
0 commit comments