plot_surface 的数组应该如何构建? [英] How should arrays for plot_surface be built?

查看:37
本文介绍了plot_surface 的数组应该如何构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何构建用于plot_surface(在Axes3d中)的数组.我试图构建一个简单的表面处理这些数组的数据:

I'm trying to understand how to build arrays for use in plot_surface (in Axes3d). I tried to build a simple surface manipulating data of those arrays:

In [106]: x
Out[106]: 
array([[0, 0],
       [0, 1],
       [0, 0]])

In [107]: y
Out[107]: 
array([[0, 0],
       [1, 1],
       [0, 0]])

In [108]: z
Out[108]: 
array([[0, 0],
       [1, 1],
       [2, 2]])

但是我无法弄清楚它们是如何解释的-例如,在我的绘图中z = 2中没有任何内容.任何人都请确切说明将取哪些值作为指向点,取哪些值作为线并最终取曲面.

But I can't figure out how they are interpreted - for example there is nothing in z=2 on my plot. Anybody please explain exactly which values will be taken to make point, which for line and finally surface.

例如,我想构建一个与线点连接的表面:[0,0,0]-> [1,1,1]-> [0,0,2][0,0,0]->[1,-1,1]->[0,0,2]以及这些线之间的表面.plot_surface 的数组应该是什么样的?

For example I would like to build a surface that would connect with lines points: [0,0,0]->[1,1,1]->[0,0,2] [0,0,0]->[1,-1,1]->[0,0,2] and a surface between those lines. What should arrays for plot_surface look like to get something like this?

推荐答案

了解 plot_surface 中的网格如何工作并不容易.因此,首先我将给出一个一般性的解释,然后再解释如何根据您的情况转换数据.如果您有一个 N x个值的数组和一个 M 个y值的数组,则需要创建两个维度为(M,N) 每个.幸运的是 numpy.meshgrid 将有所帮助.使困惑?看一个例子:

Understanding how the grids in plot_surface work is not easy. So first I'll give a general explanation, and then I'll explain how to convert the data in your case. If you have an array of N x values and an array of M y values, you need to create two grids of x and y values of dimension (M,N) each. Fortunately numpy.meshgrid will help. Confused? See an example:

x = np.arange(3)
y=np.arange(1,5)
X, Y = np.meshgrid(x,y)

元素(x [i],y [j])作为(X [j,i],Y [j,i])访问.而它的Z值当然是Z[j,i],你也需要定义.

The element (x[i], y[j]) is accessed as (X[j,i], Y[j,i]). And its Z value is, of course, Z[j,i], which you also need to define.

话虽如此,您的数据确实按预期在(0,0,2)中产生了曲面的一个点.事实上,在那个位置有两个点,来自坐标索引(0,0,0)(1,1,1).

Having said that, your data does produce a point of the surface in (0,0,2), as expected. In fact, there are two points at that position, coming from coordinate indices (0,0,0) and (1,1,1).

我附上绘制数组的结果:

I attach the result of plotting your arrays with:

fig = plt.figure()
ax=fig.add_subplot(1,1,1, projection='3d')
surf=ax.plot_surface(X, Y, Z)

这篇关于plot_surface 的数组应该如何构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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