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

Skip to content

Commit 91a33e8

Browse files
committed
Re-added points parameter to violinplot.
Fixed several style issues.
1 parent e8b3041 commit 91a33e8

File tree

1 file changed

+69
-84
lines changed

1 file changed

+69
-84
lines changed

lib/matplotlib/axes/_axes.py

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

6728-
def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False,
6729-
showextrema=True, showmedians=False):
6728+
def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
6729+
showmeans=False, showextrema=True, showmedians=False,
6730+
points=100):
67306731
"""
67316732
Make a violin plot.
67326733
67336734
Call signature::
67346735
6735-
violinplot(dataset, positions=None)
6736+
violinplot(dataset, positions=None, vert=True, widths=0.5,
6737+
showmeans=False, showextrema=True, showmedians=False,
6738+
points=100):
67366739
6737-
Make a violin plot for each column of *dataset* or each vector in
6740+
Make a violin plot for each column of *dataset* or each vector in
67386741
sequence *dataset*. Each filled area extends to represent the
67396742
entire data range, with three lines at the mean, the minimum, and
67406743
the maximum.
@@ -6750,8 +6753,8 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
67506753
automatically set to match the positions.
67516754
67526755
vert : bool, default = True.
6753-
If true, creates vertical violin plot
6754-
Else, creates horizontal violin plot
6756+
If true, creates a vertical violin plot.
6757+
Otherwise, creates a horizontal violin plot.
67556758
67566759
widths : array-like, default = 0.5
67576760
Either a scalar or a vector that sets the maximal width of
@@ -6767,14 +6770,18 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
67676770
showmedians : bool, default = False
67686771
If true, will toggle rendering of the medians.
67696772
6773+
points : scalar, default = 100
6774+
Defines the number of points to evaluate each of the gaussian
6775+
kernel density estimations at.
6776+
67706777
Returns
67716778
-------
67726779
67736780
A dictionary mapping each component of the violinplot to a list of the
67746781
corresponding collection instances created. The dictionary has
67756782
the following keys:
67766783
6777-
- bodies: A list of the
6784+
- bodies: A list of the
67786785
:class:`matplotlib.collections.PolyCollection` instances
67796786
containing the filled area of each violin.
67806787
- means: A :class:`matplotlib.collections.LineCollection` instance
@@ -6786,9 +6793,9 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
67866793
created to identify the top of each violin's distribution.
67876794
- bars: A :class:`matplotlib.collections.LineCollection` instance
67886795
created to identify the centers of each violin's distribution.
6789-
- medians: A :class:`matplotlib.collections.LineCollection` instance
6790-
created to identify the median values of each of the violin's
6791-
distribution.
6796+
- medians: A :class:`matplotlib.collections.LineCollection`
6797+
instance created to identify the median values of each of the
6798+
violin's distribution.
67926799
67936800
"""
67946801

@@ -6799,15 +6806,20 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
67996806
medians = []
68006807

68016808
# Collections to be returned
6802-
bodies = []
6803-
cmeans = None
6804-
cmaxes = None
6805-
cmins = None
6806-
cbars = None
6807-
cmedians = None
6809+
artists = {
6810+
'bodies': [],
6811+
'cmeans': None,
6812+
'cmaxes': None,
6813+
'cmins': None,
6814+
'cbars': None,
6815+
'cmedians': None
6816+
}
6817+
6818+
datashape_message = ("List of violinplot statistics and `{0}` "
6819+
"values must have the same length")
68086820

68096821
# Validate positions
6810-
if positions == None:
6822+
if positions is None:
68116823
positions = range(1, len(dataset) + 1)
68126824
elif len(positions) != len(dataset):
68136825
raise ValueError(datashape_message.format("positions"))
@@ -6825,91 +6837,64 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=F
68256837
# Check hold status
68266838
if not self._hold:
68276839
self.cla()
6828-
holdStatus = self._hold
6840+
hold_status = self._hold
6841+
6842+
# Check whether we are rendering vertically or horizontally
6843+
if vert:
6844+
fill = self.fill_betweenx
6845+
rlines = self.hlines
6846+
blines = self.vlines
6847+
else:
6848+
fill = self.fill_between
6849+
rlines = self.vlines
6850+
blines = self.hlines
68296851

