简体   繁体   English

3D表面图,其中z是采用由x和y形成的向量的函数

[英]3D Surface Plot where z is a function that takes a vector formed from x and y

I am trying to produce a 3D surface plot where X and Y are values between -50 and 50, and Z is calculated by a function depending on X and Y. 我正在尝试生成3D表面图,其中X和Y的值在-50到50之间,而Z是通过取决于X和Y的函数计算的。

This function takes a vector as a parameter in the form of an np array. 此函数以np数组的形式将向量作为参数。 The vector's first row is a value from X and the second a value from Y. All combinations of X and Y should produce a Z value, hence the meshgrid. 向量的第一行是X的值,第二行是Y的值。X和Y的所有组合都应产生Z值,因此是网格。

Here is my implementation, for ZI am currently creating a vector where the first row is the entire dataset of X, and the second the entire dataset of Y. This is of course incorrect. 这是我的实现,因为ZI当前正在创建一个向量,其中第一行是X的整个数据集,第二行是Y的整个数据集。这当然是不正确的。

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import matplotlib.pyplot as plt

def myFunction(v):
    return v.dot(np.array([1, 2]))

fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.linspace(-50,50, 100)
Y = np.linspace(-50,50, 100)
X, Y = np.meshgrid(X, Y)
Z = myFunction(np.array([X, Y])) # <-- Here is the problem

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.Greens,
                       linewidth=0, antialiased=False)


ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

I hope I have made sense, Thanks 我希望我有道理,谢谢

You would probably like to supply an array with all x values in the frst column and all y values in the second column to the function. 您可能想为函数提供一个数组,其中在frst列中具有所有x值,在第二列中具有所有y值。 That would ensure to have the dimensions match for the dot product. 这样可以确保点产品的尺寸匹配。 The result can then be reshaped to the shape of the mesh. 然后可以将结果重塑为网格的形状。

Z = myFunction(np.array([X.flatten(), Y.flatten()]).T).reshape(X.shape)

Complete example: 完整的例子:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import matplotlib.pyplot as plt

def myFunction(v):
    return v.dot(np.array([1, 2]))

fig = plt.figure()
ax = fig.gca(projection='3d')

# Make data.
X = np.linspace(-50,50, 100)
Y = np.linspace(-50,50, 100)
X, Y = np.meshgrid(X, Y)
Z = myFunction(np.array([X.flatten(), Y.flatten()]).T).reshape(X.shape)

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, cmap=cm.Greens,
                       linewidth=0, antialiased=False)


ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

在此处输入图片说明

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

相关问题 参数化3D曲面图,颜色取决于(x,y,z) - Parametric 3D Surface Plot with color depending on (x,y,z) 绘制 Z 取决于 X 和 Y 向量的曲面 - Plot surface where Z depends on a vector of X and Y (python) 绘制 3d 表面,颜色图为第 4 维,x,y,z 的函数 - (python) plot 3d surface with colormap as 4th dimension, function of x,y,z 如何从 Plotly 中的 X、Y、Z 数组绘制 3D 表面? - How to plot a 3D surface from X,Y,Z array in Plotly? 在python中从{x,y,z} - 散射数据绘制3D表面 - Plot a 3D surface from {x,y,z}-scatter data in python 当Z是Python中的列表列表时,如何用X,Y,Z绘制3D曲面? - How to plot 3D surface with X, Y, Z when Z is a list of list in Python? 如何绘制来自3个不同列表(x,y和z)的3d直方图,其中z是所有重复的x,y坐标的平均值 - How to plot a 3d histogram from 3 different lists (x, y and z), where z is the mean of all repeated x,y coordinates 如何在3D曲面图中绘制x,y,z的Numpy数组? - How can I plot a numpy array of x, y, z in 3D surface plot? 给定 x,y,z 坐标的数据,我将如何在 Matplotlib 中制作 3D 表面 plot? - How would I make a 3D surface plot in Matplotlib given this data of x,y,z coordinates? Python-如何在表面离散3D点(x,y,z)之后从给定的x,y获取z值 - Python - How to get z value from given x, y after surface discrete 3D points (x,y,z)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM