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

Skip to content

Commit bb973bb

Browse files
srihitha09Jessy Liangsamriddhikaushik
authored andcommitted
Implement 3D stem plots.
* Implemented 3D stem plots. * refactor code to work with juggle_axes() * Fixed pep8 * Changed file names * update code, tests, and demos to work with zdir param and stem() * fix pep8 for test file * clean up baseline images Co-authored-by: Jessy Liang <[email protected]> Co-authored-by: Samriddhi Kaushik <[email protected]>
1 parent 09ab2c2 commit bb973bb

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

examples/mplot3d/stem3d_demo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from mpl_toolkits.mplot3d import Axes3D
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
fig = plt.figure()
6+
ax = fig.gca(projection='3d')
7+
theta = np.linspace(0, 2*np.pi)
8+
x = np.cos(theta)
9+
y = np.sin(theta)
10+
z = theta
11+
markerline, stemlines, baseline = ax.stem(x, y, z)
12+
13+
plt.show()

examples/mplot3d/stem3d_demo2.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from mpl_toolkits.mplot3d import Axes3D
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
5+
fig = plt.figure()
6+
ax = fig.gca(projection='3d')
7+
x = np.linspace(-np.pi/2, np.pi/2, 40)
8+
y = [1]*len(x)
9+
z = np.cos(x)
10+
markerline, stemlines, baseline = ax.stem(x, y, z, '-.', zdir='-y')
11+
plt.setp(markerline, 'markerfacecolor', 'b')
12+
plt.setp(baseline, 'color', 'r', 'linewidth', 1)
13+
14+
plt.show()

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,6 +3288,98 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
32883288
batch.append(axis_bb)
32893289
return mtransforms.Bbox.union(batch)
32903290

3291+
def stem(self, *args, **kwargs):
3292+
"""
3293+
Create a 3D stem plot.
3294+
3295+
Call signatures::
3296+
stem3(x, y, z, linefmt='b-', markerfmt='bo', basefmt='r-',
3297+
zdir={'x'|'y'|'z'|'-x'|'-y'})
3298+
3299+
By default, stem plots vertical lines (using *linefmt*) in the z
3300+
direction at each *x* and *y* location from the baseline to *z*, and
3301+
places a marker there using *markerfmt*. By default, the baseline at
3302+
the xy-plane is plotted using *basefmt*.
3303+
3304+
*x*, *y, and *z* values are required and must be arrays of the same
3305+
length.
3306+
3307+
If no zdir value is provided, then it is set to default and no rotation
3308+
occurs.
3309+
3310+
Return value is a tuple (*markerline*, *stemlines*, *baseline*).
3311+
3312+
.. seealso::
3313+
This `document
3314+
<http://www.mathworks.com/help/techdoc/ref/stem3.html>`_ for
3315+
details.
3316+
3317+
**Example:**
3318+
.. plot:: /mplot3d/stem3d_demo.py
3319+
"""
3320+
3321+
from matplotlib.container import StemContainer
3322+
3323+
had_data = self.has_data()
3324+
3325+
# Extract the required values
3326+
x, y, z = [np.asarray(i) for i in args[:3]]
3327+
args = args[3:]
3328+
3329+
# Popping some defaults
3330+
try:
3331+
linefmt = kwargs.pop('linefmt', args[0])
3332+
except IndexError:
3333+
linefmt = kwargs.pop('linefmt', 'b-')
3334+
try:
3335+
markerfmt = kwargs.pop('markerfmt', args[1])
3336+
except IndexError:
3337+
markerfmt = kwargs.pop('markerfmt', 'bo')
3338+
try:
3339+
basefmt = kwargs.pop('basefmt', args[2])
3340+
except IndexError:
3341+
basefmt = kwargs.pop('basefmt', 'r-')
3342+
try:
3343+
zdir = kwargs.pop('zdir', args[3])
3344+
except IndexError:
3345+
zdir = kwargs.pop('zdir', 'z')
3346+
3347+
bottom = kwargs.pop('bottom', None)
3348+
label = kwargs.pop('label', None)
3349+
3350+
if bottom is None:
3351+
bottom = 0
3352+
3353+
stemlines = []
3354+
jx, jy, jz = art3d.juggle_axes(x, y, z, zdir)
3355+
3356+
# plot the baseline in the appropriate plane
3357+
for i in range(len(x)-1):
3358+
baseline, = Axes.plot(self, [x[i], x[i+1]], [y[i], y[i+1]],
3359+
basefmt, label="_nolegend_")
3360+
art3d.line_2d_to_3d(baseline, [bottom, bottom], zdir)
3361+
3362+
# plot the stemlines based on the value of rotate
3363+
for thisx, thisy, thisz in zip(x, y, z):
3364+
l, = Axes.plot(self, [thisx, thisx], [thisy, thisy], linefmt,
3365+
label="_nolegend_")
3366+
art3d.line_2d_to_3d(l, [bottom, thisz], zdir)
3367+
3368+
stemlines.append(l)
3369+
3370+
markerline, = Axes.plot(self, x, y, markerfmt, label="_nolegend_")
3371+
art3d.line_2d_to_3d(markerline, z, zdir)
3372+
3373+
stem_container = StemContainer((markerline, stemlines, baseline),
3374+
label=label)
3375+
self.add_container(stem_container)
3376+
3377+
self.auto_scale_xyz(jx, jy, jz, had_data)
3378+
3379+
return stem_container
3380+
3381+
stem3D = stem
3382+
32913383
docstring.interpd.update(Axes3D_kwdoc=artist.kwdoc(Axes3D))
32923384
docstring.dedent_interpd(Axes3D.__init__)
32933385

0 commit comments

Comments
 (0)