Axes3D绘制3d图不出图解决办法【Python】

运行下面一段代码​:

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D#这里设函数为y=3x+2x_data = [1.0,2.0,3.0]y_data = [5.0,8.0,11.0]def forward(x):    return x * w + bdef loss(x,y):    y_pred = forward(x)    return (y_pred-y)*(y_pred-y)mse_list = []W=np.arange(0.0,4.1,0.1)B=np.arange(0.0,4.1,0.1)[w,b]=np.meshgrid(W,B)l_sum = 0for x_val, y_val in zip(x_data, y_data):    y_pred_val = forward(x_val)    print(y_pred_val)    loss_val = loss(x_val, y_val)    l_sum += loss_valfig = plt.figure()ax = Axes3D(fig)ax.plot_surface(w, b, l_sum/3)plt.show(block=True)

可能出现的结果是:(版本问题导致)

Axes3D绘制3d图不出图解决办法【Python】_第1张图片

​解决办法:

添加一条代码:

fig.add_axes(ax)

​全部完整代码如下:

import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3D#这里设函数为y=3x+2x_data = [1.0,2.0,3.0]y_data = [5.0,8.0,11.0]def forward(x):    return x * w + bdef loss(x,y):    y_pred = forward(x)    return (y_pred-y)*(y_pred-y)mse_list = []W=np.arange(0.0,4.1,0.1)B=np.arange(0.0,4.1,0.1)[w,b]=np.meshgrid(W,B)l_sum = 0for x_val, y_val in zip(x_data, y_data):    y_pred_val = forward(x_val)    print(y_pred_val)    loss_val = loss(x_val, y_val)    l_sum += loss_valfig = plt.figure()ax = Axes3D(fig)fig.add_axes(ax)ax.plot_surface(w, b, l_sum/3)plt.show(block=True)

Axes3D绘制3d图不出图解决办法【Python】_第2张图片

ok.

你可能感兴趣的:(pyhton,3d,python,开发语言)