如何增加 3D 绘图中轴的尺寸(拉伸)
- 2025-04-10 09:46:00
- admin 原创
- 17
问题描述:
我目前有这个:
x,y,z = data.nonzero()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, zdir='z', c= 'red')
plt.savefig("plot.png")
这将产生:
我想做的是将其拉伸,使 Z 轴高出 9 倍,并保持 X 和 Y 不变。但我想保持相同的坐标。
到目前为止我尝试过这个家伙:
fig = plt.figure(figsize=(4.,35.))
但这只是拉长了 plot.png 图像。
解决方案 1:
下面的代码示例提供了一种相对于其他轴缩放每个轴的方法。但是,要做到这一点,您需要修改 Axes3D.get_proj 函数。下面是一个基于 matplot lib 提供的示例:http://matplotlib.org/1.4.0/mpl_toolkits/mplot3d/tutorial.html#line-plots
(本答案末尾有一个较短的版本)
from mpl_toolkits.mplot3d.axes3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
#Make sure these are floating point values:
scale_x = 1.0
scale_y = 2.0
scale_z = 3.0
#Axes are scaled down to fit in scene
max_scale=max(scale_x, scale_y, scale_z)
scale_x=scale_x/max_scale
scale_y=scale_y/max_scale
scale_z=scale_z/max_scale
#Create scaling matrix
scale = np.array([[scale_x,0,0,0],
[0,scale_y,0,0],
[0,0,scale_z,0],
[0,0,0,1]])
print scale
def get_proj_scale(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.
"""
relev, razim = np.pi * self.elev/180, np.pi * self.azim/180
xmin, xmax = self.get_xlim3d()
ymin, ymax = self.get_ylim3d()
zmin, zmax = self.get_zlim3d()
# transform to uniform world coordinates 0-1.0,0-1.0,0-1.0
worldM = proj3d.world_transformation(
xmin, xmax,
ymin, ymax,
zmin, zmax)
# look into the middle of the new coordinates
R = np.array([0.5, 0.5, 0.5])
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 / proj3d.mod(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)
perspM = proj3d.persp_transformation(zfront, zback)
M0 = np.dot(viewM, worldM)
M = np.dot(perspM, M0)
return np.dot(M, scale);
Axes3D.get_proj=get_proj_scale
"""
You need to include all the code above.
From here on you should be able to plot as usual.
"""
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure(figsize=(5,5))
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label='parametric curve')
ax.legend()
plt.show()
标准输出:
按照 (1, 2, 3) 缩放:
按照 (1, 1, 3) 缩放:
我特别喜欢这种方法的原因是,交换 z 和 x,按(3,1,1)缩放:
下面是该代码的较短版本。
from mpl_toolkits.mplot3d.axes3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure(figsize=(5,5))
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
"""
Scaling is done from here...
"""
x_scale=1
y_scale=1
z_scale=2
scale=np.diag([x_scale, y_scale, z_scale, 1.0])
scale=scale*(1.0/scale.max())
scale[3,3]=1.0
def short_proj():
return np.dot(Axes3D.get_proj(ax), scale)
ax.get_proj=short_proj
"""
to here
"""
ax.plot(z, y, x, label='parametric curve')
ax.legend()
plt.show()
解决方案 2:
请注意,下面的答案简化了补丁,但使用与@ChristianSarofeen 的答案相同的基本原理。
解决方案
正如其他答案中指出的那样,它不是当前在 matplotlib 中实现的功能。但是,由于您请求的只是一个可以应用于 matplotlib 使用的现有投影矩阵的3D 变换,并且得益于 Python 的出色功能,可以使用简单的一行程序解决此问题:
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([scale_x, scale_y, scale_z, 1]))
其中scale_x
,scale_y
和scale_z
是从 0 到 1 的值,它们将相应地沿每个轴重新缩放绘图。ax
只是 3D 轴,可以通过以下方式获得ax = fig.gca(projection='3d')
解释
解释一下,函数get_proj
从Axes3D
当前观察位置生成投影矩阵。将其乘以缩放矩阵:
scale_x, 0, 0
0, scale_y, 0
0, 0, scale_z
0, 0, 1
将缩放纳入渲染器使用的投影中。因此,我们在这里所做的是将原始get_proj
函数替换为一个表达式,该表达式取原始结果get_proj
并将其乘以缩放矩阵。
例子
用标准参数函数示例来说明结果:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z ** 2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
# OUR ONE LINER ADDED HERE:
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([0.5, 0.5, 1, 1]))
ax.plot(x, y, z)
plt.show()
对于值0.5, 0.5, 1
,我们得到:
而对于值0.2, 1.0, 0.2
,我们得到:
解决方案 3:
在我的例子中,我想将 z 轴拉伸 2 倍,以获得更好的点可见性
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
# plt.rcParams["figure.figsize"] = (10,200)
# plt.rcParams["figure.autolayout"] = True
ax = plt.axes(projection='3d')
ax.set_box_aspect(aspect = (1,1,2))
ax.plot(dataX,dataY,dataZ)
解决方案 4:
默认情况下,mplot3d 会在非常高的绘图的顶部和底部留出相当多的空间。但是,你可以诱使它使用 填充该空间fig.subplots_adjust
,并将顶部和底部延伸出正常绘图区域(即top > 1
和bottom < 0
)。对于你的特定绘图,这里可能需要一些反复试验。
我已经为 x、y 和 z 创建了一些随机数组,其限制与您的图类似,并且发现下面的参数(bottom=-0.15
,top = 1.2
)似乎运行正常。
您可能还想更改ax.view_init
以设置一个良好的视角。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from numpy import random
# Make some random data with similar limits to the OP's example
x,y,z=random.rand(3,100)
z*=250
y*=800
y+=900
x*=350
x+=1200
fig=plt.figure(figsize=(4,35))
# Set the bottom and top outside the actual figure limits,
# to stretch the 3D axis
fig.subplots_adjust(bottom=-0.15,top=1.2)
ax = fig.add_subplot(111, projection='3d')
# Change the viewing angle to an agreeable one
ax.view_init(2,None)
ax.scatter(x, y, z, zdir='z', c= 'red')
plt.savefig("plot.png")
解决方案 5:
听起来你正在尝试调整绘图的比例。我认为没有办法将线性比例拉伸到用户要求的水平,但你可以使用set_yscale()
、set_xscale()
、set_zscale()
来相互改变比例。
直观地说set_yscale(log)
,,,可能会解决您的问题。set_xscale(log)
`set_zscale(linear)`
可能更好的选择:指定一个拉伸,将它们全部设置为具有相同对数底数的 symlog,然后使用 kwargs 根据您的规范指定 Z 轴的 symlog 比例linscalex/linscaley
。
更多内容请见:
http://matplotlib.org/mpl_toolkits/mplot3d/api.html
解决方案 6:
我在搜索类似问题时发现了这一点。经过一些实验,也许我可以在这里分享我的一些初步发现。matplotlib 库非常强大!!(我是新手)。请注意,与这个问题非常相似,我想要的只是“视觉上”拉伸图表而不扭曲它。
背景故事(仅显示关键代码片段,以避免对了解该库的人造成不必要的混乱,如果您想要可运行的代码,请发表评论):我有三个 1-d ndarray,分别代表 X、Y 和 Z 数据点。显然我不能使用 plot_surface(因为它需要每个 dim 的 2d ndarray),所以我选择了非常有用的 plot_trisurf:
fig = plt.figure()
ax = Axes3D(fig)
3d_surf_obj = ax.plot_trisurf(X, Y, Z_defl, cmap=cm.jet,linewidth=0,antialiased=True)
您可以将情节想象成一艘在波浪中变形的漂浮驳船……正如您所看到的,轴的拉伸使其在视觉上非常具有欺骗性(请注意,x 应该比 y 和 >>>>> z 长 6 倍)。虽然情节要点是正确的,但我至少想要一些在视觉上更“拉伸”的东西。如果可以的话,我正在寻找一个快速解决方案。长话短说,我发现使用“figure.figsize”常规设置(见下面的代码片段)取得了一些成功。
matplotlib.rcParams.update({'font.serif': 'Times New Roman',
'font.size': 10.0,
'axes.labelsize': 'Medium',
'axes.labelweight': 'normal',
'axes.linewidth': 0.8,
###########################################
# THIS IS THE IMPORTANT ONE FOR STRETCHING
# default is [6,4] but...i changed it to
'figure.figsize':[15,5] # THIS ONE #
})
对于 [15,5] 我得到了类似的结果......
非常整洁!!
所以我开始推动它....并上升到 [20,6] 然后决定在那里安定下来..
如果您想尝试在视觉上拉伸垂直轴,请尝试使用如下比例...... [7,10],在这种情况下,这给了我...
不太寒酸!
应该为了视觉能力而这样做。
解决方案 7:
将所有 z 值乘以 9,
ax.scatter(x, y, 9*z, zdir='z', c= 'red')
然后为 z 轴提供自定义绘图标签和间距。
ax.ZTick = [0,-9*50, -9*100, -9*150, -9*200];
ax.ZTickLabel = {'0','-50','-100','-150','-200'};
扫码咨询,免费领取项目管理大礼包!