Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
matplotlib/tutorials/advanced/path_tutorial.py at v3.4.x · matplotlib/matplotlib · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
matplotlib
/
matplotlib
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
8.5k
Star
23.2k
Code
Issues
1.1k
Pull requests
423
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
v3.4.x
Breadcrumbs
matplotlib
/
tutorials
/
advanced
/
path_tutorial.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
230 lines (205 loc) · 7.56 KB
v3.4.x
Breadcrumbs
matplotlib
/
tutorials
/
advanced
/
path_tutorial.py
Copy path
Top
File metadata and controls
Code
Blame
230 lines (205 loc) · 7.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
=============
Path Tutorial
=============
Defining paths in your Matplotlib visualization.
The object underlying all of the :mod:`matplotlib.patches` objects is
the :class:`~matplotlib.path.Path`, which supports the standard set of
moveto, lineto, curveto commands to draw simple and compound outlines
consisting of line segments and splines. The ``Path`` is instantiated
with a (N, 2) array of (x, y) vertices, and a N-length array of path
codes. For example to draw the unit rectangle from (0, 0) to (1, 1), we
could use this code:
"""
import
matplotlib
.
pyplot
as
plt
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
]
codes
=
[
Path
.
MOVETO
,
Path
.
LINETO
,
Path
.
LINETO
,
Path
.
LINETO
,
Path
.
CLOSEPOLY
,
]
path
=
Path
(
verts
,
codes
)
fig
,
ax
=
plt
.
subplots
()
patch
=
patches
.
PathPatch
(
path
,
facecolor
=
'orange'
,
lw
=
2
)
ax
.
add_patch
(
patch
)
ax
.
set_xlim
(
-
2
,
2
)
ax
.
set_ylim
(
-
2
,
2
)
plt
.
show
()
###############################################################################
# The following path codes are recognized
#
# ============= ======================== ======================================
# Code Vertices Description
# ============= ======================== ======================================
# ``STOP`` 1 (ignored) A marker for the end of the entire
# path (currently not required and
# ignored).
# ``MOVETO`` 1 Pick up the pen and move to the given
# vertex.
# ``LINETO`` 1 Draw a line from the current position
# to the given vertex.
# ``CURVE3`` 2: Draw a quadratic Bézier curve from the
# 1 control point, current position, with the given
# 1 end point control point, to the given end point.
# ``CURVE4`` 3: Draw a cubic Bézier curve from the
# 2 control points, current position, with the given
# 1 end point control points, to the given end
# point.
# ``CLOSEPOLY`` 1 (the point is ignored) Draw a line segment to the start point
# of the current polyline.
# ============= ======================== ======================================
#
#
# .. path-curves:
#
#
# Bézier example
# ==============
#
# Some of the path components require multiple vertices to specify them:
# for example CURVE 3 is a `bézier
# <https://en.wikipedia.org/wiki/B%C3%A9zier_curve>`_ curve with one
# control point and one end point, and CURVE4 has three vertices for the
# two control points and the end point. The example below shows a
# CURVE4 Bézier spline -- the bézier curve will be contained in the
# convex hull of the start point, the two control points, and the end
# point
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
,
ax
=
plt
.
subplots
()
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
()
###############################################################################
# .. compound_paths:
#
# Compound paths
# ==============
#
# All of the simple patch primitives in matplotlib, Rectangle, Circle,
# Polygon, etc, are implemented with simple path. Plotting functions
# like :meth:`~matplotlib.axes.Axes.hist` and
# :meth:`~matplotlib.axes.Axes.bar`, which create a number of
# primitives, e.g., a bunch of Rectangles, can usually be implemented more
# efficiently using a compound path. The reason ``bar`` creates a list
# of rectangles and not a compound path is largely historical: the
# :class:`~matplotlib.path.Path` code is comparatively new and ``bar``
# predates it. While we could change it now, it would break old code,
# so here we will cover how to create compound paths, replacing the
# functionality in bar, in case you need to do so in your own code for
# efficiency reasons, e.g., you are creating an animated bar plot.
#
# We will make the histogram chart by creating a series of rectangles
# for each histogram bar: the rectangle width is the bin width and the
# rectangle height is the number of datapoints in that bin. First we'll
# create some random normally distributed data and compute the
# histogram. Because numpy returns the bin edges and not centers, the
# length of ``bins`` is 1 greater than the length of ``n`` in the
# example below::
#
# # histogram our data with numpy
# data = np.random.randn(1000)
# n, bins = np.histogram(data, 100)
#
# We'll now extract the corners of the rectangles. Each of the
# ``left``, ``bottom``, etc, arrays below is ``len(n)``, where ``n`` is
# the array of counts for each histogram bar::
#
# # get the corners of the rectangles for the histogram
# left = np.array(bins[:-1])
# right = np.array(bins[1:])
# bottom = np.zeros(len(left))
# top = bottom + n
#
# Now we have to construct our compound path, which will consist of a
# series of ``MOVETO``, ``LINETO`` and ``CLOSEPOLY`` for each rectangle.
# For each rectangle, we need 5 vertices: 1 for the ``MOVETO``, 3 for
# the ``LINETO``, and 1 for the ``CLOSEPOLY``. As indicated in the
# table above, the vertex for the closepoly is ignored but we still need
# it to keep the codes aligned with the vertices::
#
# nverts = nrects*(1+3+1)
# verts = np.zeros((nverts, 2))
# codes = np.ones(nverts, int) * path.Path.LINETO
# codes[0::5] = path.Path.MOVETO
# codes[4::5] = path.Path.CLOSEPOLY
# verts[0::5, 0] = left
# verts[0::5, 1] = bottom
# verts[1::5, 0] = left
# verts[1::5, 1] = top
# verts[2::5, 0] = right
# verts[2::5, 1] = top
# verts[3::5, 0] = right
# verts[3::5, 1] = bottom
#
# All that remains is to create the path, attach it to a
# :class:`~matplotlib.patches.PathPatch`, and add it to our axes::
#
# barpath = path.Path(verts, codes)
# patch = patches.PathPatch(barpath, facecolor='green',
# edgecolor='yellow', alpha=0.5)
# ax.add_patch(patch)
import
numpy
as
np
import
matplotlib
.
patches
as
patches
import
matplotlib
.
path
as
path
fig
,
ax
=
plt
.
subplots
()
# Fixing random state for reproducibility
np
.
random
.
seed
(
19680801
)
# histogram our data with numpy
data
=
np
.
random
.
randn
(
1000
)
n
,
bins
=
np
.
histogram
(
data
,
100
)
# get the corners of the rectangles for the histogram
left
=
np
.
array
(
bins
[:
-
1
])
right
=
np
.
array
(
bins
[
1
:])
bottom
=
np
.
zeros
(
len
(
left
))
top
=
bottom
+
n
nrects
=
len
(
left
)
nverts
=
nrects
*
(
1
+
3
+
1
)
verts
=
np
.
zeros
((
nverts
,
2
))
codes
=
np
.
ones
(
nverts
,
int
)
*
path
.
Path
.
LINETO
codes
[
0
::
5
]
=
path
.
Path
.
MOVETO
codes
[
4
::
5
]
=
path
.
Path
.
CLOSEPOLY
verts
[
0
::
5
,
0
]
=
left
verts
[
0
::
5
,
1
]
=
bottom
verts
[
1
::
5
,
0
]
=
left
verts
[
1
::
5
,
1
]
=
top
verts
[
2
::
5
,
0
]
=
right
verts
[
2
::
5
,
1
]
=
top
verts
[
3
::
5
,
0
]
=
right
verts
[
3
::
5
,
1
]
=
bottom
barpath
=
path
.
Path
(
verts
,
codes
)
patch
=
patches
.
PathPatch
(
barpath
,
facecolor
=
'green'
,
edgecolor
=
'yellow'
,
alpha
=
0.5
)
ax
.
add_patch
(
patch
)
ax
.
set_xlim
(
left
[
0
],
right
[
-
1
])
ax
.
set_ylim
(
bottom
.
min
(),
top
.
max
())
plt
.
show
()
You can’t perform that action at this time.