Skip to content

added force_order param in violin FF #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

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

## [2.0.8] - 2017-04-21
### Added
Expand Down
39 changes: 25 additions & 14 deletions plotly/figure_factory/_violin.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def violinplot(vals, fillcolor='#1f77b4', rugplot=True):


def violin_no_colorscale(data, data_header, group_header, colors,
use_colorscale, group_stats, rugplot,
use_colorscale, group_stats, rugplot, sort,
height, width, title):
"""
Refer to FigureFactory.create_violin() for docstring.
Expand All @@ -208,7 +208,8 @@ def violin_no_colorscale(data, data_header, group_header, colors,
for name in data[group_header]:
if name not in group_name:
group_name.append(name)
group_name.sort()
if sort:
group_name.sort()

gb = data.groupby([group_header])
L = len(group_name)
Expand All @@ -223,8 +224,7 @@ def violin_no_colorscale(data, data_header, group_header, colors,
if color_index >= len(colors):
color_index = 0
plot_data, plot_xrange = violinplot(vals,
fillcolor=colors[color_index],
rugplot=rugplot)
fillcolor=colors[color_index])
layout = graph_objs.Layout()

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


def violin_colorscale(data, data_header, group_header, colors, use_colorscale,
group_stats, rugplot, height, width, title):
group_stats, rugplot, sort, height, width,
title):
"""
Refer to FigureFactory.create_violin() for docstring.

Expand All @@ -264,7 +265,8 @@ def violin_colorscale(data, data_header, group_header, colors, use_colorscale,
for name in data[group_header]:
if name not in group_name:
group_name.append(name)
group_name.sort()
if sort:
group_name.sort()

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


def violin_dict(data, data_header, group_header, colors, use_colorscale,
group_stats, rugplot, height, width, title):
group_stats, rugplot, sort, height, width, title):
"""
Refer to FigureFactory.create_violin() for docstring.

Expand All @@ -358,7 +360,9 @@ def violin_dict(data, data_header, group_header, colors, use_colorscale,
for name in data[group_header]:
if name not in group_name:
group_name.append(name)
group_name.sort()

if sort:
group_name.sort()

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

def create_violin(data, data_header=None, group_header=None, colors=None,
use_colorscale=False, group_stats=None, rugplot=True,
height=450, width=600, title='Violin and Rug Plot'):
sort=False, height=450, width=600,
title='Violin and Rug Plot'):
"""
Returns figure for a violin plot

Expand All @@ -429,12 +434,15 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
variable. Will implement a colorscale based on the first 2 colors
of param colors. This means colors must be a list with at least 2
colors in it (Plotly colorscales are accepted since they map to a
list of two rgb colors).
list of two rgb colors). Default = False
:param (dict) group_stats: a dictioanry where each key is a unique
value from the group_header column in data. Each value must be a
number and will be used to color the violin plots if a colorscale
is being used.
:param (bool) rugplot: determines if a rugplot is draw on violin plot.
Default = True
:param (bool) sort: determines if violins are sorted
alphabetically (True) or by input order (False). Default = False
:param (float) height: the height of the violin plot.
:param (float) width: the width of the violin plot.
:param (str) title: the title of the violin plot.
Expand Down Expand Up @@ -482,7 +490,7 @@ def create_violin(data, data_header=None, group_header=None, colors=None,

# create violin fig
fig = create_violin(df, data_header='Score', group_header='Group',
height=600, width=1000)
sort=True, height=600, width=1000)

# plot
py.iplot(fig, filename='Violin Plot with Coloring')
Expand Down Expand Up @@ -601,13 +609,15 @@ def create_violin(data, data_header=None, group_header=None, colors=None,
# validate colors dict choice below
fig = violin_dict(
data, data_header, group_header, valid_colors,
use_colorscale, group_stats, rugplot, height, width, title
use_colorscale, group_stats, rugplot, sort,
height, width, title
)
return fig
else:
fig = violin_no_colorscale(
data, data_header, group_header, valid_colors,
use_colorscale, group_stats, rugplot, height, width, title
use_colorscale, group_stats, rugplot, sort,
height, width, title
)
return fig
else:
Expand All @@ -627,6 +637,7 @@ def create_violin(data, data_header=None, group_header=None, colors=None,

fig = violin_colorscale(
data, data_header, group_header, valid_colors,
use_colorscale, group_stats, rugplot, height, width, title
use_colorscale, group_stats, rugplot, sort, height,
width, title
)
return fig