问题描述
我正在尝试在R ^ 3中的一个多维数据集上可视化3个参数的函数,以了解函数的平滑度。下面的示例代码中显示了此问题的一个示例
I am trying to visualize a function of 3 parameters over a cube in R^3 to get an idea of the smoothness of the function. An example of this problem is shown in the sample code below
%pylab
from mpl_toolkits.mplot3d import Axes3D
import itertools
x = np.linspace(0,10,50)
y = np.linspace(0,15,50)
z = np.linspace(0,8,50)
points = []
for element in itertools.product(x, y, z):
points.append(element)
def f(vals):
return np.cos(vals[0]) + np.sin(vals[1]) + vals[2]**0.5
fxyz = map(f, points)
xi, yi, zi = zip(*points)
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xi, yi, zi, c=fxyz, alpha=0.5)
plt.show()
此方法的问题是无法可视化多维数据集的内部。是否有更好的方法在R ^ 3的某些密集子集上绘制函数图?
The problem with this approach is that the inside of the cube cannot be visualized. Is there a better way to graph a function over some dense subset of R^3?
推荐答案
如@HYRY和@nicoguaro所建议在上面的评论中,Mayavi更适合此类工作。 有很多示例我参考了。这是我想出的东西
As @HYRY and @nicoguaro suggested in the comments above, Mayavi is much better suited for this type of work. There is a good set of examples here that I used for reference. Here is what I came up with
import numpy as np
from mayavi import mlab
x = np.linspace(0,10,50)
y = np.linspace(0,15,50)
z = np.linspace(0,8,50)
X, Y, Z = np.meshgrid(x, y, z)
s = np.cos(X) + np.sin(Y) + Z**0.5
b1 = np.percentile(s, 20)
b2 = np.percentile(s, 80)
mlab.pipeline.volume(mlab.pipeline.scalar_field(s), vmin=b1, vmax=b2)
mlab.axes()
mlab.show()
之后,我旋转了用GUI绘制所需角度并保存所需视图
After which I rotated the figure to desired angles with the GUI and saved desired views
这篇关于使用matplotlib在给定域上绘制3维尺寸的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!