-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlollipop.py
367 lines (334 loc) · 11.6 KB
/
lollipop.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, zoomed_inset_axes
from matplotlib.ticker import FormatStrFormatter
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
import numpy as np
def apply_alpha(arr_img, alpha=1.0):
"""Helper method to apply an alpha to a numpy array image"""
new_arr_img = np.zeros(
shape=(arr_img.shape[0], arr_img.shape[1], 4), dtype=arr_img.dtype
)
for i, m in enumerate(arr_img):
for j, n in enumerate(m):
new_arr_img[i][j] = np.append(arr_img[i][j], alpha)
return new_arr_img
def plot_lollipop(
ordered_df,
figsize=(10, 10),
log=True,
hlines=False,
zoom_factor=3,
num_in_zoom=3,
indices=None,
labels=None,
bold_labels=None,
):
alpha = 1
my_range = range(0, len(ordered_df.index))
neuro_max_lim = ordered_df["count_neuro"].max()
cn_max_lim = ordered_df["count_cn"].max()
if not log:
neuro_max_lim += 100
cn_max_lim += 10
factor = neuro_max_lim / cn_max_lim
# Change color if in Africa
my_size = np.where(ordered_df["region"] == "Africa", 40, 40)
my_color = np.where(ordered_df["region"] == "Africa", "orange", "skyblue")
indx_africa = np.where(my_color == "orange")[0][0]
indx_row = np.where(my_color == "skyblue")[0][0]
africa = ordered_df[ordered_df["region"] == "Africa"]["count_neuro"].head(1)
row = ordered_df[ordered_df["region"] != "Africa"]["count_neuro"].head(1)
fig, ax = plt.subplots(1, 1, figsize=figsize, sharey=True)
# create zooms
if indices is None:
indices = [0, "South Africa", -1]
locs = ["upper center", "center right", "lower right"]
axins = []
for i in range(len(indices)):
if i < len(locs):
loc = locs[i]
else:
loc = "best"
axin: plt.Axes = zoomed_inset_axes(ax, zoom_factor, loc=loc)
axin.set_facecolor("w")
axins.append(axin)
if log:
for i, ax_i in enumerate([ax] + axins):
ax_i.set_xscale("log")
# do some formatting of the axis before possible duplication with twiny()
ax.spines["right"].set_color("none")
ax.spines["top"].set_color("none")
ax.grid(axis="x", which="major")
cols = {"count_cn": "Computational Neuroscience", "count_neuro": "Neuroscience"}
# plot specific points for legend
ax.scatter(
[],
[],
color="skyblue",
s=my_size[0] * 4,
alpha=alpha,
marker="o",
label="Computational Neuroscience",
)
ax.scatter(
[],
[],
color="skyblue",
s=my_size[0] * 4,
alpha=alpha,
marker="x",
label="Neuroscience",
)
ax.scatter(
[],
[],
color="orange",
s=my_size[0] * 4,
alpha=alpha,
marker="o",
label="Africa",
)
ax.scatter(
[],
[],
color="skyblue",
s=my_size[0] * 4,
alpha=alpha,
marker="o",
label="Rest of World",
)
# because of the zooms, points are plotted multiple times (otherwise 'zoom' views would be empty)
for i, ax_i in enumerate([ax] + axins):
# plot computational neuroscience points
ax_i.scatter(
ordered_df["count_cn"],
my_range,
color=my_color,
marker="o",
s=my_size * (4 if i > 0 else 1),
alpha=alpha,
label=None,
)
if not log:
# create a second axis for the other count
# set formatting options before 'twiny'
ax.set_xlim([0, cn_max_lim])
ax.legend(loc="left")
ax.grid(axis="x")
ax.set_xlabel(f"Number of Computational Neuroscience Publications")
ax.autoscale(True)
ax = ax.twiny()
# use original frame
ax.set_frame_on(False)
for i, ax_i in enumerate([ax] + axins):
# plot neuroscience points
ax_i.scatter(
ordered_df["count_neuro"],
my_range,
color=my_color,
marker="x",
s=my_size * (4 if i > 0 else 1),
alpha=alpha,
label=None,
)
# ax.set_xlim([0,neuro_max_lim])
if hlines:
# draw connections between "computational neuroscience" and "neuroscience" publications counts
if log:
xmin = ordered_df["count_cn"]
else:
xmin = ordered_df["count_cn"] * factor
xmax = ordered_df["count_neuro"]
for ax_i in [ax] + axins:
ax_i.hlines(y=my_range, xmin=xmin, xmax=xmax, color="grey", alpha=0.3)
ax.get_xaxis().set_label_position("bottom")
ax.get_xaxis().set_ticks_position("bottom")
if log:
# change scale to 1,10,100,etc. instead of 10^0,10^1,10^2,etc.
ax.get_xaxis().set_major_formatter(FormatStrFormatter("%.0f"))
ax.set_xlabel("Number of Publications")
ax.spines["left"].set_color("none")
ax.get_yaxis().set_visible(False)
else:
# adjust 2nd xaxis a little
ax.spines["bottom"].set_position(("axes", -0.01 * figsize[1]))
# Add title and axes names
ax.set_xlabel("# Neuroscience Publications")
ax.set_yticks(my_range)
ax.set_yticklabels(ordered_df["name"] + " (" + ordered_df.index + ")")
ax.set_ylabel("Country Code")
ax.get_yaxis().set_visible(True)
leg = ax.legend(
bbox_to_anchor=(0.0, 1.0, 1.0, 0.05),
loc="upper center",
ncol=4,
mode="expand",
borderaxespad=0.0,
frameon=False,
fontsize="large",
)
for legend_text in leg.get_texts():
if legend_text.get_text() == "Rest of World":
plt.setp(legend_text, alpha=0.35)
ax.set_ylim([0, my_range[-1] + 1])
ax.set_xlim(0.4) # apply the x-limits
# zooms
x_min = "count_cn"
x_max = ["count_cn", "count_neuro"][0]
for i, axin in zip(indices, axins):
if i == 0:
top_x = ordered_df.iloc[-num_in_zoom:]
top_y = my_range[-num_in_zoom:]
elif i == -1:
top_x = ordered_df.iloc[:num_in_zoom]
top_y = my_range[:num_in_zoom]
else:
if type(i) is str:
i = np.where(ordered_df["name"] == i)[0][0]
adjust = int((num_in_zoom - 1) / 2)
top_x = ordered_df.iloc[i - adjust : i + 1 + adjust]
top_y = my_range[i - adjust : i + 1 + adjust]
x1, x2, y1, y2 = (
np.min(top_x[x_min]),
np.max(top_x[x_max]),
np.min(top_y),
np.max(top_y),
)
# 3.7, 4.6 # specify the limits
axin.set_xlim(x1 - x1 / 10, x2 + x2 / 4) # apply the x-limits
axin.set_ylim(y1 - 1, y2 + 1) # apply the y-limits
axin.set_xticks([])
axin.get_xaxis().set_visible(False)
axin.set_yticks(top_y)
y_labels = []
for name, number in zip(top_x["name"], top_y):
y_labels.append(f"{name}")
axin.set_yticklabels(y_labels)
for label, x, y, indx in zip(
ordered_df[ordered_df.index.isin(top_x.index)][x_min],
top_x[x_min].values,
top_y,
top_x.index.values,
):
indx = indx.lower()
if indx == "usa":
indx = "us"
try:
arr_img = plt.imread(
f"famfamfam_flag_icons/png/{indx}.png", format="png"
)
except BaseException:
print(indx)
continue
axin.annotate(
label,
xy=(x, y),
xytext=(10, 0),
textcoords="offset points",
ha="left",
va="center",
bbox=dict(boxstyle="square,pad=0.1", fc="w", ec="w", alpha=1),
# arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0')
)
imagebox = OffsetImage(arr_img, zoom=1)
imagebox.image.axes = ax
ab = AnnotationBbox(
imagebox,
(x, y),
xybox=(x, y),
xycoords="data",
boxcoords="data",
pad=0.0,
)
axin.add_artist(ab)
if x_min != x_max:
for label, x, y in zip(
ordered_df[ordered_df.index.isin(top_x.index)][x_max],
top_x[x_max].values,
top_y,
):
axin.annotate(
label,
xy=(x, y),
xytext=(-10, 6),
textcoords="offset points",
ha="right",
va="center",
bbox=dict(
boxstyle="square,pad=0.1", fc="skyblue", ec="w", alpha=0.5
),
# arrowprops=dict(arrowstyle = '->', connectionstyle='arc3,rad=0')
)
# link zooms to main axis
mark_inset(ax, axin, loc1=2, loc2=3, fc="none", ec="0.5")
# add flags
if bold_labels is None:
bold_labels = []
if labels is not None:
y_max = my_range[-1] + 1
for label in labels:
if type(label) is str:
i = np.where(ordered_df["name"] == label)[0][0]
else:
i = label
label = ordered_df.iloc[i]["name"]
x = ordered_df.iloc[i]["count_cn"]
y = my_range[i]
xy = (x - x / 20, y)
xy_offset = (0.4, y) # in data coords
format_option = [f"{label} {x:>10g}", f"{x:g}"][1]
alpha = 1
if i not in bold_labels:
# 35% opacity
alpha = 0.35
# add flag
indx = ordered_df.iloc[i].name.lower()
if indx == "usa":
indx = "us"
try:
arr_img = plt.imread(
f"famfamfam_flag_icons/png/{indx}.png", format="png"
)
arr_img = apply_alpha(arr_img, alpha)
except BaseException:
print(f"flag not found: {indx} - {label}")
continue
else:
ax.annotate(
label,
xy=xy_offset,
xytext=(-15, 0),
textcoords="offset points",
ha="right",
va="center",
fontsize="x-large",
alpha=alpha,
bbox=dict(boxstyle="round,pad=0.2", fc="skyblue", alpha=0),
)
imagebox = OffsetImage(arr_img, zoom=1)
imagebox.image.axes = ax
xy = xy
ab = AnnotationBbox(
imagebox,
xy,
xybox=xy_offset,
xycoords="data",
pad=0.0,
bboxprops=dict(facecolor="none", edgecolor="none"),
arrowprops=dict(
arrowstyle="-", connectionstyle="arc3,rad=-0", alpha=0.09
),
)
ax.add_artist(ab)
color = "skyblue" if i not in bold_labels else "orange"
ax.annotate(
format_option,
xy=xy_offset,
xytext=(50, 0),
textcoords="offset points",
ha="right",
va="center",
fontsize="medium",
alpha=alpha,
bbox=dict(boxstyle="round,pad=0.1", ec=color, fc="w", alpha=1),
)