我需要对一些复杂的多变量数据集进行可视化,并且可选择的选择是使用堆叠的2D图来修改平行轴可视化,其中每个图绘制一个自由度/模型参数和属于相同数据集的数据点应该在不同的地块之间相互联系。我附上一个概念草图。我怎样才能在matplotlib
中实现它?
答案 0 :(得分:2)
要想有一个粗略的想法,这可能是matplotlib使用Axes3D
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle, PathPatch
import mpl_toolkits.mplot3d.art3d as art3d
x = np.array([1,2,3])
y = np.array([2,3,1])
z = np.array([1,1,1])
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0,0,1,1], projection='3d')
#plot the points
ax.scatter(x,y,z*0.4, c="r", facecolor="r", s=60)
ax.scatter(y,x,z*0.9, c="b", facecolor="b", s=60)
ax.scatter(x,y,z*1.6, c="g", facecolor="g", s=60)
#plot connection lines
ax.plot([x[0],y[0],x[0]],[y[0],x[0],y[0]],[0.4,0.9,1.6], color="k")
ax.plot([x[2],y[2],x[2]],[y[2],x[2],y[2]],[0.4,0.9,1.6], color="k")
#plot planes
p = Rectangle((0,0), 4,4, color="r", alpha=0.2)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0.4, zdir="z")
p = Rectangle((0,0), 4,4, color="b", alpha=0.2)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0.9, zdir="z")
p = Rectangle((0,0), 4,4, color="g", alpha=0.2)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=1.6, zdir="z")
ax.set_aspect('equal')
ax.view_init(13,-63)
ax.set_xlim3d([0,4])
ax.set_ylim3d([0,4])
ax.set_zlim3d([0,2])
plt.savefig(__file__+".png")
plt.show()
<小时/> 更新
可以创建三个不同的轴。必须添加轴并使上面的轴透明(ax2.patch.set_alpha(0.)
)。然后必须关闭网格(ax.grid(False)
)并且我们不需要的窗格和线条设置为不可见。
但是,我不知道如何在轴之间绘制连接。 matplotlib.patches.ConnectionPatch
的2D方法不适用于3D轴。
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import mpl_toolkits.mplot3d.art3d as art3d
x = np.array([1,2,3])
y = np.array([2,3,1])
z = np.array([0,0,0])
fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0,0,1,1], projection='3d')
ax2 = fig.add_axes([0.0,0.24,1,1], projection='3d')
ax2.patch.set_alpha(0.)
ax3 = fig.add_axes([0.0,0.48,1,1], projection='3d')
ax3.patch.set_alpha(0.)
#plot the points
ax.scatter(x,y,z, c="r", facecolor="r", s=60)
ax2.scatter(y*4,x*4,z, c="b", facecolor="b", s=60)
ax3.scatter(x*100,y*100,z, c="g", facecolor="g", s=60)
#plot connection lines
#ax.plot([x[0],y[0],x[0]],[y[0],x[0],y[0]],[0.4,0.9,1.6], color="k")
#ax.plot([x[2],y[2],x[2]],[y[2],x[2],y[2]],[0.4,0.9,1.6], color="k")
#plot planes
p = Rectangle((0,0), 4,4, color="r", alpha=0.2)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
p = Rectangle((0,0), 16,16, color="b", alpha=0.2)
ax2.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
p = Rectangle((0,0), 400,400, color="g", alpha=0.2)
ax3.add_patch(p)
art3d.pathpatch_2d_to_3d(p, z=0, zdir="z")
ax.set_aspect('equal')
ax2.set_aspect('equal')
ax3.set_aspect('equal')
ax.view_init(13,-63)
ax2.view_init(10,-63)
ax3.view_init(8,-63)
ax.set_xlim3d([0,4])
ax.set_ylim3d([0,4])
ax.set_zlim3d([0,2])
ax2.set_xlim3d([0,16])
ax2.set_ylim3d([0,16])
ax2.set_zlim3d([0,2])
ax3.set_xlim3d([0,400])
ax3.set_ylim3d([0,400])
ax3.set_zlim3d([0,2])
ax.grid(False)
ax2.grid(False)
ax3.grid(False)
def axinvisible(ax):
for zax in (ax.w_zaxis, ax.w_xaxis, ax.w_yaxis):
zax.pane.set_visible(False)
if zax == ax.w_zaxis:
zax.line.set_visible(False)
for ll in zax.get_ticklines()+zax.get_ticklabels():
ll.set_visible(False)
axinvisible(ax)
axinvisible(ax2)
axinvisible(ax3)
# setting a ConnectionPatch does NOT work
from matplotlib.patches import ConnectionPatch
con = ConnectionPatch(xyA=(2,2), xyB=(2,2),
coordsA='data', coordsB='data',
axesA=ax, axesB=ax2,
arrowstyle='->', clip_on=True)
ax2.add_artist(con) # artist is not shown :-(
plt.show()
答案 1 :(得分:0)
解决方案可以基于matplotlib's 3D capabilities。您可以使用Axes3D.plot_surface
绘制轴,并使用用户散点图和其余部分的线图。