创建一个制作3D图形的函数

时间:2015-02-18 13:19:47

标签: python arrays python-2.7 graph

我正在创建一个函数,当给定两个值数组时,该函数从这两个数组计算第三个数组,并绘制三个变量的图形。值数组基本上是随机的。

我所拥有的是:(其中x和y是相等大小的数组)

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)


def f(x,y):
    x = np.array(x)
    y = np.array(y)*np.pi
    t = g(x,y) #g(x,y) is a previously created function that takes one value of x 
               #and one of y and gives a number, hence this function should give
               #an array of numbers with the same index as x and y.
    c = np.array((x,y)) #just leaving this in case the surface requires a 2D array


    ax.scatter(???) #This is where I'm confused
return plt.show()

问题是,我是一个完全的初学者,所以在阅读有关如何创建这些曲面和曲线的参数时,我不明白要写什么。另外,我不知道什么类型的情节最能显示x,y和t之间的关系。我在线框图,曲面图,三面图和散点图之间徘徊。

1 个答案:

答案 0 :(得分:1)

您需要查看本教程:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

它包含您提到的所有图表的示例(可下载源代码)。使用它的最终选择必须是你的...完全主观。

作为一个例子,你可以写的是这样的(最小的工作示例)......在这种情况下是一个三维散点图。

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, t)
plt.show()