Python 从列表创建 3d 图
Python creating 3d plot from lists
我正在尝试根据以下列表制作 3D 图:
X轴:
S = [1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0]
Y轴:
T = [0.25,0.5,1,2,3,5]
Z轴:
Diff_025 = [-0.0, -0.0001, -0.0002, -0.0027, -0.0004, 0.0, -0.0056, 0.004, 0.0104]
Diff_05 = [-0.0, 0.0, -0.0021, -0.0044, -0.0028, -0.0131, 0.0024, 0.0049, 0.0005]
Diff_1 = [-0.0, -0.0002, -0.0026, 0.0041, 0.0058, -0.0061, -0.0054, 0.0086, -0.0056]
Diff_2 = [0.0001, -0.0077, -0.0054, -0.0018, 0.0009, -0.0153, -0.0072, 0.0056, 0.0066]
Diff_3 = [-0.0008, -0.0002, -0.0075, 0.0021, -0.0012, 0.0046, 0.0104, -0.0003, -0.002]
Diff_5 = [0.001, -0.0068, 0.003, 0.0072, 0.0093, 0.0066, -0.0086, 0.0034, -0.0073]
其中 Z 轴形成一个表面。但是,我找不到合适的方法来处理列表。
有没有人有我可以使用的代码片段?我正在寻找看起来像这样的东西:
我尝试了以下代码:
data= pd.DataFrame([[1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0],
[0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,5,5,5,5,5,5,5,5,5]
,[0.000000,0.000160,0.020047,0.179782,0.664449,1.543504,2.771955,4.280637,5.959681,0.000000,0.009424,0.127804,0.512490,1.226957,2.226444,3.484431,4.895013,6.406580,0.000800,0.084148,0.432837,1.090617,1.989930,3.056985,4.262359,5.564177,6.889929,0.018143,0.314030,0.948941,1.799778,2.785208,3.837540,4.968680,6.138531,7.317157,0.056033,0.537801,1.292654,2.210227,3.197653,4.243821,5.320167,6.398639,7.497133,0.151225,0.830068,1.704220,2.647585,3.627470,4.624528,5.622238,6.655008,7.669625]]).T
plt.contour(data.iloc[:,0].values.reshape(9,6), data.iloc[:,1].values.reshape(9,6), data.iloc[:,2].values.reshape(9,6))
plt.grid()
plt.legend()
plt.show()
这导致了下图
您需要做的就是使用 np.meshgrid()
格式化列表
import numpy as np
import matplotlib.pyplot as plt
s = [1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0]
t = [0.25,0.5,1,2,3,5]
S, T = np.meshgrid(s, t)
Z = [Diff_025, Diff_05, Diff_1, Diff_2, Diff_3, Diff_5]
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(S, T, Diff_mat, 100, cmap='binary')
ax.set_xlabel('S')
ax.set_ylabel('T')
ax.set_zlabel('Difference')
3d plot
我正在尝试根据以下列表制作 3D 图:
X轴:
S = [1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0]
Y轴:
T = [0.25,0.5,1,2,3,5]
Z轴:
Diff_025 = [-0.0, -0.0001, -0.0002, -0.0027, -0.0004, 0.0, -0.0056, 0.004, 0.0104]
Diff_05 = [-0.0, 0.0, -0.0021, -0.0044, -0.0028, -0.0131, 0.0024, 0.0049, 0.0005]
Diff_1 = [-0.0, -0.0002, -0.0026, 0.0041, 0.0058, -0.0061, -0.0054, 0.0086, -0.0056]
Diff_2 = [0.0001, -0.0077, -0.0054, -0.0018, 0.0009, -0.0153, -0.0072, 0.0056, 0.0066]
Diff_3 = [-0.0008, -0.0002, -0.0075, 0.0021, -0.0012, 0.0046, 0.0104, -0.0003, -0.002]
Diff_5 = [0.001, -0.0068, 0.003, 0.0072, 0.0093, 0.0066, -0.0086, 0.0034, -0.0073]
其中 Z 轴形成一个表面。但是,我找不到合适的方法来处理列表。 有没有人有我可以使用的代码片段?我正在寻找看起来像这样的东西:
我尝试了以下代码:
data= pd.DataFrame([[1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0,1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0],
[0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,5,5,5,5,5,5,5,5,5]
,[0.000000,0.000160,0.020047,0.179782,0.664449,1.543504,2.771955,4.280637,5.959681,0.000000,0.009424,0.127804,0.512490,1.226957,2.226444,3.484431,4.895013,6.406580,0.000800,0.084148,0.432837,1.090617,1.989930,3.056985,4.262359,5.564177,6.889929,0.018143,0.314030,0.948941,1.799778,2.785208,3.837540,4.968680,6.138531,7.317157,0.056033,0.537801,1.292654,2.210227,3.197653,4.243821,5.320167,6.398639,7.497133,0.151225,0.830068,1.704220,2.647585,3.627470,4.624528,5.622238,6.655008,7.669625]]).T
plt.contour(data.iloc[:,0].values.reshape(9,6), data.iloc[:,1].values.reshape(9,6), data.iloc[:,2].values.reshape(9,6))
plt.grid()
plt.legend()
plt.show()
这导致了下图
您需要做的就是使用 np.meshgrid()
import numpy as np
import matplotlib.pyplot as plt
s = [1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0]
t = [0.25,0.5,1,2,3,5]
S, T = np.meshgrid(s, t)
Z = [Diff_025, Diff_05, Diff_1, Diff_2, Diff_3, Diff_5]
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(S, T, Diff_mat, 100, cmap='binary')
ax.set_xlabel('S')
ax.set_ylabel('T')
ax.set_zlabel('Difference')
3d plot