从mplot3d图形获取数据 [英] Get data from mplot3d graph
问题描述
我找不到如何从mplot3d图形中获取数据的方法.类似于2D样式:
I can’t find out how to get data from an mplot3d graph. Something similar to the 2D style:
line.get_xdata()
有可能吗?
推荐答案
Line3D
您可以从(私有)_verts3d
属性获取原始数据
Line3D
You can get get the original data from the (private) _verts3d
attribute
xdata, ydata, zdata = line._verts3d
print(xdata)
完整示例
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
# Prepare arrays x, y, z
x = np.arange(5)
y = np.arange(5)*4
z = np.arange(5)*100
line, = ax.plot(x, y, z, label='curve')
fig.canvas.draw()
xdata, ydata, zdata = line._verts3d
print(xdata) # This prints [0 1 2 3 4]
plt.show()
一些解释:get_data
或get_xdata
的问题在于,绘制图形后它将返回投影坐标.因此,在绘制图形之前,line.get_xdata()
确实会返回正确的值,而在绘制之后,它将返回类似
Some explanation: The problem with get_data
or get_xdata
is that it will return the projected coordinates once the figure is drawn. So while before drawing the figure, line.get_xdata()
would indeed return the correct values, after drawing, it would return something like
[ -6.14413090e-02 -3.08824862e-02 -3.33066907e-17 3.12113190e-02 6.27567511e-02]
在上面的示例中,
是投影到2D上的3D坐标的x分量.
in the above example, which is the x component of the 3D coordinates projected onto 2D.
有一个向matplotlib发出拉动请求,该请求将允许获取数据通过方法get_data_3d
.仍然没有合并,但是可以允许在不使用私有参数的情况下完成上述操作.
There is a pull request to matplotlib, which would allow to get the data via methods get_data_3d
. This is still not merged, but might allow the above to be done without using private arguments in a future version of matplotlib.
对于plot_surface
图,这看起来很相似,除了要查看的属性是._vec
For a plot_surface
plot this looks similar, except that the attribute to look at is the ._vec
surf = ax.plot_surface(X, Y, Z)
xdata, ydata, zdata, _ = surf._vec
print(xdata)
这篇关于从mplot3d图形获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!