Setting aspect ratio of 3D plot

后端 未结 4 1583
终归单人心
终归单人心 2020-11-27 15:15

I am trying to plot a 3D image of the seafloor from the data of a sonar run over a 500m by 40m portion of the seafloor. I am using matplotlib/mplot3d with Axes3D and I want

相关标签:
4条回答
  • 2020-11-27 15:53

    The answer to this question works perfectly for me. And you do not need to set up any ratio, it does everything automatically.

    0 讨论(0)
  • 2020-11-27 15:56

    That how i solved the wasted space problem:

    try: 
        self.localPbAspect=self.pbaspect
        zoom_out = (self.localPbAspect[0]+self.localPbAspect[1]+self.localPbAspect[2]) 
    except AttributeError: 
        self.localPbAspect=[1,1,1]
        zoom_out = 0 
    xmin, xmax = self.get_xlim3d() /  self.localPbAspect[0]
    ymin, ymax = self.get_ylim3d() /  self.localPbAspect[1]
    zmin, zmax = self.get_zlim3d() /  self.localPbAspect[2]
    
    # 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*self.localPbAspect[0], 0.5*self.localPbAspect[1], 0.5*self.localPbAspect[2]])
    xp = R[0] + np.cos(razim) * np.cos(relev) * (self.dist+zoom_out)
    yp = R[1] + np.sin(razim) * np.cos(relev) * (self.dist+zoom_out)
    zp = R[2] + np.sin(relev) * (self.dist+zoom_out)
    E = np.array((xp, yp, zp))
    
    0 讨论(0)
  • 2020-11-27 15:58

    An issue has been opened over at github: https://github.com/matplotlib/matplotlib/issues/8593

    The above solutions don't seem to work any more. One has to edit the get_proj function inside site-packages\mpl_toolkits\mplot3d\axes3d.py in the following way now:

            try:
                self.localPbAspect = self.pbaspect
            except AttributeError:
                self.localPbAspect = [1,1,1]
    
            xmin, xmax = ( lim / self.localPbAspect[0] for lim in self.get_xlim3d() )
            ymin, ymax = ( lim / self.localPbAspect[1] for lim in self.get_ylim3d() )
            zmin, zmax = ( lim / self.localPbAspect[2] for lim in self.get_zlim3d() )
    
    0 讨论(0)
  • 2020-11-27 15:59

    Add following code before savefig:

    ax.auto_scale_xyz([0, 500], [0, 500], [0, 0.15])
    

    enter image description here

    If you want no square axis:

    edit the get_proj function inside site-packages\mpl_toolkits\mplot3d\axes3d.py:

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

    then add one line to set pbaspect:

    ax = fig.gca(projection = '3d')
    ax.pbaspect = [2.0, 0.6, 0.25]
    

    enter image description here

    0 讨论(0)
提交回复
热议问题