matplotlib(mplot3d)-如何在3D图中增加轴的尺寸(拉伸)? - python

到目前为止,我有:

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图片扩展了。

参考方案

下面的代码示例提供了一种缩放每个轴相对于另一个轴的方法。但是,这样做您需要修改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、3、1):

我之所以特别喜欢这种方法,
交换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()

Matplotlib-固定x轴缩放比例和自动缩放y轴 - python

我只想绘制部分数组,固定x部分,但让y部分自动缩放。我尝试如下所示,但是它不起作用。有什么建议么?import numpy as np import matplotlib.pyplot as plt data=[np.arange(0,101,1),300-0.1*np.arange(0,101,1)] plt.figure() plt.scatter(da…

如何在Linux上安装2个Anacondas(Python 2.7和3.5)? - python

我想使用Python 2和3版本。我已经读过有关conda环境的用法,但是不断向终端source (de)activate py27写入内容似乎不方便。如picture所示,如何使用命令选择内核版本? 参考方案 您在该图像中寻找的是Jupyter Notebook。您需要使用Jupyter和所需的python版本创建环境:conda create -n py…

为什么在for循环中将单词从复数形式转换为单数形式会花费这么长时间(Python 3)? - python

这是我的代码,用于从CSV文件读取文本并将一列中的所有单词从复数形式转换为单数形式:import pandas as pd from textblob import TextBlob as tb data = pd.read_csv(r'path\to\data.csv') for i in range(len(data)): blob …

Matplotlib-更改自动轴范围 - python

我将自动轴范围用于数据。例如,当我使用-29到+31之间的x数据时,ax = plt.gca() xsta, xend = ax.get_xlim() 我得到-30和40,这不能正确描述数据范围。我希望看到轴范围四舍五入为5,即极限为-30和35。有可能这样做吗?或者,是否有可能获取x轴数据的精确范围(-29,31),然后编写算法手动更改该值(使用set_x…

Matplotlib:保存图形时白色边距和隐藏轴 - python

我一直在尝试保存用matplotlib制作的图,但遇到了一些问题:不仅遇到了常见的白色边距问题(我在网上找到了一些解决方案),而且看来我的坐标轴和标签都在当我保存图像时,它们消失了,尽管当我要求Python show()结果时它们看起来很好。这是MWE,是我用show()得到的结果的打印屏幕(这是我想要的结果),以及将图形保存到.png时得到的结果(我相信白…