0

Trying to do a 3d plot with matplotlib, but for some reason my code fails when i try to set xi,yi and keep getting the following message:

xi = np.linspace(min(x_mtx), max(x_mtx))
  File "C:\Python27\lib\site-packages\numpy\core\function_base.py", line 80, in linspace
    step = (stop-start)/float((num-1))
TypeError: unsupported operand type(s) for -: 'list' and 'list'

Code:

def plot_3D(self,x_mtx,y_mtx,z_mtx,title,xlabel,ylabel):

    fig = plt.figure()
    ax = fig.gca(projection='3d')


    x = x_mtx
    y = y_mtx
    z = z_mtx


    xi = np.linspace(min(x_mtx), max(x_mtx))
    yi = np.linspace(min(y_mtx), max(y_mtx))



    X, Y = np.meshgrid(xi, yi)
    Z = griddata(x, y, z, xi, yi)

    Z = np.nan_to_num(Z)

    surf = ax.plot_surface(X, Y, Z, rstride=3, cstride=1,  cmap=cm.jet,
                           linewidth=0, antialiased=True)

    ax.set_zlim3d(np.min(Z), np.max(Z))

    fig.colorbar(surf)

    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    plt.title(title)

    plt.show()

I am using the following data set:

x =[[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9],...,[[0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]]
y =[[1,2,3,4],...,[1,2,3,4]
z =[[1604.18997105,1537.61273892,1475.55679943,1372.35580231,1338.5212552,1205.65768444,1123.58398781,1011.84290322,859.696324611],[1032.18731228,996.573332541,948.61368911,912.983432776,881.29239958,798.381328007,750.773525511,679.725673182,586.014048166],[727.489743398,674.426010669,660.796225936,636.607836391,603.244223602,559.648437086,513.633091109,473.594466259,417.134921259],[511.067337872,482.096743673,471.899423715,448.898733469,436.745110773,392.610890968,362.940790577,330.484896223,290.875981749]]

1 Answer 1

2

This is because (presumably) x_mtx is a matrix, and so the in-built max returns a list containing the largest element in each row of x_mtx.

If you want to get the min/max values in x_mtx globally, use numpy's min/max instead, which returns the scalar minimum over the entire matrix, not just each row:

xi = np.linspace(np.min(x_mtx), np.max(x_mtx))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.