Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
matplotlib/examples/misc/packed_bubbles.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
/
examples
/
misc
/
packed_bubbles.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
171 lines (143 loc) · 6.25 KB
v3.4.x
Breadcrumbs
matplotlib
/
examples
/
misc
/
packed_bubbles.py
Copy path
Top
File metadata and controls
Code
Blame
171 lines (143 loc) · 6.25 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
"""
===================
Packed-bubble chart
===================
Create a packed-bubble chart to represent scalar data.
The presented algorithm tries to move all bubbles as close to the center of
mass as possible while avoiding some collisions by moving around colliding
objects. In this example we plot the market share of different desktop
browsers.
(source: https://gs.statcounter.com/browser-market-share/desktop/worldwidev)
"""
import
numpy
as
np
import
matplotlib
.
pyplot
as
plt
browser_market_share
=
{
'browsers'
: [
'firefox'
,
'chrome'
,
'safari'
,
'edge'
,
'ie'
,
'opera'
],
'market_share'
: [
8.61
,
69.55
,
8.36
,
4.12
,
2.76
,
2.43
],
'color'
: [
'#5A69AF'
,
'#579E65'
,
'#F9C784'
,
'#FC944A'
,
'#F24C00'
,
'#00B825'
]
}
class
BubbleChart
:
def
__init__
(
self
,
area
,
bubble_spacing
=
0
):
"""
Setup for bubble collapse.
Parameters
----------
area : array-like
Area of the bubbles.
bubble_spacing : float, default: 0
Minimal spacing between bubbles after collapsing.
Notes
-----
If "area" is sorted, the results might look weird.
"""
area
=
np
.
asarray
(
area
)
r
=
np
.
sqrt
(
area
/
np
.
pi
)
self
.
bubble_spacing
=
bubble_spacing
self
.
bubbles
=
np
.
ones
((
len
(
area
),
4
))
self
.
bubbles
[:,
2
]
=
r
self
.
bubbles
[:,
3
]
=
area
self
.
maxstep
=
2
*
self
.
bubbles
[:,
2
].
max
()
+
self
.
bubble_spacing
self
.
step_dist
=
self
.
maxstep
/
2
# calculate initial grid layout for bubbles
length
=
np
.
ceil
(
np
.
sqrt
(
len
(
self
.
bubbles
)))
grid
=
np
.
arange
(
length
)
*
self
.
maxstep
gx
,
gy
=
np
.
meshgrid
(
grid
,
grid
)
self
.
bubbles
[:,
0
]
=
gx
.
flatten
()[:
len
(
self
.
bubbles
)]
self
.
bubbles
[:,
1
]
=
gy
.
flatten
()[:
len
(
self
.
bubbles
)]
self
.
com
=
self
.
center_of_mass
()
def
center_of_mass
(
self
):
return
np
.
average
(
self
.
bubbles
[:, :
2
],
axis
=
0
,
weights
=
self
.
bubbles
[:,
3
]
)
def
center_distance
(
self
,
bubble
,
bubbles
):
return
np
.
hypot
(
bubble
[
0
]
-
bubbles
[:,
0
],
bubble
[
1
]
-
bubbles
[:,
1
])
def
outline_distance
(
self
,
bubble
,
bubbles
):
center_distance
=
self
.
center_distance
(
bubble
,
bubbles
)
return
center_distance
-
bubble
[
2
]
-
\
bubbles
[:,
2
]
-
self
.
bubble_spacing
def
check_collisions
(
self
,
bubble
,
bubbles
):
distance
=
self
.
outline_distance
(
bubble
,
bubbles
)
return
len
(
distance
[
distance
<
0
])
def
collides_with
(
self
,
bubble
,
bubbles
):
distance
=
self
.
outline_distance
(
bubble
,
bubbles
)
idx_min
=
np
.
argmin
(
distance
)
return
idx_min
if
type
(
idx_min
)
==
np
.
ndarray
else
[
idx_min
]
def
collapse
(
self
,
n_iterations
=
50
):
"""
Move bubbles to the center of mass.
Parameters
----------
n_iterations : int, default: 50
Number of moves to perform.
"""
for
_i
in
range
(
n_iterations
):
moves
=
0
for
i
in
range
(
len
(
self
.
bubbles
)):
rest_bub
=
np
.
delete
(
self
.
bubbles
,
i
,
0
)
# try to move directly towards the center of mass
# direction vector from bubble to the center of mass
dir_vec
=
self
.
com
-
self
.
bubbles
[
i
, :
2
]
# shorten direction vector to have length of 1
dir_vec
=
dir_vec
/
np
.
sqrt
(
dir_vec
.
dot
(
dir_vec
))
# calculate new bubble position
new_point
=
self
.
bubbles
[
i
, :
2
]
+
dir_vec
*
self
.
step_dist
new_bubble
=
np
.
append
(
new_point
,
self
.
bubbles
[
i
,
2
:
4
])
# check whether new bubble collides with other bubbles
if
not
self
.
check_collisions
(
new_bubble
,
rest_bub
):
self
.
bubbles
[
i
, :]
=
new_bubble
self
.
com
=
self
.
center_of_mass
()
moves
+=
1
else
:
# try to move around a bubble that you collide with
# find colliding bubble
for
colliding
in
self
.
collides_with
(
new_bubble
,
rest_bub
):
# calculate direction vector
dir_vec
=
rest_bub
[
colliding
, :
2
]
-
self
.
bubbles
[
i
, :
2
]
dir_vec
=
dir_vec
/
np
.
sqrt
(
dir_vec
.
dot
(
dir_vec
))
# calculate orthogonal vector
orth
=
np
.
array
([
dir_vec
[
1
],
-
dir_vec
[
0
]])
# test which direction to go
new_point1
=
(
self
.
bubbles
[
i
, :
2
]
+
orth
*
self
.
step_dist
)
new_point2
=
(
self
.
bubbles
[
i
, :
2
]
-
orth
*
self
.
step_dist
)
dist1
=
self
.
center_distance
(
self
.
com
,
np
.
array
([
new_point1
]))
dist2
=
self
.
center_distance
(
self
.
com
,
np
.
array
([
new_point2
]))
new_point
=
new_point1
if
dist1
<
dist2
else
new_point2
new_bubble
=
np
.
append
(
new_point
,
self
.
bubbles
[
i
,
2
:
4
])
if
not
self
.
check_collisions
(
new_bubble
,
rest_bub
):
self
.
bubbles
[
i
, :]
=
new_bubble
self
.
com
=
self
.
center_of_mass
()
if
moves
/
len
(
self
.
bubbles
)
<
0.1
:
self
.
step_dist
=
self
.
step_dist
/
2
def
plot
(
self
,
ax
,
labels
,
colors
):
"""
Draw the bubble plot.
Parameters
----------
ax : matplotlib.axes.Axes
labels : list
Labels of the bubbles.
colors : list
Colors of the bubbles.
"""
for
i
in
range
(
len
(
self
.
bubbles
)):
circ
=
plt
.
Circle
(
self
.
bubbles
[
i
, :
2
],
self
.
bubbles
[
i
,
2
],
color
=
colors
[
i
])
ax
.
add_patch
(
circ
)
ax
.
text
(
*
self
.
bubbles
[
i
, :
2
],
labels
[
i
],
horizontalalignment
=
'center'
,
verticalalignment
=
'center'
)
bubble_chart
=
BubbleChart
(
area
=
browser_market_share
[
'market_share'
],
bubble_spacing
=
0.1
)
bubble_chart
.
collapse
()
fig
,
ax
=
plt
.
subplots
(
subplot_kw
=
dict
(
aspect
=
"equal"
))
bubble_chart
.
plot
(
ax
,
browser_market_share
[
'browsers'
],
browser_market_share
[
'color'
])
ax
.
axis
(
"off"
)
ax
.
relim
()
ax
.
autoscale_view
()
ax
.
set_title
(
'Browser market share'
)
plt
.
show
()
You can’t perform that action at this time.