使用Python中的Matplotlib进行3D绘图并继续获取TypeError:不支持的操作数类型 - :'list'和'list'

时间:2021-08-16 06:00:53

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:

尝试使用matplotlib进行3d绘图,但由于某些原因,当我尝试设置xi,yi并继续获取以下消息时,我的代码失败:

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 个解决方案

#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.

这是因为(推测)x_mtx是一个矩阵,因此内置的max返回一个包含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:

如果要全局获取x_mtx中的最小值/最大值,请使用numpy的min / max,它将返回整个矩阵的标量最小值,而不仅仅是每行:

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

#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.

这是因为(推测)x_mtx是一个矩阵,因此内置的max返回一个包含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:

如果要全局获取x_mtx中的最小值/最大值,请使用numpy的min / max,它将返回整个矩阵的标量最小值,而不仅仅是每行:

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