Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9f71fb

Browse files
committed
Added functionality for plotting the actual violin bodies to violinplot.
1 parent 4b1bd63 commit d9f71fb

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6725,6 +6725,88 @@ def matshow(self, Z, **kwargs):
67256725
integer=True))
67266726
return im
67276727

6728+
6729+
def violinplot(self, x, positions=None, width=0.5):
6730+
"""
6731+
Make a violin plot.
6732+
6733+
Call signature::
6734+
6735+
violinplot(x, positions=None)
6736+
6737+
Make a violin plot for each column of *x* or each
6738+
vector in sequence *x*. Each filled area extends to represent the
6739+
entire data range, with three lines at the mean, the minimum, and
6740+
the maximum.
6741+
6742+
Parameters
6743+
----------
6744+
6745+
x : Array or a sequence of vectors.
6746+
The input data.
6747+
6748+
positions : array-like, default = [1, 2, ..., n]
6749+
Sets the positions of the violins. The ticks and limits are
6750+
automatically set to match the positions.
6751+
6752+
width : array-like, default = 0.5
6753+
Either a scalar or a vector that sets the maximal width of
6754+
each violin. The default is 0.5, which uses about half of the
6755+
available horizontal space.
6756+
6757+
Returns
6758+
-------
6759+
6760+
A dictionary mapping each component of the violinplot to a list of the
6761+
corresponding collection instances created. The dictionary has
6762+
the following keys:
6763+
6764+
- bodies: A list of the
6765+
:class:`matplotlib.collections.PolyCollection` instances
6766+
containing the filled area of each violin.
6767+
- means: A list of the :class:`matplotlib.lines.Line2D` instances
6768+
created to identify the mean values for each of the violins.
6769+
- caps: A list of the :class:`matplotlib.lines.Line2D` instances
6770+
created to identify the extremal values of each violin's
6771+
data set.
6772+
6773+
"""
6774+
6775+
bodies = []
6776+
means = []
6777+
caps = []
6778+
6779+
if positions == None:
6780+
positions = range(1, len(x) + 1)
6781+
elif len(positions) != len(x):
6782+
raise ValueError(datashape_message.format("positions"))
6783+
6784+
# TODO: Use kde estimation function on x
6785+
# These numbers are contrived
6786+
coords = np.arange(0.0, np.pi, np.pi/100.)
6787+
datasets = map(lambda i: np.sin(coords) ** i, range(1,len(x) + 1))
6788+
6789+
for d,x in zip(datasets,positions):
6790+
# Since each data point p is plotted from x-p to x+p,
6791+
# we need to scale it by an additional 0.5 factor so that we get
6792+
# correct width in the end.
6793+
d = 0.5 * widths * d/d.max()
6794+
m = d.min() # This should actually be the min for the dataset
6795+
M = d.max() # likewise
6796+
# bodies += [self.fill_betweenx(np.arange(m,M,(M-m)/100.),
6797+
bodies += [self.fill_betweenx(coords,
6798+
-d+x,
6799+
d+x,
6800+
facecolor='y',
6801+
alpha=0.3)]
6802+
6803+
return {
6804+
'bodies' : bodies,
6805+
'means' : means,
6806+
'caps' : caps
6807+
}
6808+
6809+
67286810
def tricontour(self, *args, **kwargs):
67296811
return mtri.tricontour(self, *args, **kwargs)
67306812
tricontour.__doc__ = mtri.TriContourSet.tricontour_doc

0 commit comments

Comments
 (0)