-
Notifications
You must be signed in to change notification settings - Fork 21
/
makeplots.py
729 lines (632 loc) · 26 KB
/
makeplots.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import warnings
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
import pandas as pd
import seaborn as sns
from bdpy.fig import box_off
def makeplots(
df,
x=None, y=None,
x_list=None,
subplot=None, subplot_list=None,
figure=None, figure_list=None,
group=None, group_list=None,
bar_group_width=0.8,
plot_type='bar', horizontal=False, ebar=None,
plot_size_auto=True, plot_size=(4, 0.3),
max_col=None,
y_lim=None, y_ticks=None,
title=None, x_label=None, y_label=None,
fontsize=12, tick_fontsize=9, points=100,
style='default', colorset=None,
chance_level=None, chance_level_style={'color': 'k', 'linewidth': 1},
swarm_dot_color='gray',
swarm_dot_size=3, swarm_dot_alpha=0.7,
swarm_violin_color='blue',
box_color='blue', box_width=0.5, box_linewidth=1,
box_meanprops=dict(linestyle='-', linewidth=1.5, color='red'),
box_medianprops={}, flierprops={},
removenan=True,
verbose=False, colors=None, reverse_x=False
):
'''Make plots.
Parameters
----------
df : pandas.core.frame.DataFrame
x : str
y : str
x_list : list
subplot : str
subplot : list
figure : str
figure_list : list
plot_type : {'bar', 'violin', 'paired violin', 'swarm', 'box', 'swarm+box'}
horizontal: bool
plot_size : (width, height)
y_lim : (y_min, y_max)
y_ticks : array_like
title, x_label, y_label : str
fontsize : int
tick_fontsize : int
style : str
verbose : bool
Returns
-------
fig : matplotlib.figure.Figure or list of matplotlib.figure.Figure
'''
x_keys = sorted(df[x].unique())
subplot_keys = sorted(df[subplot].unique()) if subplot is not None else [None]
figure_keys = sorted(df[figure].unique()) if figure is not None else [None]
group_keys = sorted(df[group].unique()) if group is not None else [None]
x_list = x_keys if x_list is None else x_list
subplot_list = subplot_keys if subplot_list is None else subplot_list
figure_list = figure_keys if figure_list is None else figure_list
group_list = group_keys if group_list is None else group_list
if reverse_x:
x_list = x_list[::-1]
grouping = group is not None
if plot_type == 'paired violin':
if not grouping:
RuntimeError('plot type "paired violin" can be used only when grouping is enabled')
comparison_pairs = list(__split_list(group_list, 2))
if grouping:
warnings.warn('"grouping mode" is still experimental and will not work correctly yet!')
if verbose:
print('X: {}'.format(x_list))
if grouping:
print('Group by: {} ({})'.format(group_keys, group_list))
if subplot is not None:
print('Subplot: {}'.format(subplot_list))
if figure is not None:
print('Figures: {}'.format(figure_list))
col_num = np.ceil(np.sqrt(len(subplot_list)))
row_num = int(np.ceil(len(subplot_list) / col_num))
col_num = int(col_num)
if max_col is not None and col_num > max_col:
col_num = max_col
row_num = int(np.ceil(len(subplot_list) / col_num))
# Plot size
if plot_size_auto:
if horizontal:
plot_size = (plot_size[0], plot_size[1] * len(x_list))
else:
plot_size = (plot_size[0] * len(x_list), plot_size[1])
# Figure size
figsize = (col_num * plot_size[0], row_num * plot_size[1]) # (width, height)
if verbose:
print('Subplot in {} x {}'.format(row_num, col_num))
# Figure instances
if plot_type == 'paired violin':
figure_instances = [
{
'label': f,
'comparison pair': p
}
for f in figure_list
for p in comparison_pairs
]
else:
figure_instances = [
{
'label': f
}
for f in figure_list
]
figs = []
# Figure loop
for figure_instance in figure_instances:
fig_label = figure_instance['label']
if verbose:
if fig_label is None:
print('Creating a figure')
else:
print('Creating figure for {}'.format(fig_label))
plt.style.use(style)
fig = plt.figure(figsize=figsize)
# Subplot loop
for i, sp_label in enumerate(subplot_list):
if verbose:
print('Creating subplot for {}'.format(sp_label))
# Set subplot position
col = int(i / row_num)
row = i - col * row_num
sbpos = (row_num - row - 1) * col_num + col + 1
# Get data
if plot_type == 'paired violin':
group_list = figure_instance['comparison pair']
if plot_type == "swarm+box" or plot_type == "box":
df_t = __strict_data(
df, subplot, sp_label,
figure, fig_label, y, removenan
)
weird_keys = []
for key_candidate in [figure, subplot, group, x]:
if key_candidate is not None:
weird_keys.append(key_candidate)
df_t = __weird_form_to_long(df_t, y, identify_cols = weird_keys)
else:
data = __get_data(
df, subplot, sp_label,
x, x_list, figure, fig_label, y,
group, group_list, grouping, removenan
)
if not isinstance(sp_label, list):
if grouping:
data_mean = [[np.nanmean(d) for d in data_t] for data_t in data]
else:
data_mean = [np.nanmean(d) for d in data]
else:
data_mean = None
# Plot
ax = plt.subplot(row_num, col_num, sbpos)
if not style == 'ggplot':
if horizontal:
ax.grid(axis='x', color='k', linestyle='-', linewidth=0.5)
else:
ax.grid(axis='y', color='k', linestyle='-', linewidth=0.5)
xpos = range(len(x_list))
if plot_type == 'bar':
__plot_bar(
ax, xpos, data_mean,
horizontal=horizontal,
grouping=grouping, group_list=group_list,
bar_group_width=bar_group_width
)
elif plot_type == 'violin':
group_label_list = __plot_violin(
ax, xpos, data,
horizontal=horizontal,
grouping=grouping, group_list=group_list,
bar_group_width=bar_group_width, points=points
)
elif plot_type == 'paired violin':
group_label_list = __plot_violin_paired(
ax, xpos, data,
horizontal=horizontal,
grouping=grouping, group_list=group_list,
points=points, colors=colors
)
elif plot_type == 'swarm':
__plot_swarm(
ax, x_list, data,
horizontal=horizontal, grouping=grouping,
dot_color=swarm_dot_color,
dot_size=swarm_dot_size,
dot_alpha=swarm_dot_alpha,
violin_color=swarm_violin_color,
)
elif plot_type == 'box':
__plot_box(
ax, x, y, x_list, df_t,
horizontal=horizontal, reverse_x=reverse_x,
grouping=grouping, group=group, group_list=group_list,
box_color=box_color, box_width=box_width,
box_linewidth=box_linewidth,
box_meanprops=box_meanprops,
box_medianprops=box_medianprops
)
elif plot_type == 'swarm+box':
legend_handler = __plot_swarmbox(
ax, x, y, x_list, df_t,
horizontal=horizontal, reverse_x=reverse_x,
grouping=grouping, group=group, group_list=group_list,
dot_color=swarm_dot_color,
dot_size=swarm_dot_size,
dot_alpha=swarm_dot_alpha,
box_color=box_color, box_width=box_width,
box_linewidth=box_linewidth,
box_meanprops=box_meanprops,
box_medianprops=box_medianprops
)
else:
raise ValueError('Unknown plot_type: {}'.format(plot_type))
if not horizontal:
# Vertical plot
ax.set_xlim([-1, len(x_list)])
ax.set_xticks(range(len(x_list)))
if row == 0:
ax.set_xticklabels(x_list, rotation=-45, ha='left', fontsize=tick_fontsize)
else:
ax.set_xticklabels([])
if y_lim is None:
pass
else:
ax.set_ylim(y_lim)
if y_ticks is not None:
ax.set_yticks(y_ticks)
ax.tick_params(axis='y', labelsize=tick_fontsize, grid_color='gray', grid_linestyle='--', grid_linewidth=0.8)
if chance_level is not None:
plt.hlines(chance_level, xmin=plt.gca().get_xlim()[0], xmax=plt.gca().get_xlim()[1], **chance_level_style)
else:
# Horizontal plot
ax.set_ylim([-1, len(x_list)])
ax.set_yticks(range(len(x_list)))
if col == 0:
ax.set_yticklabels(x_list, fontsize=tick_fontsize)
else:
ax.set_yticklabels([])
if y_lim is None:
pass
else:
ax.set_xlim(y_lim)
if y_ticks is not None:
ax.set_xticks(y_ticks)
ax.tick_params(axis='x', labelsize=tick_fontsize, grid_color='gray', grid_linestyle='--', grid_linewidth=0.8)
if chance_level is not None:
plt.vlines(chance_level, ymin=plt.gca().get_ylim()[0], ymax=plt.gca().get_ylim()[1], **chance_level_style)
# Inset title
x_range = plt.gca().get_xlim()[1] - plt.gca().get_xlim()[0]
y_range = plt.gca().get_ylim()[1] - plt.gca().get_ylim()[0]
tpos = (
plt.gca().get_xlim()[0] + 0.03 * x_range,
plt.gca().get_ylim()[1] + 0.01 * y_range
)
ax.text(tpos[0], tpos[1], sp_label, horizontalalignment='left', verticalalignment='top', fontsize=fontsize, bbox=dict(facecolor='white', edgecolor='none'))
# Inset legend
if grouping:
if 'violin' in plot_type:
if i == len(subplot_list) - 1:
group_label_list = group_label_list[::-1]
ax.legend(*zip(*group_label_list), loc='upper left', bbox_to_anchor=(1, 1))
elif plot_type == 'swarm+box':
pass
else:
plt.legend()
box_off(ax)
plt.tight_layout()
# Draw legend, X/Y labels, and title ----------------------------
# Legend
if plot_type == 'swarm+box':
if legend_handler is not None:
if len(subplot_list) < col_num*row_num:
ax = plt.subplot(row_num, col_num, col_num)
else:
ax = fig.add_axes([1, 0.5, 1./col_num*0.6, 0.5])
ax.legend(legend_handler[0], legend_handler[1],
loc='upper left', bbox_to_anchor=(0, 1.0), fontsize=tick_fontsize)
ax.set_axis_off()
plt.tight_layout()
ax = fig.add_axes([0, 0, 1, 1])
ax.patch.set_alpha(0.0)
ax.set_axis_off()
# X Label
if x_label is not None:
txt = y_label if horizontal else x_label
ax.text(0.5, 0, txt, verticalalignment='center', horizontalalignment='center', fontsize=fontsize)
# Y label
if y_label is not None:
txt = x_label if horizontal else y_label
ax.text(0, 0.5, txt, verticalalignment='center', horizontalalignment='center', fontsize=fontsize, rotation=90)
# Figure title
if title is not None:
if fig_label is None:
ax.text(0.5, 0.99, title, horizontalalignment='center', fontsize=fontsize)
else:
ax.text(0.5, 0.99, '{}: {}'.format(title, fig_label), horizontalalignment='center', fontsize=fontsize)
figs.append(fig)
if figure is None:
return figs[0]
else:
return figs
def __plot_bar(
ax, xpos, data_mean,
horizontal=False,
grouping=False, group_list=[],
bar_group_width=0.8
):
if grouping:
ydata = np.array(data_mean)
n_grp = ydata.shape[1]
w = bar_group_width / n_grp
for grpi in range(n_grp):
offset = grpi * w
if horizontal:
plt.barh(np.array(xpos) - bar_group_width / 2 + (bar_group_width / 2) * w + offset, ydata[:, grpi], height=w, label=group_list[grpi])
else:
plt.bar(np.array(xpos) - bar_group_width / 2 + (bar_group_width / 2) * w + offset, ydata[:, grpi], width=w, label=group_list[grpi])
else:
if horizontal:
ax.barh(xpos, data_mean, color='gray')
else:
ax.bar(xpos, data_mean, color='gray')
def __plot_violin(
ax, xpos, data,
horizontal=False,
grouping=False, group_list=[],
bar_group_width=0.8, points=100
):
if grouping:
n_grp = len(group_list)
w = bar_group_width / (n_grp + 1)
group_label_list = []
for grpi in range(n_grp):
offset = grpi * w - (n_grp // 2) * w
xpos_grp = np.array(xpos) + offset #- bar_group_width / 2 + (bar_group_width / 2) * w + offset
ydata_grp = [a_data[grpi] for a_data in data]
violinobj = ax.violinplot(
ydata_grp, xpos_grp,
vert=not horizontal,
showmeans=True, showextrema=False, showmedians=False, points=points,
widths=w * 0.8)
color = violinobj["bodies"][0].get_facecolor().flatten()
group_label_list.append((mpatches.Patch(color=color), group_list[grpi]))
else:
ax.violinplot(data, xpos, vert=not horizontal, showmeans=True, showextrema=False, showmedians=False, points=points)
group_label_list = None
return group_label_list
def __plot_violin_paired(
ax, xpos, data,
horizontal=False,
grouping=False, group_list=[],
points=100, colors=None
):
assert grouping
n_grp = len(group_list)
assert n_grp == 2
group_label_list = []
if colors is not None and len(colors) >= 2:
__draw_half_violin(ax, [a_data[0] for a_data in data], points, xpos, color=colors[0], left=True, vert=not horizontal)
__draw_half_violin(ax, [a_data[1] for a_data in data], points, xpos, color=colors[1], left=False, vert=not horizontal)
else:
colors = []
color = __draw_half_violin(ax, [a_data[0] for a_data in data], points, xpos, color=None, left=True, vert=not horizontal)
colors.append(color)
color = __draw_half_violin(ax, [a_data[1] for a_data in data], points, xpos, color=None, left=False, vert=not horizontal)
colors.append(color)
for color, label in zip(colors, group_list):
group_label_list.append((mpatches.Patch(color=color), label))
return group_label_list
def __plot_swarm(
ax, x_list, data,
horizontal=False,
grouping=False,
dot_color='#595959', dot_size=1.5, dot_alpha=0.8,
violin_color='blue'
):
if grouping:
raise RuntimeError("The function of grouping on `swarm` plot is not implemeted yet.")
else:
df_list = []
for xi, x_lbl in enumerate(x_list):
a_df = pd.DataFrame.from_dict({'y': data[xi]})
a_df['x'] = x_lbl
df_list.append(a_df)
tmp_df = pd.concat(df_list)
mean_df = tmp_df.groupby('x', as_index=False).mean()
mean_list = [mean_df[mean_df['x'] == x_lbl]['y'].values[0] for x_lbl in x_list]
if horizontal:
plotx, ploty = 'y', 'x'
scatterx, scattery = mean_list, np.arange(len(x_list))
scattermark = "|"
else:
plotx, ploty = 'x', 'y'
scatterx, scattery = np.arange(len(x_list)), mean_list
scattermark = "_"
ax = sns.violinplot(
x=plotx, y=ploty, order=x_list, orient="h" if horizontal else "v",
data=tmp_df, ax=ax, color=violin_color, linewidth=0
)
for violin in ax.collections[::2]:
violin.set_alpha(0.6)
sns.swarmplot(
x=plotx, y=ploty, order=x_list, orient="h" if horizontal else "v",
data=tmp_df, ax=ax, color=dot_color, alpha=dot_alpha, size=dot_size
)
ax.scatter(x=scatterx, y=scattery, marker=scattermark, c="red", linewidths=2, zorder=10)
ax.set(xlabel=None, ylabel=None)
def __plot_box(
ax, x, y, x_list, df_t,
horizontal=False, reverse_x=False,
grouping=False, group=None, group_list=[],
box_color='blue', box_width=0.5, box_linewidth=1, box_props={'alpha': .3},
box_meanprops=dict(linestyle='-', linewidth=1.5, color='red'),
box_medianprops={},
flierprops={}
):
# prepare plot
#box_color_palette = sns.color_palette("pastel") if grouping else None
box_color_palette = sns.color_palette("bright") if grouping else None
if horizontal:
plotx, ploty = y, x
else:
plotx, ploty = x, y
# plot boxplot
if grouping:
boxax = sns.boxplot(
data=df_t, ax=ax,
x=plotx, y=ploty, order=x_list, hue=group, hue_order=group_list,
orient="h" if horizontal else "v",
palette=box_color_palette,
linewidth=box_linewidth,
showfliers=True,flierprops=flierprops,
showmeans=True, meanline=True, meanprops=box_meanprops,
medianprops=box_medianprops,
boxprops=box_props, zorder=100 # <- This zorder is very important for visualization.
)
# prepare legend
handlers, labels = boxax.get_legend_handles_labels()
handlers = handlers[:len(group_list)]
labels = labels[:len(group_list)]
if horizontal:
legend_handler = [handlers[::-1], labels[::-1]]
else:
legend_handler = [handlers, labels]
ax.get_legend().remove()
else:
sns.boxplot(
data=df_t, ax=ax,
x=plotx, y=ploty, order=x_list,
orient="h" if horizontal else "v",
color=box_color,
linewidth=box_linewidth,
width=box_width,
showfliers=True, flierprops=flierprops,
showmeans=True, meanline=True, meanprops=box_meanprops,
medianprops=box_medianprops,
boxprops=box_props, zorder=100 # <- This zorder is very important for visualization.
)
legend_handler = None
# remove label
ax.set(xlabel=None, ylabel=None)
return legend_handler
def __plot_swarmbox(
ax, x, y, x_list, df_t,
horizontal=False, reverse_x=False,
grouping=False, group=None, group_list=[],
dot_color='#696969', dot_size=3, dot_alpha=0.7,
box_color='blue', box_width=0.5, box_linewidth=1, box_props={'alpha': .3},
box_meanprops=dict(linestyle='-', linewidth=1.5, color='red'),
box_medianprops={}
):
# warning
if grouping:
warnings.warn('When grouping is True, "box_width" is not working to make the layout consistent with the swarm plot.')
# prepare plot
box_color_palette = sns.color_palette("pastel") if grouping else None
dot_color_palette = sns.color_palette("bright") if grouping else None
if horizontal:
plotx, ploty = y, x
else:
plotx, ploty = x, y
# plot swarmplot
if grouping:
sns.swarmplot(
data=df_t, ax=ax,
x=plotx, y=ploty, order=x_list, hue=group, hue_order=group_list,
orient="h" if horizontal else "v",
palette=dot_color_palette,
dodge=True,
color=dot_color, size=dot_size, alpha=dot_alpha, zorder=10
)
else:
sns.swarmplot(
data=df_t, ax=ax,
x=plotx, y=ploty, order=x_list,
orient="h" if horizontal else "v",
color=dot_color, size=dot_size, alpha=dot_alpha, zorder=10
)
# plot boxplot
if grouping:
boxax = sns.boxplot(
data=df_t, ax=ax,
x=plotx, y=ploty, order=x_list, hue=group, hue_order=group_list,
orient="h" if horizontal else "v",
palette=dot_color_palette,
linewidth=box_linewidth,
showfliers=False,
showmeans=True, meanline=True, meanprops=box_meanprops,
medianprops=box_medianprops,
boxprops=box_props, zorder=100 # <- This zorder is very important for visualization.
)
# prepare legend
handlers, labels = boxax.get_legend_handles_labels()
handlers = handlers[:len(group_list)]
labels = labels[:len(group_list)]
if horizontal:
legend_handler = [handlers[::-1], labels[::-1]]
else:
legend_handler = [handlers, labels]
ax.get_legend().remove()
else:
sns.boxplot(
data=df_t, ax=ax,
x=plotx, y=ploty, order=x_list,
orient="h" if horizontal else "v",
color=box_color,
linewidth=box_linewidth,
width=box_width,
showfliers=False,
showmeans=True, meanline=True, meanprops=box_meanprops,
medianprops=box_medianprops,
boxprops=box_props, zorder=100 # <- This zorder is very important for visualization.
)
legend_handler = None
# remove label
ax.set(xlabel=None, ylabel=None)
return legend_handler
def __split_list(l, n):
for idx in range(0, len(l), n):
yield l[idx:idx + n]
def __get_data(
df, subplot, sp_label,
x, x_list, figure, fig_label, y,
group, group_list, grouping, removenan
):
data = []
for j, x_lbl in enumerate(x_list):
if grouping:
data_t = []
for group_label in group_list:
if fig_label is None:
df_t = df.query('`{}` == "{}" & `{}` == "{}" & `{}` == "{}"'.format(subplot, sp_label, group, group_label, x, x_lbl))
else:
df_t = df.query('`{}` == "{}" & `{}` == "{}" & `{}` == "{}" & `{}` == "{}"'.format(subplot, sp_label, group, group_label, figure, fig_label, x, x_lbl))
data_tt = df_t[y].values
if removenan:
data_tt[0] = np.delete(data_tt[0], np.isnan(data_tt[0])) # FXIME
data_tt = np.array([np.nan, np.nan]) if len(data_tt) == 0 else np.concatenate(data_tt)
data_t.append(data_tt)
# violinplot requires at least two elements in the dataset
else:
if fig_label is None:
df_t = df.query('`{}` == "{}" & `{}` == "{}"'.format(subplot, sp_label, x, x_lbl))
else:
df_t = df.query('`{}` == "{}" & `{}` == "{}" & `{}` == "{}"'.format(subplot, sp_label, figure, fig_label, x, x_lbl))
data_t = df_t[y].values
if removenan:
data_t[0] = np.delete(data_t[0], np.isnan(data_t[0])) # FXIME
data_t = np.array([np.nan, np.nan]) if len(data_t) == 0 else np.concatenate(data_t)
# violinplot requires at least two elements in the dataset
data.append(data_t)
return data
def __strict_data(
df, subplot, sp_label, figure, fig_label, y, removenan
):
if fig_label is None and sp_label is None:
df_t = df
elif fig_label is None:
df_t = df.query('`{}` == "{}"'.format(subplot, sp_label))
else:
df_t = df.query('`{}` == "{}" & `{}` == "{}"'.format(subplot, sp_label, figure, fig_label))
df_t = df_t.reset_index(drop=True)
if removenan:
df_t[y] = df_t[y].apply(lambda x: np.delete(x, np.isnan(x)))
return df_t
def __weird_form_to_long(df, target_col, identify_cols=[]):
df_result = pd.DataFrame()
for i, row in df.iterrows():
tmp = {}
for col in identify_cols:
tmp[col] = row[col]
tmp[target_col] = row[target_col]
df_result = pd.concat([df_result, pd.DataFrame(tmp)])
return df_result
def __draw_half_violin(
ax, data, points, positions,
color=None, left=True, vert=True
):
v = ax.violinplot(data, points=points, positions=positions, vert=vert,
showmeans=True, showextrema=False, showmedians=False)
for i, b in enumerate(v['bodies']):
if i == 0 and color is None:
color = b.get_facecolor().flatten()
if vert:
# get the center
m = np.mean(b.get_paths()[0].vertices[:, 0])
if left:
# modify the paths to not go further right than the center
b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], -np.inf, m)
else: # right
b.get_paths()[0].vertices[:, 0] = np.clip(b.get_paths()[0].vertices[:, 0], m, np.inf)
else:
# get the center
m = np.mean(b.get_paths()[0].vertices[:, 1])
if left:
b.get_paths()[0].vertices[:, 1] = np.clip(b.get_paths()[0].vertices[:, 1], -np.inf, m)
else: # right
b.get_paths()[0].vertices[:, 1] = np.clip(b.get_paths()[0].vertices[:, 1], m, np.inf)
if color is not None:
# TODO: error handling
b.set_color(color)
return color