python - 在 Matplotlib 上更改 3D 绘图的纵横比
问题描述
我正在尝试使用 Matplotlib 设置 3D 图的纵横比。按照这个问题的答案:设置 3D 绘图的纵横比
我将解决方案应用为:
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 的值,它变得更好,但仍然裁剪数据:
解决方案
您可以尝试更改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 问题:
该问题是由于将 3D 数据减少到 2D + z 阶标量而发生的。单个值表示集合中 3D 对象所有部分的第 3 维。因此,当两个集合的边界框相交时,就有可能出现这种伪影。此外,两个 3D 对象(例如多边形或补丁)的交集无法在 matplotlib 的 2D 渲染引擎中正确渲染。
推荐阅读
- java - Tomcat 拒绝所有 POST 请求
- python - 如何在 python 中解决一组 ODE,其中一个包含 dirac delta 函数(或逐步增量)?
- php - Zf3:每个模块中的类似Controllers
- c# - C#从Rabbit MQ每x间隔以y批量大小消费/读取消息
- javascript - 异步 Javascript:意外结果
- git - Azure Build Pipeline 从 git repo 获取最新标签
- python - 如何使用 python 创建 EC2 实例并附加已创建的安全组
- javascript - 将一个页面重定向到另一个页面 10 秒,然后再次重定向回原始 URL
- swift - 为什么这段代码会产生如此令人困惑的错误信息?
- r - 用 dopar 迭代地运行 foreach