如何在3D绘图中增加轴(拉伸)的大小

36

到目前为止, 我有:

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")

创建了什么: enter image description here

我想做的是将Z轴拉伸9倍,保持X和Y不变。但我想保持相同的坐标。
到目前为止,我尝试了这个方法:
fig = plt.figure(figsize=(4.,35.))

但这只是拉伸了plot.png图像。


你尝试过使用 set_zlim(z_min, z_max) 吗?或者是 set_zlim3d - wflynny
谢谢您的建议。zlim是做什么用的?我不确定应该为z_min和z_max放什么值。我对它的z边界很满意,只是想要它拉伸一下。 - Greg
我误解了。看起来你想改变3D图的纵横比。这取决于你使用的matplotlib版本。尝试查看这里1这里2 - wflynny
谢谢。我想我不明白here1链接会做什么。它似乎不会设置纵横比。在提问之前,我阅读了here2,但我看不出如何将答案应用于我的代码。 - Greg
here1被接受的答案修改了轴的纵横比。这是一种hack方法,因此您需要修改matplotlib的文件。默认情况下似乎不允许使用不同的纵横比。 - Imanol Luengo
1
这个问题有一个一行解决方案。只需要在你的代码中添加一行非常简单的猴子补丁。请查看我下面的答案:https://dev59.com/TF0a5IYBdhLWcg3whJHW#30419243 - Andrzej Pronobis
7个回答

43

下面的代码示例提供了一种相对于其他轴缩放每个轴的方法。但是,为了这样做,需要修改Axes3D.get_proj函数。以下是基于Matplotlib提供的示例的示例: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):

Scale_x=1, Scale_y=2, Scale_z=3

缩放比例为 (1, 1, 3):

Scale_x=1, Scale_y=1, Scale_z=3

我特别喜欢这种方法的原因是,将 z 和 x 交换位置,缩放比例为 (3, 1, 1):

交换 z 和 x,scale_x=4

以下是代码的简化版本。

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()

@gboffi 我可以。你应该能够简单地复制代码并运行它。 - Christian Sarofeen
哈哈,你比我快。不过我认为这是正确的方法。你可以在这里查看默认缩放、world_transformation():https://github.com/matplotlib/matplotlib/blob/4314d447dfc7127daa80fa295c9bd56cf07faf01/lib/mpl_toolkits/mplot3d/proj3d.py - Ben
1
唯一的限制是它需要数据被居中。否则,点积也会创建图形的空间变换,可能会将其移出屏幕。 - Prageeth Jayathissa
我发现使用这个解决方案得到的结果图总是太缩小了。 - Jason

35

请注意,以下答案简化了补丁,但使用了与 @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_xscale_yscale_z是来自0到1的值,将按照对应的轴重新缩放您的图形。 ax只是可以使用ax = fig.gca(projection='3d')获取的三维坐标轴。

Explanation

为了解释,Axes3Dget_proj函数从当前的视角位置生成投影矩阵。将其乘以缩放矩阵:

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,我们得到:

enter image description here

而对于值为0.2, 1.0, 0.2,我们得到:

enter image description here


15

在我的情况下,我想将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)

5
我认为这是最棒的现代答案,显然现在你不需要别的,这个效果非常好! - nicael

7
默认情况下,mplot3d会在非常高的图形顶部和底部留下相当多的空间。但是,您可以使用fig.subplots_adjust来欺骗它填充该空间,并将顶部和底部扩展到正常绘图区域之外(即top > 1bottom < 0)。对于您特定的情节,可能需要进行一些试验和误差。
我已经为x、y和z创建了一些随机数组,其限制类似于您的情节,并发现下面的参数(bottom=-0.15top=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")


1
我不喜欢这个选项,因为它只是扭曲了视口。当你尝试旋转这个图时,你会发现图像实际上只是被扭曲了。 - Christian Sarofeen

1

听起来你正在尝试调整图表的比例。我不认为有一种方法可以将线性比例按照用户规格拉伸,但是您可以使用set_yscale()set_xscale()set_zscale() 来相互改变比例。

直观地说,set_yscale(log)set_xscale(log)set_zscale(linear) 可能会解决您的问题。

更好的选择是:指定一个拉伸,将它们全部设置为具有相同对数基数的symlog,然后使用linscalex/linscaley kwargs 指定Z轴的symlog比例以符合您的规格。

更多信息请参见:

http://matplotlib.org/mpl_toolkits/mplot3d/api.html


1

在搜索类似问题时,我发现了这个。经过一些试验后,也许我可以在这里分享一些我的初步发现... matplotlib库非常庞大!(我是新手)。请注意,与这个问题非常相似,我想要的只是在不扭曲图表的情况下“视觉”拉伸它。

背景故事(仅显示关键代码片段,以避免对那些了解该库的人造成不必要的混乱,如果您需要可运行的代码,请留言): 我有三个1-d ndarrays分别表示X、Y和Z数据点。显然,我不能使用plot_surface(因为它需要每个dim的2d ndarrays),所以我选择了非常有用的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)

enter image description here

你可以把这个情节想象成一个在波浪中变形的漂浮驳船...正如你所看到的,轴的伸展使得它在视觉上非常欺骗(请注意,x应该比y长6倍,而且z更长)。虽然情节点是正确的,但我希望至少有更加“拉伸”的视觉效果。如果可以的话,我正在寻找一个快速解决方法。长话短说,我在“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],我得到了类似于...

enter image description here

非常不错!!

于是我开始推动它......在决定停留在[20,6]之前,我已经推到了那里。

enter image description here

如果您想尝试在视觉上拉伸垂直轴,请使用比率,如[7,10],在这种情况下,它给我...

enter image description here

不错!

对于视觉效果来说,这应该足够了。


我复制了你的例子,但不幸的是它不起作用。 - flatronka
嗨@flatronka,你能详细说明一下哪里出了问题/遇到了什么错误吗?我很乐意帮忙。 - aaronlhe

-3

将所有的 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'};

这不会使Z轴变高。 - Christian Sarofeen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接