68306852
# Render violins
6831-
for d,p,w in zip(dataset,positions,widths):
6853+
for data, pos, width in zip(dataset, positions, widths):
68326854
# Calculate the kernel density
6833-
kde = mlab.ksdensity(d)
6834-
m = kde['xmin']
6835-
M = kde['xmax']
6855+
kde = mlab.ksdensity(data)
6856+
min_val = kde['xmin']
6857+
max_val = kde['xmax']
68366858
mean = kde['mean']
68376859
median = kde['median']
6838-
v = kde['result']
6839-
coords = np.arange(m,M,(M-m)/100.)
6860+
vals = kde['result']
6861+
coords = np.arange(min_val, max_val, (max_val - min_val)/points)
68406862

68416863
# Since each data point p is plotted from v-p to v+p,
68426864
# we need to scale it by an additional 0.5 factor so that we get
68436865
# correct width in the end.
6844-
v = 0.5 * w * v/v.max()
6845-
6846-
# create vertical violin plot
6847-
if vert:
6848-
bodies += [self.fill_betweenx(coords,
6849-
-v+p,
6850-
v+p,
6851-
facecolor='y',
6852-
alpha=0.3)]
6853-
# create horizontal violin plot
6854-
else:
6855-
bodies += [self.fill_between(coords,
6856-
-v+p,
6857-
v+p,
6858-
facecolor='y',
6859-
alpha=0.3)]
6866+
vals = 0.5 * width * vals/vals.max()
6867+
6868+
# create the violin bodies
6869+
artists['bodies'] += [fill(coords,
6870+
-vals + pos,
6871+
vals + pos,
6872+
facecolor='y',
6873+
alpha=0.3)]
68606874

68616875
means.append(mean)
6862-
mins.append(m)
6863-
maxes.append(M)
6876+
mins.append(min_val)
6877+
maxes.append(max_val)
68646878
medians.append(median)
68656879

6866-
# respective means, extrema median on vertical violin plot
6867-
if vert:
6868-
# Render means
6869-
if showmeans:
6870-
cmeans = self.hlines(means, pmins, pmaxes, colors='r')
6871-
6872-
# Render extrema
6873-
if showextrema:
6874-
cmaxes = self.hlines(maxes, pmins, pmaxes, colors='r')
6875-
cmins = self.hlines(mins, pmins, pmaxes, colors='r')
6876-
cbars = self.vlines(positions, mins, maxes, colors='r')
6877-
6878-
# Render medians
6879-
if showmedians:
6880-
cmedians = self.hlines(medians, pmins, pmaxes, colors='r')
6881-
6882-
# respective means, extrema median on horizontal violin plot
6883-
else:
6884-
# Render means
6885-
if showmeans:
6886-
cmeans = self.vlines(means, pmins, pmaxes, colors='r')
6887-
6888-
# Render extrema
6889-
if showextrema:
6890-
cmaxes = self.vlines(maxes, pmins, pmaxes, colors='r')
6891-
cmins = self.vlines(mins, pmins, pmaxes, colors='r')
6892-
cbars = self.hlines(positions, mins, maxes, colors='r')
6893-
6894-
# Render medians
6895-
if showmedians:
6896-
cmedians = self.vlines(medians, pmins, pmaxes, colors='r')
6897-
6898-
6880+
# Render means
6881+
if showmeans:
6882+
artists['cmeans'] = rlines(means, pmins, pmaxes, colors='r')
68996883

6884+
# Render extrema
6885+
if showextrema:
6886+
artists['cmaxes'] = rlines(maxes, pmins, pmaxes, colors='r')
6887+
artists['cmins'] = rlines(mins, pmins, pmaxes, colors='r')
6888+
artists['cbars'] = blines(positions, mins, maxes, colors='r')
69006889

6890+
# Render medians
6891+
if showmedians:
6892+
artists['cmedians'] = rlines(medians, pmins, pmaxes, colors='r')
69016893

69026894
# Reset hold
6903-
self.hold(holdStatus)
6895+
self.hold(hold_status)
69046896

6905-
return {
6906-
'bodies' : bodies,
6907-
'means' : cmeans,
6908-
'mins' : cmins,
6909-
'maxes' : cmaxes,
6910-
'bars' : cbars,
6911-
'medians' : cmedians
6912-
}
6897+
return artists
69136898

69146899

69156900
def tricontour(self, *args, **kwargs):

0 commit comments

Comments
 (0)