简体   繁体   中英

How to make a 3d surface plot with matplotlib, Z is not calculated by func?

I have 3 rows data used as X,Z, Y separately. But Z data is directly read from a csv file, so it's not a function of (x,y).

The data shows:

[[0.00000000e+00 1.00000000e-01 2.00000000e-01 3.00000000e-01
  3.09958506e-01 3.20000000e-01 3.25000000e-01 3.50000000e-01
  3.75000000e-01 4.00000000e-01 5.00000000e-01 6.00000000e-01
  7.00000000e-01]
 [1.31722083e+06 1.31722083e+06 1.31722083e+06 1.31722089e+06
  1.31722121e+06 1.31722083e+06 1.31722098e+06 1.31722134e+06
  1.31722101e+06 1.31722083e+06 1.31738292e+06 3.92708000e+12
  7.93453000e+12]
 [1.42000000e+02 1.42000000e+02 1.42000000e+02 1.42000000e+02
  1.42000000e+02 1.42000000e+02 1.42000000e+02 1.42000000e+02
  1.42000000e+02 1.42000000e+02 1.42000000e+02 1.42000000e+02
  1.42000000e+02]]

I referred this answer: Simplest way to plot 3d surface given 3d points

And my code is:

mat = pd.read_csv('results2.csv', header=None,sep=',').values
mat = mat[0:3,:13]
#print(mat)

Sparsity1 = mat[0,:]
agents1 = mat[2,:]
optimal1 = mat[1,:]

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax = Axes3D(fig)
#ax.plot_surface(Sparsity1,agents1, optimal1, rstride=1, cstride=1, cmap=cm.viridis)
ax.plot_trisurf(Sparsity1, agents1, optimal1, linewidth=0, antialiased=False)

surf = ax.plot_trisurf(Sparsity1, agents1, optimal1, cmap=cm.jet, linewidth=0)
fig.colorbar(surf)
plt.show()

How can I make 3d plot with surface? It always tells me:

RuntimeError: Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2); use python verbose option (-v) to see original qhull error.

If I use np.meshgrid(x,y), then I got the error:

 ValueError: Argument Z must be 2-dimensional” 

How can I fix this?

Thanks in advance!

The reason your code crashes is that your input data is degenerate. The plot_trisurf function tries to construct a Delaunay triangulation from the X , Y parameters of the function (in your code these are Sparsity1, agents1 ). However, agents1 holds identical values so you are trying to triangulate a set of points on a line (a singular/degenerate configuration), which invokes the singular input data error from QHull , the underlying library that scipy uses.

If you call the function with the agents1 as your Z values (instead of as your Y values) you will get a surface plot without the error.

ax.plot_trisurf(Sparsity1, optimal1, agents1, linewidth=0, antialiased=False) .

Alternatively you can construct your own triangular mesh and pass it to the function, like what was done in this stackoverflow answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM