如何使用 matplotlib 制作 3d 曲面图,Z 不是由 func 计算的? [英] How to make a 3d surface plot with matplotlib, Z is not calculated by func?

查看:80
本文介绍了如何使用 matplotlib 制作 3d 曲面图,Z 不是由 func 计算的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 行数据分别用作 X、Z、Y.但是Z数据是直接从csv文件中读取的,所以不是(x,y)的函数.

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

数据显示:

[[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]]

我提到了这个答案:给定 3d 点绘制 3d 表面的最简单方法

我的代码是:

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

如何制作带有曲面的 3d 绘图?它总是告诉我:

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.

如果我使用 np.meshgrid(x,y),则会出现错误:

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

 ValueError: Argument Z must be 2-dimensional" 

我该如何解决这个问题?

How can I fix this?

提前致谢!

推荐答案

你的代码崩溃的原因是你的输入数据退化了.plot_trisurf 函数试图构建一个 Delaunay 三角剖分(在您的代码中,这些是 Sparsity1、agents1).但是,agents1 拥有相同的值,因此您尝试对一条线上的一组点进行三角测量(单数/退化配置),这会调用 singular input data 错误来自 QHull,scipy 使用的底层库.

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.

如果您使用 agents1 作为您的 Z 值(而不是您的 Y 值)调用该函数,您将获得一个曲面图没有错误.

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

或者,您可以构建自己的三角形网格并将其传递给函数,就像在 this stackoverflow answer 中所做的那样.

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

这篇关于如何使用 matplotlib 制作 3d 曲面图,Z 不是由 func 计算的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