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

Skip to content

Commit 3c4c619

Browse files
donaldsolvents
donald
authored andcommitted
Added horizontal violin plot feature compatible with means, extremas and medians.
1 parent b9d7e03 commit 3c4c619

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

lib/matplotlib/axes/_axes.py

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

6728-
def violinplot(self, dataset, positions=None, widths=0.5, showmeans=False,
6728+
def violinplot(self, dataset, positions=None, vert=True, widths=0.5, showmeans=False,
67296729
showextrema=True, showmedians=False):
67306730
"""
67316731
Make a violin plot.
@@ -6749,6 +6749,10 @@ def violinplot(self, dataset, positions=None, widths=0.5, showmeans=False,
67496749
Sets the positions of the violins. The ticks and limits are
67506750
automatically set to match the positions.
67516751
6752+
vert : bool, default = True.
6753+
If true, creates vertical violin plot
6754+
Else, creates horizontal violin plot
6755+
67526756
widths : array-like, default = 0.5
67536757
Either a scalar or a vector that sets the maximal width of
67546758
each violin. The default is 0.5, which uses about half of the
@@ -6839,30 +6843,61 @@ def violinplot(self, dataset, positions=None, widths=0.5, showmeans=False,
68396843
# correct width in the end.
68406844
v = 0.5 * w * v/v.max()
68416845

6842-
bodies += [self.fill_betweenx(coords,
6846+
# create vertical violin plot
6847+
if vert:
6848+
bodies += [self.fill_betweenx(coords,
68436849
-v+p,
68446850
v+p,
68456851
facecolor='y',
68466852
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)]
68476860

68486861
means.append(mean)
68496862
mins.append(m)
68506863
maxes.append(M)
68516864
medians.append(median)
68526865

6853-
# Render means
6854-
if showmeans:
6855-
cmeans = self.hlines(means, pmins, pmaxes, colors='r')
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+
68566899

6857-
# Render extrema
6858-
if showextrema:
6859-
cmaxes = self.hlines(maxes, pmins, pmaxes, colors='r')
6860-
cmins = self.hlines(mins, pmins, pmaxes, colors='r')
6861-
cbars = self.vlines(positions, mins, maxes, colors='r')
68626900

6863-
# Render medians
6864-
if showmedians:
6865-
cmedians = self.hlines(medians, pmins, pmaxes, colors='r')
68666901

68676902
# Reset hold
68686903
self.hold(holdStatus)

0 commit comments

Comments
 (0)