0% found this document useful (0 votes)
87 views14 pages

Borrao 18 03 Matplotlib

This document contains code snippets demonstrating how to use matplotlib to create and modify path objects and add them to plots. It shows how to create paths from vertices and codes, add them as patches to axes, and modify properties like face color, line width, and axis limits. Various path shapes are created like circles, lines, rectangles, and polygons with curved and straight segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views14 pages

Borrao 18 03 Matplotlib

This document contains code snippets demonstrating how to use matplotlib to create and modify path objects and add them to plots. It shows how to create paths from vertices and codes, add them as patches to axes, and modify properties like face color, line width, and axis limits. Various path shapes are created like circles, lines, rectangles, and polygons with curved and straight segments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 14

Jupyter QtConsole 4.2.

Python 2.7.13 |Anaconda 4.3.1 (64-bit)| (default, Dec 19 2016, 13:29:36) [MSC v.1500 64 bit (AMD64)]

Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.

? -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help -> Python's own help system.

object? -> Details about 'object', use 'object??' for extra details.

import matplotlib.pyplot as plt

plt.axes()

Out[2]: <matplotlib.axes._subplots.AxesSubplot at 0x81f32b0>

circle = plt.Circle((0, 0), radius=0.75, fc='y')

plt.gca().add_patch(circle)

Out[4]: <matplotlib.patches.Circle at 0x83a8b38>

plt.axis('scaled')

Out[5]:

(-0.82499999999999996,

0.82499999999999996,
-0.82499999999999996,

0.82499999999999996)

plt.show()

line = plt.Line2D((2, 8), (6, 6), lw=2.5)

plt.gca().add_line(line)

Out[8]: <matplotlib.lines.Line2D at 0x83a8a58>

plt.show()

rectangle = plt.Rectangle((10, 10), 100, 100, fc='r')

plt.gca().add_line(line)

RuntimeErrorTraceback (most recent call last)

<ipython-input-11-11d1af3f8597> in <module>()

----> 1 plt.gca().add_line(line)

C:\Users\adm\Anaconda2\lib\site-packages\matplotlib\axes\_base.pyc in add_line(self, line)


1926 Returns the line.

1927 """

-> 1928 self._set_artist_props(line)

1929 if line.get_clip_path() is None:

1930 line.set_clip_path(self.patch)

C:\Users\adm\Anaconda2\lib\site-packages\matplotlib\axes\_base.pyc in _set_artist_props(self, a)

952 def _set_artist_props(self, a):

953 """set the boilerplate props for artists added to axes"""

--> 954 a.set_figure(self.figure)

955 if not a.is_transform_set():

956 a.set_transform(self.transData)

C:\Users\adm\Anaconda2\lib\site-packages\matplotlib\artist.pyc in set_figure(self, fig)

628 # to more than one Axes

629 if self.figure is not None:

--> 630 raise RuntimeError("Can not put single artist in "

631 "more than one figure")

632 self.figure = fig

RuntimeError: Can not put single artist in more than one figure

points = [[2, 4], [2, 8], [4, 6], [6, 8]]

polygon = plt.Polygon(points)
line = plt.Polygon(points, closed=None, fill=None, edgecolor='r')

line

Out[15]: <matplotlib.patches.Polygon at 0x888f748>

line.show()

AttributeErrorTraceback (most recent call last)

<ipython-input-16-ae7a3858647c> in <module>()

----> 1 line.show()

AttributeError: 'Polygon' object has no attribute 'show'

line

Out[17]: <matplotlib.patches.Polygon at 0x888f748>

from matplotlib.path import Path

import matplotlib.patches as patches

verts = [

(0., 0.), # left, bottom

(0., 1.), # left, top

(1., 1.), # right, top


(1., 0.), # right, bottom

(0., 0.), # ignored

path = Path(verts, codes)

NameErrorTraceback (most recent call last)

<ipython-input-21-fe970f43eeb2> in <module>()

----> 1 path = Path(verts, codes)

NameError: name 'codes' is not defined

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)
patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

Out[27]: <matplotlib.patches.PathPatch at 0x885c978>

ax.set_xlim(-2,2)

Out[28]: (-2, 2)

ax.set_ylim(-2,2)

Out[29]: (-2, 2)

plt.show()

verts = [

(0., 0.), # P0

(0.2, 1.), # P1

(1., 0.8), # P2

(0.8, 0.), # P3

codes = [Path.MOVETO,
Path.CURVE4,

Path.CURVE4,

Path.CURVE4,

path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='none', lw=2)

ax.add_patch(patch)

xs, ys = zip(*verts)

ax.plot(xs, ys, 'x--', lw=2, color='black', ms=10)

ax.text(-0.05, -0.05, 'P0')

ax.text(0.15, 1.05, 'P1')

ax.text(1.05, 0.85, 'P2')

ax.text(0.85, -0.05, 'P3')

ax.set_xlim(-0.1, 1.1)

ax.set_ylim(-0.1, 1.1)

plt.show()
verts = [

(1., 1.), # left, bottom

(0.5, 1.), # left, top

(0.5, 2.), # right, top

(1., 3.), # right, bottom

(0., 0.), # ignored

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

ax.set_xlim(-5,5)

ax.set_ylim(-5,5)
plt.show()

verts = [

(1., 1.), # left, bottom

(1, 3.), # left, top

(3, 1.), # right, top

(1., 3.), # right, bottom

(0., 0.), # ignored

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)


ax.add_patch(patch)

ax.set_xlim(-5,5)

ax.set_ylim(-5,5)

plt.show()

verts = [

(1., 1.), # left, bottom

(1, 3.), # left, top

(3, 3.), # right, top

(3., 1.), # right, bottom

(0., 0.), # ignored

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

path = Path(verts, codes)


fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

ax.set_xlim(-5,5)

ax.set_ylim(-5,5)

plt.show()

verts = [

(2.82842712,1.4142135), # left, bottom

(4.24264069, 0.), # left, top

(1.41421356, 0.), # right, top

(2.82842712, -1.41421356), # right, bottom

(0., 0.), # ignored

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

]
path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

ax.set_xlim(-5,5)

ax.set_ylim(-5,5)

plt.show()

verts = [

(1,1), # left, bottom

(3,3), # left, top

(5,1), # right, top

(0., 0.), # ignored

codes = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,
]

path = Path(verts, codes)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

ax.set_xlim(-5,5)

ax.set_ylim(-5,5)

plt.show()

import matplotlib.pyplot as plt

from matplotlib.path import Path

import matplotlib.patches as patches

vertices = [

(1,1), # left, bottom

(3,3), # left, top

(5,1), # right, top

(0., 0.), # ignored

]
comandos = [Path.MOVETO,

Path.LINETO,

Path.LINETO,

Path.CLOSEPOLY,

path = Path(vertices, comandos)

fig = plt.figure()

ax = fig.add_subplot(111)

patch = patches.PathPatch(path, facecolor='orange', lw=2)

ax.add_patch(patch)

ax.set_xlim(-8,8)

ax.set_ylim(-8,8)

plt.show()

link

https://matplotlib.org/users/path_tutorial.html

You might also like