Skip to content

Commit d7e7704

Browse files
committed
Shorten the implementation of violin().
Just some standard rewriting.
1 parent 4da4cf9 commit d7e7704

File tree

1 file changed

+33
-58
lines changed

1 file changed

+33
-58
lines changed

lib/matplotlib/axes/_axes.py

+33-58
Original file line numberDiff line numberDiff line change
@@ -8100,15 +8100,17 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
81008100
- ``cquantiles``: A `~.collections.LineCollection` instance created
81018101
to identify the quantiles values of each of the violin's
81028102
distribution.
8103-
81048103
"""
81058104

81068105
# Statistical quantities to be plotted on the violins
81078106
means = []
81088107
mins = []
81098108
maxes = []
81108109
medians = []
8111-
quantiles = np.asarray([])
8110+
quantiles = []
8111+
8112+
ppmins = [] # left end of quantiles lines
8113+
ppmaxs = [] # right end of quantiles lines
81128114

81138115
# Collections to be returned
81148116
artists = {}
@@ -8133,80 +8135,53 @@ def violin(self, vpstats, positions=None, vert=True, widths=0.5,
81338135
pmins = -0.25 * np.array(widths) + positions
81348136
pmaxes = 0.25 * np.array(widths) + positions
81358137

8138+
# Colors.
8139+
if rcParams['_internal.classic_mode']:
8140+
fillcolor = 'y'
8141+
linecolor = 'r'
8142+
else:
8143+
fillcolor = linecolor = self._get_lines.get_next_color()
8144+
81368145
# Check whether we are rendering vertically or horizontally
81378146
if vert:
81388147
fill = self.fill_betweenx
8139-
perp_lines = self.hlines
8140-
par_lines = self.vlines
8148+
perp_lines = functools.partial(self.hlines, colors=linecolor)
8149+
par_lines = functools.partial(self.vlines, colors=linecolor)
81418150
else:
81428151
fill = self.fill_between
8143-
perp_lines = self.vlines
8144-
par_lines = self.hlines
8145-
8146-
if rcParams['_internal.classic_mode']:
8147-
fillcolor = 'y'
8148-
edgecolor = 'r'
8149-
else:
8150-
fillcolor = edgecolor = self._get_lines.get_next_color()
8152+
perp_lines = functools.partial(self.vlines, colors=linecolor)
8153+
par_lines = functools.partial(self.hlines, colors=linecolor)
81518154

81528155
# Render violins
81538156
bodies = []
8154-
for stats, pos, width in zip(vpstats, positions, widths):
8155-
# The 0.5 factor reflects the fact that we plot from v-p to
8156-
# v+p
8157+
for stats, pos, width, cmin, cmax in zip(
8158+
vpstats, positions, widths, pmins, pmaxes):
8159+
# The 0.5 factor reflects the fact that we plot from v-p to v+p.
81578160
vals = np.array(stats['vals'])
81588161
vals = 0.5 * width * vals / vals.max()
8159-
bodies += [fill(stats['coords'],
8160-
-vals + pos,
8161-
vals + pos,
8162-
facecolor=fillcolor,
8163-
alpha=0.3)]
8162+
bodies += [fill(stats['coords'], -vals + pos, vals + pos,
8163+
facecolor=fillcolor, alpha=0.3)]
81648164
means.append(stats['mean'])
81658165
mins.append(stats['min'])
81668166
maxes.append(stats['max'])
81678167
medians.append(stats['median'])
81688168
q = stats.get('quantiles')
81698169
if q is not None:
8170-
# If exist key quantiles, assume it's a list of floats
8171-
quantiles = np.concatenate((quantiles, q))
8170+
quantiles.extend(q) # q is assumed to be a list of floats.
8171+
ppmins.extend([cmin] * len(q))
8172+
ppmaxs.extend([cmax] * len(q))
81728173
artists['bodies'] = bodies
81738174

8174-
# Render means
8175-
if showmeans:
8176-
artists['cmeans'] = perp_lines(means, pmins, pmaxes,
8177-
colors=edgecolor)
8178-
8179-
# Render extrema
8180-
if showextrema:
8181-
artists['cmaxes'] = perp_lines(maxes, pmins, pmaxes,
8182-
colors=edgecolor)
8183-
artists['cmins'] = perp_lines(mins, pmins, pmaxes,
8184-
colors=edgecolor)
8185-
artists['cbars'] = par_lines(positions, mins, maxes,
8186-
colors=edgecolor)
8187-
8188-
# Render medians
8189-
if showmedians:
8190-
artists['cmedians'] = perp_lines(medians,
8191-
pmins,
8192-
pmaxes,
8193-
colors=edgecolor)
8194-
8195-
# Render quantile values
8196-
if quantiles.size > 0:
8197-
# Recalculate ranges for statistics lines for quantiles.
8198-
# ppmins are the left end of quantiles lines
8199-
ppmins = np.asarray([])
8200-
# pmaxes are the right end of quantiles lines
8201-
ppmaxs = np.asarray([])
8202-
for stats, cmin, cmax in zip(vpstats, pmins, pmaxes):
8203-
q = stats.get('quantiles')
8204-
if q is not None:
8205-
ppmins = np.concatenate((ppmins, [cmin] * np.size(q)))
8206-
ppmaxs = np.concatenate((ppmaxs, [cmax] * np.size(q)))
8207-
# Start rendering
8208-
artists['cquantiles'] = perp_lines(quantiles, ppmins, ppmaxs,
8209-
colors=edgecolor)
8175+
if showmeans: # Render means
8176+
artists['cmeans'] = perp_lines(means, pmins, pmaxes)
8177+
if showextrema: # Render extrema
8178+
artists['cmaxes'] = perp_lines(maxes, pmins, pmaxes)
8179+
artists['cmins'] = perp_lines(mins, pmins, pmaxes)
8180+
artists['cbars'] = par_lines(positions, mins, maxes)
8181+
if showmedians: # Render medians
8182+
artists['cmedians'] = perp_lines(medians, pmins, pmaxes)
8183+
if quantiles: # Render quantiles
8184+
artists['cquantiles'] = perp_lines(quantiles, ppmins, ppmaxs)
82108185

82118186
return artists
82128187

0 commit comments

Comments
 (0)