Matplotlib的Axes3D绘图是不是不直观?

时间:2022-01-23 23:40:35

I want to make a 3D plot for these 100 points in X,Y and Z axes. I have generated lists that I require for all 3 axes. I assumed that this should be sufficient to plot a set of points in 3D. However I do not understand the output. I appreciate any kind of help in this regard.

我想把这100个X,Y, Z轴的点画出来。我已经生成了我需要的三个轴的列表。我假设这个足够在3D中绘制一组点。但是我不理解输出。我感谢在这方面的任何帮助。

################################################################
# problem : f(x) = (e**(-(y**2)))*cos(3*x)+(e**(x**2))*cos(3*y)
################################################################

from mpl_toolkits.mplot3d import Axes3D
import math
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax=Axes3D(fig)
x = np.arange(-5,5,1)
y = np.arange(-5,5,1)
X = []
Y = []
Z=[]
for i in range(len(x)):
        for j in range(len(y)):
            z=(np.exp(-(y[j]**2))*np.cos(3*x[i]))+(np.exp(x[i]**2)*np.cos(3*y[j]))
        Z.append(z)
        X.append(x[i])
        Y.append(y[j])
ax.plot(X,Y,Z,'o')
plt.show()

edit/update: I am not sure if my problem is with the code itself or the way i understand 3Dplots, Should I use meshgrids to get a plot that i expect?

编辑/更新:我不确定我的问题是代码本身还是我理解3dplot的方式,我应该使用网格来得到我期望的图吗?

1 个解决方案

#1


4  

Which version of matplotlib do you have? The documentation states that for matplotlib versions 1.0.0 and greater you should create a 3D axes as follows:

你们有哪个版本的matplotlib ?文件指出,对于matplotlib版本1.0.0和更高版本,您应该创建如下的3D轴:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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

rather than the ax = Axes3D(fig) used in previous versions.

而不是以前版本中使用的ax = Axes3D(图)。

Edit Following the OPs comment it seems that is the result of the code is not as expected, rather than there being some sort of error. The following code is what I presume is intended

在OPs评论之后进行编辑,这似乎是代码的结果不像预期的那样,而不是存在某种错误。下面的代码是我想要的。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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

x, y = np.meshgrid(np.linspace(-5., 5., 100), np.linspace(-5., 5., 100))

def zfunc(x, y):
    return np.exp(-(y**2)) * np.cos(3.*x) + np.exp(x**2) * np.cos(3.*y)

z = zfunc(x, y)

ax.plot_surface(x, y, z)

plt.show()

In the above code a two dimensional mesh is created (missing from the original post) and the function is calculated as a function of these two variables and plotted as a surface. Previously just a line of points running along x=y was being plotting.

在上面的代码中,创建了一个二维网格(从原始post中缺失),并将函数作为这两个变量的函数进行计算,并绘制为一个曲面。之前,只有沿x=y方向的点在作图。

#1


4  

Which version of matplotlib do you have? The documentation states that for matplotlib versions 1.0.0 and greater you should create a 3D axes as follows:

你们有哪个版本的matplotlib ?文件指出,对于matplotlib版本1.0.0和更高版本,您应该创建如下的3D轴:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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

rather than the ax = Axes3D(fig) used in previous versions.

而不是以前版本中使用的ax = Axes3D(图)。

Edit Following the OPs comment it seems that is the result of the code is not as expected, rather than there being some sort of error. The following code is what I presume is intended

在OPs评论之后进行编辑,这似乎是代码的结果不像预期的那样,而不是存在某种错误。下面的代码是我想要的。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

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

x, y = np.meshgrid(np.linspace(-5., 5., 100), np.linspace(-5., 5., 100))

def zfunc(x, y):
    return np.exp(-(y**2)) * np.cos(3.*x) + np.exp(x**2) * np.cos(3.*y)

z = zfunc(x, y)

ax.plot_surface(x, y, z)

plt.show()

In the above code a two dimensional mesh is created (missing from the original post) and the function is calculated as a function of these two variables and plotted as a surface. Previously just a line of points running along x=y was being plotting.

在上面的代码中,创建了一个二维网格(从原始post中缺失),并将函数作为这两个变量的函数进行计算,并绘制为一个曲面。之前,只有沿x=y方向的点在作图。