|
| 1 | +""" |
| 2 | +Show examples of matplotlib artists |
| 3 | +http://matplotlib.sourceforge.net/api/artist_api.html |
| 4 | +
|
| 5 | +Several examples of standard matplotlib graphics primitives (artists) |
| 6 | +are drawn using matplotlib API. Full list of artists and the |
| 7 | +documentation is available at |
| 8 | +http://matplotlib.sourceforge.net/api/artist_api.html |
| 9 | +
|
| 10 | +Copyright (c) 2010, Bartosz Telenczuk |
| 11 | +
|
| 12 | +License: This work is licensed under the BSD. A copy should be |
| 13 | +included with this source code, and is also available at |
| 14 | +http://www.opensource.org/licenses/bsd-license.php |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +import matplotlib.pyplot as plt |
| 20 | +import matplotlib |
| 21 | +from matplotlib.collections import PatchCollection |
| 22 | +import matplotlib.path as mpath |
| 23 | +import matplotlib.patches as mpatches |
| 24 | +import matplotlib.lines as mlines |
| 25 | + |
| 26 | +font = "sans-serif" |
| 27 | +fig = plt.figure(figsize=(5,5)) |
| 28 | +ax = plt.axes([0,0,1,1]) |
| 29 | + |
| 30 | +# create 3x3 grid to plot the artists |
| 31 | +pos = np.mgrid[0.2:0.8:3j, 0.2:0.8:3j].reshape(2, -1) |
| 32 | + |
| 33 | +patches = [] |
| 34 | + |
| 35 | +# add a circle |
| 36 | +art = mpatches.Circle(pos[:,0], 0.1,ec="none") |
| 37 | +patches.append(art) |
| 38 | +plt.text(pos[0,0], pos[1,0]-0.15, "Circle", ha="center", |
| 39 | + family=font, size=14) |
| 40 | + |
| 41 | +# add a rectangle |
| 42 | +art = mpatches.Rectangle(pos[:,1] - np.array([0.025, 0.05]), 0.05, 0.1, |
| 43 | + ec="none") |
| 44 | +patches.append(art) |
| 45 | +plt.text(pos[0,1], pos[1,1]-0.15, "Rectangle", ha="center", |
| 46 | + family=font, size=14) |
| 47 | + |
| 48 | +# add a wedge |
| 49 | +wedge = mpatches.Wedge(pos[:,2], 0.1, 30, 270, ec="none") |
| 50 | +patches.append(wedge) |
| 51 | +plt.text(pos[0,2], pos[1,2]-0.15, "Wedge", ha="center", |
| 52 | + family=font, size=14) |
| 53 | + |
| 54 | +# add a Polygon |
| 55 | +polygon = mpatches.RegularPolygon(pos[:,3], 5, 0.1) |
| 56 | +patches.append(polygon) |
| 57 | +plt.text(pos[0,3], pos[1,3]-0.15, "Polygon", ha="center", |
| 58 | + family=font, size=14) |
| 59 | + |
| 60 | +#add an ellipse |
| 61 | +ellipse = mpatches.Ellipse(pos[:,4], 0.2, 0.1) |
| 62 | +patches.append(ellipse) |
| 63 | +plt.text(pos[0,4], pos[1,4]-0.15, "Ellipse", ha="center", |
| 64 | + family=font, size=14) |
| 65 | + |
| 66 | +#add an arrow |
| 67 | +arrow = mpatches.Arrow(pos[0,5]-0.05, pos[1,5]-0.05, 0.1, 0.1, width=0.1) |
| 68 | +patches.append(arrow) |
| 69 | +plt.text(pos[0,5], pos[1,5]-0.15, "Arrow", ha="center", |
| 70 | + family=font, size=14) |
| 71 | + |
| 72 | +# add a path patch |
| 73 | +Path = mpath.Path |
| 74 | +verts = np.array([ |
| 75 | + (0.158, -0.257), |
| 76 | + (0.035, -0.11), |
| 77 | + (-0.175, 0.20), |
| 78 | + (0.0375, 0.20), |
| 79 | + (0.085, 0.115), |
| 80 | + (0.22, 0.32), |
| 81 | + (0.3, 0.005), |
| 82 | + (0.20, -0.05), |
| 83 | + (0.158, -0.257), |
| 84 | + ]) |
| 85 | +verts = verts-verts.mean(0) |
| 86 | +codes = [Path.MOVETO, |
| 87 | + Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.LINETO, |
| 88 | + Path.CURVE4, Path.CURVE4, Path.CURVE4, Path.CLOSEPOLY] |
| 89 | + |
| 90 | +path = mpath.Path(verts/2.5+pos[:,6], codes) |
| 91 | +patch = mpatches.PathPatch(path) |
| 92 | +patches.append(patch) |
| 93 | +plt.text(pos[0,6], pos[1,6]-0.15, "PathPatch", ha="center", |
| 94 | + family=font, size=14) |
| 95 | + |
| 96 | +# add a fancy box |
| 97 | +fancybox = mpatches.FancyBboxPatch( |
| 98 | + pos[:,7]-np.array([0.025, 0.05]), 0.05, 0.1, |
| 99 | + boxstyle=mpatches.BoxStyle("Round", pad=0.02)) |
| 100 | +patches.append(fancybox) |
| 101 | +plt.text(pos[0,7], pos[1,7]-0.15, "FancyBoxPatch", ha="center", |
| 102 | + family=font, size=14) |
| 103 | + |
| 104 | +# add a line |
| 105 | +x,y = np.array([[-0.06, 0.0, 0.1], [0.05,-0.05, 0.05]]) |
| 106 | +line = mlines.Line2D(x+pos[0,8], y+pos[1,8], lw=5., |
| 107 | + alpha=0.4) |
| 108 | +plt.text(pos[0,8], pos[1,8]-0.15, "Line2D", ha="center", |
| 109 | + family=font, size=14) |
| 110 | + |
| 111 | +colors = 100*np.random.rand(len(patches)) |
| 112 | +collection = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4) |
| 113 | +collection.set_array(np.array(colors)) |
| 114 | +ax.add_collection(collection) |
| 115 | +ax.add_line(line) |
| 116 | +ax.set_xticks([]) |
| 117 | +ax.set_yticks([]) |
| 118 | + |
| 119 | +plt.show() |
0 commit comments