简体   繁体   English

Matplotlib的Axes3D绘图不直观吗?

[英]Is Matplotlib's Axes3D plotting not intuitive?

I want to make a 3D plot for these 100 points in X,Y and Z axes. 我想为X,Y和Z轴上的这100个点绘制3D图。 I have generated lists that I require for all 3 axes. 我已经生成了所有3轴所需的列表。 I assumed that this should be sufficient to plot a set of points in 3D. 我认为这足以在3D中绘制一组点。 However I do not understand the output. 但是我不明白输出。 I appreciate any kind of help in this regard. 我感谢在这方面的任何帮助。

################################################################
# 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? 编辑/更新:我不确定我的问题是代码本身还是理解3Dplots的方式,我是否应该使用网状网格来获取所需的图?

Which version of matplotlib do you have? 您有哪个版本的matplotlib? The documentation states that for matplotlib versions 1.0.0 and greater you should create a 3D axes as follows: 文档指出,对于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(fig)

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. 编辑在OP注释之后,似乎代码的结果与预期不符,而不是出现某种错误。 The following code is what I presume is intended 以下代码是我假定的目标

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. 以前只绘制了沿x=y一点线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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