Home > Net >  Python creating 3d plot from lists
Python creating 3d plot from lists

Time:05-23

I am trying to make a 3D plot from the following lists:

X-axis:

S = [1.0, 3.0, 5.0, 7.0, 9.0,11.0,13.0,15.0,17.0]

Y-axis:

T = [0.25,0.5,1,2,3,5]

Z-axis:

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]

Where the Z-axis forms a surface. However, I can not find a proper method to do this with lists. Does anyone have a code snippet I can use for this? I am looking for something that looks like this:

enter image description here

I tried the following code:

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()

which resulted in the following graph enter image description here

CodePudding user response:

All you need to do is to format the lists using 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

  • Related