Skip to content

Commit 077fc40

Browse files
authored
Merge pull request #755 from plotly/forceorder_violin
added force_order param in violin FF
2 parents 633f978 + 636d065 commit 077fc40

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
6+
### Added
7+
- 'sort' parameter to `FF.create_violin` to control whether violin plots are sorted alphabetically.
68

79
## [2.0.8] - 2017-04-21
810
### Added

plotly/figure_factory/_violin.py

+25-14
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def violinplot(vals, fillcolor='#1f77b4', rugplot=True):
194194

195195

196196
def violin_no_colorscale(data, data_header, group_header, colors,
197-
use_colorscale, group_stats, rugplot,
197+
use_colorscale, group_stats, rugplot, sort,
198198
height, width, title):
199199
"""
200200
Refer to FigureFactory.create_violin() for docstring.
@@ -208,7 +208,8 @@ def violin_no_colorscale(data, data_header, group_header, colors,
208208
for name in data[group_header]:
209209
if name not in group_name:
210210
group_name.append(name)
211-
group_name.sort()
211+
if sort:
212+
group_name.sort()
212213

213214
gb = data.groupby([group_header])
214215
L = len(group_name)
@@ -223,8 +224,7 @@ def violin_no_colorscale(data, data_header, group_header, colors,
223224
if color_index >= len(colors):
224225
color_index = 0
225226
plot_data, plot_xrange = violinplot(vals,
226-
fillcolor=colors[color_index],
227-
rugplot=rugplot)
227+
fillcolor=colors[color_index])
228228
layout = graph_objs.Layout()
229229

230230
for item in plot_data:
@@ -251,7 +251,8 @@ def violin_no_colorscale(data, data_header, group_header, colors,
251251

252252

253253
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):
255256
"""
256257
Refer to FigureFactory.create_violin() for docstring.
257258
@@ -264,7 +265,8 @@ def violin_colorscale(data, data_header, group_header, colors, use_colorscale,
264265
for name in data[group_header]:
265266
if name not in group_name:
266267
group_name.append(name)
267-
group_name.sort()
268+
if sort:
269+
group_name.sort()
268270

269271
# make sure all group names are keys in group_stats
270272
for group in group_name:
@@ -345,7 +347,7 @@ def violin_colorscale(data, data_header, group_header, colors, use_colorscale,
345347

346348

347349
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):
349351
"""
350352
Refer to FigureFactory.create_violin() for docstring.
351353
@@ -358,7 +360,9 @@ def violin_dict(data, data_header, group_header, colors, use_colorscale,
358360
for name in data[group_header]:
359361
if name not in group_name:
360362
group_name.append(name)
361-
group_name.sort()
363+
364+
if sort:
365+
group_name.sort()
362366

363367
# check if all group names appear in colors dict
364368
for group in group_name:
@@ -405,7 +409,8 @@ def violin_dict(data, data_header, group_header, colors, use_colorscale,
405409

406410
def create_violin(data, data_header=None, group_header=None, colors=None,
407411
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'):
409414
"""
410415
Returns figure for a violin plot
411416
@@ -429,12 +434,15 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
429434
variable. Will implement a colorscale based on the first 2 colors
430435
of param colors. This means colors must be a list with at least 2
431436
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
433438
:param (dict) group_stats: a dictioanry where each key is a unique
434439
value from the group_header column in data. Each value must be a
435440
number and will be used to color the violin plots if a colorscale
436441
is being used.
437442
: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
438446
:param (float) height: the height of the violin plot.
439447
:param (float) width: the width of the violin plot.
440448
: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,
482490
483491
# create violin fig
484492
fig = create_violin(df, data_header='Score', group_header='Group',
485-
height=600, width=1000)
493+
sort=True, height=600, width=1000)
486494
487495
# plot
488496
py.iplot(fig, filename='Violin Plot with Coloring')
@@ -601,13 +609,15 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
601609
# validate colors dict choice below
602610
fig = violin_dict(
603611
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
605614
)
606615
return fig
607616
else:
608617
fig = violin_no_colorscale(
609618
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
611621
)
612622
return fig
613623
else:
@@ -627,6 +637,7 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
627637

628638
fig = violin_colorscale(
629639
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
631642
)
632643
return fig

0 commit comments

Comments
 (0)