Skip to content

Commit f08ecc0

Browse files
committed
Switch stem3d from using zdir to orientation.
This is hopefully a less confusing way to define the 3D stem plot in different directions, as the x/y/z always mean the same point, while only the stem direction changes.
1 parent 8856736 commit f08ecc0

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,7 +3276,7 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
32763276
return mtransforms.Bbox.union(batch)
32773277

32783278
def stem(self, x, y, z, *, linefmt='C0-', markerfmt='C0o', basefmt='C3-',
3279-
bottom=0, label=None, zdir='z'):
3279+
bottom=0, label=None, orientation='z'):
32803280
"""
32813281
Create a 3D stem plot.
32823282
@@ -3287,11 +3287,11 @@ def stem(self, x, y, z, *, linefmt='C0-', markerfmt='C0o', basefmt='C3-',
32873287
Parameters
32883288
----------
32893289
x, y, z : array-like
3290-
The positions of the heads of the stems. The baseline is defined by
3291-
two of these positions and *bottom*, and stems are drawn from
3292-
*bottom* to the third position. By default, the *x* and *y*
3290+
The positions of the heads of the stems. The stems are drawn along
3291+
the *orientation*-direction from the baseline at *bottom* (in the
3292+
*orientation*-coordinate) to the heads. By default, the *x* and *y*
32933293
positions are used for the baseline and *z* for the head position,
3294-
but this can be changed by *zdir*.
3294+
but this can be changed by *orientation*.
32953295
32963296
linefmt : str, default: 'C0-'
32973297
A string defining the properties of the vertical lines. Usually,
@@ -3318,12 +3318,12 @@ def stem(self, x, y, z, *, linefmt='C0-', markerfmt='C0o', basefmt='C3-',
33183318
A format string defining the properties of the baseline.
33193319
33203320
bottom : float, default: 0
3321-
The x/y/z-position of the baseline (depending on *zdir*).
3321+
The position of the baseline, in *orientation*-coordinates.
33223322
33233323
label : str, default: None
33243324
The label to use for the stems in legends.
33253325
3326-
zdir : {'x', 'y', 'z'}, default: 'z'
3326+
orientation : {'x', 'y', 'z'}, default: 'z'
33273327
The direction along which stems are drawn.
33283328
33293329
Returns
@@ -3341,37 +3341,50 @@ def stem(self, x, y, z, *, linefmt='C0-', markerfmt='C0o', basefmt='C3-',
33413341

33423342
had_data = self.has_data()
33433343

3344-
jx, jy, jz = art3d.juggle_axes(x, y, z, zdir)
3344+
cbook._check_in_list(['x', 'y', 'z'], orientation=orientation)
33453345

3346-
# Plot the baseline in the appropriate plane.
3347-
baseline, = self.plot(x, y, basefmt, zs=bottom, zdir=zdir,
3348-
label='_nolegend_')
3346+
xlim = (np.min(x), np.max(x))
3347+
ylim = (np.min(y), np.max(y))
3348+
zlim = (np.min(z), np.max(z))
33493349

3350-
# Plot the stemlines based on the value of zdir.
3351-
if zdir[-1:] == 'x':
3350+
# Determine the appropriate plane for the baseline and the direction of
3351+
# stemlines based on the value of orientation.
3352+
if orientation == 'x':
3353+
basex, basexlim = y, ylim
3354+
basey, baseylim = z, zlim
33523355
lines = [[(bottom, thisy, thisz), (thisx, thisy, thisz)]
3353-
for thisx, thisy, thisz in zip(jx, jy, jz)]
3354-
elif zdir[-1:] == 'y':
3356+
for thisx, thisy, thisz in zip(x, y, z)]
3357+
elif orientation == 'y':
3358+
basex, basexlim = x, xlim
3359+
basey, baseylim = z, zlim
33553360
lines = [[(thisx, bottom, thisz), (thisx, thisy, thisz)]
3356-
for thisx, thisy, thisz in zip(jx, jy, jz)]
3361+
for thisx, thisy, thisz in zip(x, y, z)]
33573362
else:
3363+
basex, basexlim = x, xlim
3364+
basey, baseylim = y, ylim
33583365
lines = [[(thisx, thisy, bottom), (thisx, thisy, thisz)]
3359-
for thisx, thisy, thisz in zip(jx, jy, jz)]
3366+
for thisx, thisy, thisz in zip(x, y, z)]
3367+
3368+
# Determine style for stem lines.
33603369
linestyle, linemarker, linecolor = _process_plot_format(linefmt)
33613370
if linestyle is None:
33623371
linestyle = rcParams['lines.linestyle']
3372+
3373+
# Plot everything in required order.
3374+
baseline, = self.plot(basex, basey, basefmt, zs=bottom,
3375+
zdir=orientation, label='_nolegend_')
33633376
stemlines = art3d.Line3DCollection(
33643377
lines, linestyles=linestyle, colors=linecolor, label='_nolegend_')
33653378
self.add_collection(stemlines)
3366-
3367-
markerline, = self.plot(x, y, z, markerfmt, zdir=zdir,
3368-
label='_nolegend_')
3379+
markerline, = self.plot(x, y, z, markerfmt, label='_nolegend_')
33693380

33703381
stem_container = StemContainer((markerline, stemlines, baseline),
33713382
label=label)
33723383
self.add_container(stem_container)
33733384

3374-
self.auto_scale_xyz(jx, jy, jz, had_data)
3385+
jx, jy, jz = art3d.juggle_axes(basexlim, baseylim, [bottom, bottom],
3386+
orientation)
3387+
self.auto_scale_xyz([*jx, *xlim], [*jy, *ylim], [*jz, *zlim], had_data)
33753388

33763389
return stem_container
33773390

0 commit comments

Comments
 (0)