Skip to content

Loading…

Added broadcasting support in some mplot3d methods #1074

Merged
merged 1 commit into from

2 participants

@WeatherGod
Matplotlib Developers member

plot_surface and plot_wireframe gain the ability to allow for broadcastable inputs for X, Y, and Z.

@pelson
Matplotlib Developers member

So the inputs x y and z must all be 3d right?

>>> x = np.arange(3)
>>> y = np.arange(4)
>>> z = np.arange(5)

>>> np.broadcast_arrays(x, y, z)
ValueError: shape mismatch: two or more arrays have incompatible dimensions on axis 0.

>>> x = np.arange(3)
>>> y = np.arange(4)
>>> z = np.arange(5)
>>> x.shape = (-1, 1, 1)
>>> y.shape = (1, -1, 1)
>>> z.shape = (1, 1, -1)

>>> X, Y, Z = np.broadcast_arrays(x, y, z)
>>> print Y.shape
(3, 4, 5)
>>> print y.shape
(1, 4, 1)
@WeatherGod
Matplotlib Developers member
@WeatherGod WeatherGod merged commit 2798e47 into matplotlib:master
@WeatherGod WeatherGod deleted the WeatherGod:mplot3d/input_broadcast2 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 12, 2012
  1. @WeatherGod
Showing with 3 additions and 15 deletions.
  1. +3 −15 lib/mpl_toolkits/mplot3d/axes3d.py
View
18 lib/mpl_toolkits/mplot3d/axes3d.py
@@ -1352,15 +1352,9 @@ def plot_surface(self, X, Y, Z, *args, **kwargs):
had_data = self.has_data()
Z = np.atleast_2d(Z)
- rows, cols = Z.shape
# TODO: Support masked arrays
- X = np.asarray(X)
- Y = np.asarray(Y)
- # Force X and Y to take the same shape.
- # If they can not be fitted to that shape,
- # then an exception is automatically thrown.
- X.shape = (rows, cols)
- Y.shape = (rows, cols)
+ X, Y, Z = np.broadcast_arrays(X, Y, Z)
+ rows, cols = Z.shape
rstride = kwargs.pop('rstride', 10)
cstride = kwargs.pop('cstride', 10)
@@ -1526,14 +1520,8 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
had_data = self.has_data()
Z = np.atleast_2d(Z)
# FIXME: Support masked arrays
- X = np.asarray(X)
- Y = np.asarray(Y)
+ X, Y, Z = np.broadcast_arrays(X, Y, Z)
rows, cols = Z.shape
- # Force X and Y to take the same shape.
- # If they can not be fitted to that shape,
- # then an exception is automatically thrown.
- X.shape = (rows, cols)
- Y.shape = (rows, cols)
# We want two sets of lines, one running along the "rows" of
# Z and another set of lines running along the "columns" of Z.
Something went wrong with that request. Please try again.