I am having trouble getting np.piecewise
to work for multiple dimensional plotting due to broadcast errors.
Does anyone have any manner to get around this?
Here is what I have in a simplified executable script:
import numpy as np
from pylab import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d, Axes3D
num_steps = 100
x_arr = np.linspace(0,100, num_steps)
y_arr = np.linspace(0,20, num_steps)
def zfunc(x, y):
return np.piecewise(x, [x>=500, x<500], [x, -x])
x,y = np.meshgrid(x_arr, y_arr)
z =zfunc(x,y)
fig=plt.figure()
ax=fig.subplot(1,1,1,projection='3d')
p = x.plot_surface(x,y,z,rstride=1,cstride=1,cmap=cm.coolwarm,linewidth=0,antialiased=False)
plt.show()
Which gives the error:
return np.piecewise(x, [x>=500, x<500], [x, -x])
File "C:\Python27\lib\site-packages\numpy\lib\function_base.py", line 716, in piecewise
y[condlist[k]] = item
ValueError: array is not broadcastable to correct shape