@@ -194,7 +194,7 @@ def violinplot(vals, fillcolor='#1f77b4', rugplot=True):
194
194
195
195
196
196
def violin_no_colorscale (data , data_header , group_header , colors ,
197
- use_colorscale , group_stats , rugplot ,
197
+ use_colorscale , group_stats , rugplot , sort ,
198
198
height , width , title ):
199
199
"""
200
200
Refer to FigureFactory.create_violin() for docstring.
@@ -208,7 +208,8 @@ def violin_no_colorscale(data, data_header, group_header, colors,
208
208
for name in data [group_header ]:
209
209
if name not in group_name :
210
210
group_name .append (name )
211
- group_name .sort ()
211
+ if sort :
212
+ group_name .sort ()
212
213
213
214
gb = data .groupby ([group_header ])
214
215
L = len (group_name )
@@ -223,8 +224,7 @@ def violin_no_colorscale(data, data_header, group_header, colors,
223
224
if color_index >= len (colors ):
224
225
color_index = 0
225
226
plot_data , plot_xrange = violinplot (vals ,
226
- fillcolor = colors [color_index ],
227
- rugplot = rugplot )
227
+ fillcolor = colors [color_index ])
228
228
layout = graph_objs .Layout ()
229
229
230
230
for item in plot_data :
@@ -251,7 +251,8 @@ def violin_no_colorscale(data, data_header, group_header, colors,
251
251
252
252
253
253
def violin_colorscale (data , data_header , group_header , colors , use_colorscale ,
254
- group_stats , rugplot , height , width , title ):
254
+ group_stats , rugplot , sort , height , width ,
255
+ title ):
255
256
"""
256
257
Refer to FigureFactory.create_violin() for docstring.
257
258
@@ -264,7 +265,8 @@ def violin_colorscale(data, data_header, group_header, colors, use_colorscale,
264
265
for name in data [group_header ]:
265
266
if name not in group_name :
266
267
group_name .append (name )
267
- group_name .sort ()
268
+ if sort :
269
+ group_name .sort ()
268
270
269
271
# make sure all group names are keys in group_stats
270
272
for group in group_name :
@@ -345,7 +347,7 @@ def violin_colorscale(data, data_header, group_header, colors, use_colorscale,
345
347
346
348
347
349
def violin_dict (data , data_header , group_header , colors , use_colorscale ,
348
- group_stats , rugplot , height , width , title ):
350
+ group_stats , rugplot , sort , height , width , title ):
349
351
"""
350
352
Refer to FigureFactory.create_violin() for docstring.
351
353
@@ -358,7 +360,9 @@ def violin_dict(data, data_header, group_header, colors, use_colorscale,
358
360
for name in data [group_header ]:
359
361
if name not in group_name :
360
362
group_name .append (name )
361
- group_name .sort ()
363
+
364
+ if sort :
365
+ group_name .sort ()
362
366
363
367
# check if all group names appear in colors dict
364
368
for group in group_name :
@@ -405,7 +409,8 @@ def violin_dict(data, data_header, group_header, colors, use_colorscale,
405
409
406
410
def create_violin (data , data_header = None , group_header = None , colors = None ,
407
411
use_colorscale = False , group_stats = None , rugplot = True ,
408
- height = 450 , width = 600 , title = 'Violin and Rug Plot' ):
412
+ sort = False , height = 450 , width = 600 ,
413
+ title = 'Violin and Rug Plot' ):
409
414
"""
410
415
Returns figure for a violin plot
411
416
@@ -429,12 +434,15 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
429
434
variable. Will implement a colorscale based on the first 2 colors
430
435
of param colors. This means colors must be a list with at least 2
431
436
colors in it (Plotly colorscales are accepted since they map to a
432
- list of two rgb colors).
437
+ list of two rgb colors). Default = False
433
438
:param (dict) group_stats: a dictioanry where each key is a unique
434
439
value from the group_header column in data. Each value must be a
435
440
number and will be used to color the violin plots if a colorscale
436
441
is being used.
437
442
:param (bool) rugplot: determines if a rugplot is draw on violin plot.
443
+ Default = True
444
+ :param (bool) sort: determines if violins are sorted
445
+ alphabetically (True) or by input order (False). Default = False
438
446
:param (float) height: the height of the violin plot.
439
447
:param (float) width: the width of the violin plot.
440
448
:param (str) title: the title of the violin plot.
@@ -482,7 +490,7 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
482
490
483
491
# create violin fig
484
492
fig = create_violin(df, data_header='Score', group_header='Group',
485
- height=600, width=1000)
493
+ sort=True, height=600, width=1000)
486
494
487
495
# plot
488
496
py.iplot(fig, filename='Violin Plot with Coloring')
@@ -601,13 +609,15 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
601
609
# validate colors dict choice below
602
610
fig = violin_dict (
603
611
data , data_header , group_header , valid_colors ,
604
- use_colorscale , group_stats , rugplot , height , width , title
612
+ use_colorscale , group_stats , rugplot , sort ,
613
+ height , width , title
605
614
)
606
615
return fig
607
616
else :
608
617
fig = violin_no_colorscale (
609
618
data , data_header , group_header , valid_colors ,
610
- use_colorscale , group_stats , rugplot , height , width , title
619
+ use_colorscale , group_stats , rugplot , sort ,
620
+ height , width , title
611
621
)
612
622
return fig
613
623
else :
@@ -627,6 +637,7 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
627
637
628
638
fig = violin_colorscale (
629
639
data , data_header , group_header , valid_colors ,
630
- use_colorscale , group_stats , rugplot , height , width , title
640
+ use_colorscale , group_stats , rugplot , sort , height ,
641
+ width , title
631
642
)
632
643
return fig
0 commit comments