FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/examples/api/collections_demo.py at master · staticfloat/matplotlib · GitHub
staticfloat
matplotlib
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
examples
/
api
/
collections_demo.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
133 lines (101 loc) · 4.1 KB
Breadcrumbs
matplotlib
/
examples
/
api
/
collections_demo.py
Copy path
File metadata and controls
133 lines (101 loc) · 4.1 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
#!/usr/bin/env python
'''Demonstration of LineCollection, PolyCollection, and
RegularPolyCollection with autoscaling.
For the first two subplots, we will use spirals. Their
size will be set in plot units, not data units. Their positions
will be set in data units by using the "offsets" and "transOffset"
kwargs of the LineCollection and PolyCollection.
The third subplot will make regular polygons, with the same
type of scaling and positioning as in the first two.
The last subplot illustrates the use of "offsets=(xo,yo)",
that is, a single tuple instead of a list of tuples, to generate
successively offset curves, with the offset given in data
units. This behavior is available only for the LineCollection.
'''
import
matplotlib
.
pyplot
as
plt
from
matplotlib
import
collections
,
transforms
from
matplotlib
.
colors
import
colorConverter
import
numpy
as
np
nverts
=
50
npts
=
100
# Make some spirals
r
=
np
.
array
(
range
(
nverts
))
theta
=
np
.
array
(
range
(
nverts
))
*
(
2
*
np
.
pi
)
/
(
nverts
-
1
)
xx
=
r
*
np
.
sin
(
theta
)
yy
=
r
*
np
.
cos
(
theta
)
spiral
=
list
(
zip
(
xx
,
yy
))
# Make some offsets
rs
=
np
.
random
.
RandomState
([
12345678
])
xo
=
rs
.
randn
(
npts
)
yo
=
rs
.
randn
(
npts
)
xyo
=
list
(
zip
(
xo
,
yo
))
# Make a list of colors cycling through the rgbcmyk series.
colors
=
[
colorConverter
.
to_rgba
(
c
)
for
c
in
(
'r'
,
'g'
,
'b'
,
'c'
,
'y'
,
'm'
,
'k'
)]
fig
=
plt
.
figure
()
a
=
fig
.
add_subplot
(
2
,
2
,
1
)
col
=
collections
.
LineCollection
([
spiral
],
offsets
=
xyo
,
transOffset
=
a
.
transData
)
trans
=
fig
.
dpi_scale_trans
+
transforms
.
Affine2D
().
scale
(
1.0
/
72.0
)
col
.
set_transform
(
trans
)
# the points to pixels transform
# Note: the first argument to the collection initializer
# must be a list of sequences of x,y tuples; we have only
# one sequence, but we still have to put it in a list.
a
.
add_collection
(
col
,
autolim
=
True
)
# autolim=True enables autoscaling. For collections with
# offsets like this, it is neither efficient nor accurate,
# but it is good enough to generate a plot that you can use
# as a starting point. If you know beforehand the range of
# x and y that you want to show, it is better to set them
# explicitly, leave out the autolim kwarg (or set it to False),
# and omit the 'a.autoscale_view()' call below.
# Make a transform for the line segments such that their size is
# given in points:
col
.
set_color
(
colors
)
a
.
autoscale_view
()
# See comment above, after a.add_collection.
a
.
set_title
(
'LineCollection using offsets'
)
# The same data as above, but fill the curves.
a
=
fig
.
add_subplot
(
2
,
2
,
2
)
col
=
collections
.
PolyCollection
([
spiral
],
offsets
=
xyo
,
transOffset
=
a
.
transData
)
trans
=
transforms
.
Affine2D
().
scale
(
fig
.
dpi
/
72.0
)
col
.
set_transform
(
trans
)
# the points to pixels transform
a
.
add_collection
(
col
,
autolim
=
True
)
col
.
set_color
(
colors
)
a
.
autoscale_view
()
a
.
set_title
(
'PolyCollection using offsets'
)
# 7-sided regular polygons
a
=
fig
.
add_subplot
(
2
,
2
,
3
)
col
=
collections
.
RegularPolyCollection
(
7
,
sizes
=
np
.
fabs
(
xx
)
*
10.0
,
offsets
=
xyo
,
transOffset
=
a
.
transData
)
trans
=
transforms
.
Affine2D
().
scale
(
fig
.
dpi
/
72.0
)
col
.
set_transform
(
trans
)
# the points to pixels transform
a
.
add_collection
(
col
,
autolim
=
True
)
col
.
set_color
(
colors
)
a
.
autoscale_view
()
a
.
set_title
(
'RegularPolyCollection using offsets'
)
# Simulate a series of ocean current profiles, successively
# offset by 0.1 m/s so that they form what is sometimes called
# a "waterfall" plot or a "stagger" plot.
a
=
fig
.
add_subplot
(
2
,
2
,
4
)
nverts
=
60
ncurves
=
20
offs
=
(
0.1
,
0.0
)
yy
=
np
.
linspace
(
0
,
2
*
np
.
pi
,
nverts
)
ym
=
np
.
amax
(
yy
)
xx
=
(
0.2
+
(
ym
-
yy
)
/
ym
)
**
2
*
np
.
cos
(
yy
-
0.4
)
*
0.5
segs
=
[]
for
i
in
range
(
ncurves
):
xxx
=
xx
+
0.02
*
rs
.
randn
(
nverts
)
curve
=
list
(
zip
(
xxx
,
yy
*
100
))
segs
.
append
(
curve
)
col
=
collections
.
LineCollection
(
segs
,
offsets
=
offs
)
a
.
add_collection
(
col
,
autolim
=
True
)
col
.
set_color
(
colors
)
a
.
autoscale_view
()
a
.
set_title
(
'Successive data offsets'
)
a
.
set_xlabel
(
'Zonal velocity component (m/s)'
)
a
.
set_ylabel
(
'Depth (m)'
)
# Reverse the y-axis so depth increases downward
a
.
set_ylim
(
a
.
get_ylim
()[::
-
1
])
plt
.
show
()
Back
|
FazBrowse Home
|
New Git URL