1
1
"""
2
2
Create a packed bubble / non overlapping bubble chart to represent scalar data.
3
- In this example we plot the market share of different desktop browsers.
3
+ The presented algorithm tries to move all bubbles as close to the center of
4
+ mass as possible while avoiding some collisions by moving aroud colliding
5
+ objects. In this example we plot the market share of different desktop
6
+ browsers.
4
7
"""
5
8
6
9
import numpy as np
15
18
16
19
class BubbleChart :
17
20
def __init__ (self , r = None , a = None , bubble_distance = 0 ):
18
- """setup for bubble collapse
21
+ """
22
+ setup for bubble collapse
19
23
20
24
Args:
21
25
r (list, optional): radius of the bubbles. Defaults to None.
22
26
a (list, optional): area of the bubbles. Defaults to None.
23
27
Note: If r or a is sorted, the results might look weird
24
- bubble_distance (int, optional): minimal distance the bubbles should have after collapsing. Defaults to 0.
28
+ bubble_distance (int, optional): minimal distance the bubbles
29
+ should have after collapsing. Defaults to 0.
25
30
"""
26
31
if r is None :
27
32
r = np .sqrt (a / np .pi )
@@ -49,15 +54,18 @@ def __len__(self):
49
54
return self .n
50
55
51
56
def center_of_mass (self ):
52
- return np .average (self .bubbles [:, :2 ], axis = 0 , weights = self .bubbles [:, 3 ])
57
+ return np .average (
58
+ self .bubbles [:, :2 ], axis = 0 , weights = self .bubbles [:, 3 ]
59
+ )
53
60
54
61
def center_distance (self , bubble , bubbles ):
55
- return np .sqrt (np .power (bubble [0 ] - bubbles [:, 0 ], 2 )
56
- + np .power (bubble [1 ] - bubbles [:, 1 ], 2 ))
62
+ return np .sqrt (np .power (bubble [0 ] - bubbles [:, 0 ], 2 ) +
63
+ np .power (bubble [1 ] - bubbles [:, 1 ], 2 ))
57
64
58
65
def outline_distance (self , bubble , bubbles ):
59
66
center_distance = self .center_distance (bubble , bubbles )
60
- return center_distance - bubble [2 ] - bubbles [:, 2 ] - self .bubble_distance
67
+ return center_distance - bubble [2 ] - \
68
+ bubbles [:, 2 ] - self .bubble_distance
61
69
62
70
def check_collisions (self , bubble , bubbles ):
63
71
distance = self .outline_distance (bubble , bubbles )
@@ -112,10 +120,10 @@ def collapse(self):
112
120
113
121
if moves / len (self ) < 0.1 :
114
122
self .step_dist = self .step_dist / 2
115
- print (self .step_dist )
116
123
117
124
def plot (self , ax , labels , colors ):
118
- """draw the bubble plot
125
+ """
126
+ draw the bubble plot
119
127
120
128
Args:
121
129
ax (matplotlib.axes._subplots.AxesSubplot)
@@ -134,7 +142,8 @@ def plot(self, ax, labels, colors):
134
142
bubble_plot = BubbleChart (a = np .array (
135
143
browser_market_share ['market_share' ]), bubble_distance = 1 )
136
144
137
- # collapse plot 50 times. In some cases it might be useful to do this more or less often
145
+ # collapse plot 50 times. In some cases it might be useful
146
+ # to do this more or less often
138
147
for i in range (50 ):
139
148
bubble_plot .collapse ()
140
149
0 commit comments