【问题标题】:Changing aspect ratio for 3D plots on Matplotlib在 Matplotlib 上更改 3D 绘图的纵横比
【发布时间】:2020-02-24 17:37:14
【问题描述】:

我正在尝试使用 Matplotlib 设置 3D 绘图的纵横比。 按照这个问题的答案:Setting aspect ratio of 3D plot

我将解决方案应用为:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d

def get_proj(self):
    """
    Create the projection matrix from the current viewing position.

    elev stores the elevation angle in the z plane
    azim stores the azimuth angle in the (x, y) plane

    dist is the distance of the eye viewing point from the object point.
    """
    # chosen for similarity with the initial view before gh-8896

    relev, razim = np.pi * self.elev/180, np.pi * self.azim/180

    #EDITED TO HAVE SCALED AXIS
    xmin, xmax = np.divide(self.get_xlim3d(), self.pbaspect[0])
    ymin, ymax = np.divide(self.get_ylim3d(), self.pbaspect[1])
    zmin, zmax = np.divide(self.get_zlim3d(), self.pbaspect[2])

    # transform to uniform world coordinates 0-1, 0-1, 0-1
    worldM = proj3d.world_transformation(xmin, xmax,
                                         ymin, ymax,
                                         zmin, zmax)

    # look into the middle of the new coordinates
    R = self.pbaspect / 2

    xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist
    yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist
    zp = R[2] + np.sin(relev) * self.dist
    E = np.array((xp, yp, zp))

    self.eye = E
    self.vvec = R - E
    self.vvec = self.vvec / np.linalg.norm(self.vvec)

    if abs(relev) > np.pi/2:
        # upside down
        V = np.array((0, 0, -1))
    else:
        V = np.array((0, 0, 1))
    zfront, zback = -self.dist, self.dist

    viewM = proj3d.view_transformation(E, R, V)
    projM = self._projection(zfront, zback)
    M0 = np.dot(viewM, worldM)
    M = np.dot(projM, M0)
    return M

Axes3D.get_proj = get_proj

然后我创建一个示例数据并绘制为:

y,z,x = np.meshgrid(range(10),-np.arange(5)[::-1],range(20))
d = np.sin(x)+np.sin(y)+np.sin(z)

iy,ix = 0,-1
iz = -1

fig,ax = plt.subplots(figsize=(5,5),subplot_kw={'projection':'3d'})
ax.pbaspect = np.array([1, 1, 1])#np.array([2.0, 1.0, 0.5])

ax.contourf(x[iz], y[iz], d[iz],zdir='z',offset=0)
ax.contourf(x[:,iy,:],d[:,iy,:],z[:,iy,:],zdir='y',offset=y.min())
ax.contourf(d[:,:,ix],y[:,:,ix],z[:,:,ix],zdir='x',offset=x.max())

color = '0.3'
ax.plot(x[0,iy,:],y[0,iy,:],y[0,iy,:]*0,color,linewidth=1,zorder=1e4)
ax.plot(x[:,iy,0]*0+x.max(),y[:,iy,0],z[:,iy,0],color,linewidth=1,zorder=1e4)

ax.plot(x[0,:,ix],y[0,:,ix],y[0,:,ix]*0,color,linewidth=1,zorder=1e4)
ax.plot(x[:,0,ix],y[:,0,ix]*0+y.min(),z[:,0,ix],color,linewidth=1,zorder=1e4)


ax.set(xlim=[x.min(),x.max()],ylim=[y.min(),y.max()],zlim=[z.min(),z.max()])

fig.tight_layout()

如果我将 pbaspect 参数设置为 (1,1,1) 我得到:

但如果我将其设置为 (2,1,0.5),轴似乎是正确的,但它会以某种方式裁剪数据:

即使我让 xlim、ylim 和 zlim 自动运行。这方面也有一些奇怪的地方。 有些东西告诉我轴不是正交的。

有人知道如何为此校正纵横比吗? 我也想知道如何避免轴被图形限制裁剪。

我在网上搜索了很长时间,但找不到更好的解决方案。

更新:

我尝试按照建议为 pbaspect 使用小于 1 的值,它变得更好,但仍然裁剪数据:

【问题讨论】:

  • 使用小于或等于 1 的坐标,例如ax.pbaspect = np.array([1.0, .5, 0.25]).
  • 它变得更好了,但它仍然会裁剪数据。查看编辑。
  • 更改投影可能会奏效:ax.set_proj_type('ortho')ax.set_proj_type('persp') - matplotlib.org/matplotblog/posts/custom-3d-engine 对此进行了很好的讨论

标签: python matplotlib graphics data-analysis


【解决方案1】:

您可以尝试更改figsize 并调整子图边距,如下所示:

fig = plt.figure(figsize=(10,6))
fig.subplots_adjust(left=0.2, right=0.8, top=0.8, bottom=0.2)
ax = fig.gca(projection='3d')
ax.pbaspect = np.array([2, 1, 0.5])
ax.contourf(x[iz], y[iz], d[iz],zdir='z',offset=0)
ax.contourf(x[:,iy,:],d[:,iy,:],z[:,iy,:],zdir='y',offset=y.min())
ax.contourf(d[:,:,ix],y[:,:,ix],z[:,:,ix],zdir='x',offset=x.max())
ax.plot(x[0,iy,:],y[0,iy,:],y[0,iy,:]*0,color,linewidth=1,zorder=1e4)
ax.plot(x[:,iy,0]*0+x.max(),y[:,iy,0],z[:,iy,0],color,linewidth=1,zorder=1e4)
ax.plot(x[0,:,ix],y[0,:,ix],y[0,:,ix]*0,color,linewidth=1,zorder=1e4)
ax.plot(x[:,0,ix],y[:,0,ix]*0+y.min(),z[:,0,ix],color,linewidth=1,zorder=1e4)
plt.show()

输出(2,1,0.5):

关于消失的图形有Matplotlib issue

由于将 3D 数据减少到 2D + z 顺序标量,会出现此问题。单个值表示集合中 3D 对象所有部分的第 3 维。因此,当两个集合的边界框相交时,就有可能出现这种伪影。此外,在 matplotlib 的 2D 渲染引擎中无法正确渲染两个 3D 对象(例如多边形或补丁)的交集。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多