首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否更改3D绘图(Matplotlib)的垂直(z)轴的位置?

是否更改3D绘图(Matplotlib)的垂直(z)轴的位置?
EN

Stack Overflow用户
提问于 2013-02-23 23:25:37
回答 2查看 13.3K关注 0票数 18

我正在用Python中的Matplotlib做一些3D表面绘图,并注意到了一个恼人的现象。根据我如何设置视点(相机位置),垂直(z)轴在左侧和右侧之间移动。这里有两个例子:Example 1, Axis leftExample 2, Axis right。第一个示例具有ax.view_init(25,-135),而第二个示例具有ax.view_init(25,-45)。

我想保持视点不变(查看数据的最佳方式)。有没有办法迫使轴移动到一边或另一边?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-01 23:15:23

我需要一些类似的东西:在两边画z轴。感谢@crayzeewulf的回答,我得到了以下变通方法(左侧、右侧或两侧):

首先根据需要绘制3d图形,然后在调用show()之前,用一个简单覆盖draw()方法的包装器类包装Axes3D

包装器类调用简单地将某些特征的可见性设置为False,它绘制自己,并最终绘制带有修改平面的zaxis。此包装器类允许您在左侧、右侧或两侧绘制z轴。

代码语言:javascript
复制
import matplotlib
matplotlib.use('QT4Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

class MyAxes3D(axes3d.Axes3D):

    def __init__(self, baseObject, sides_to_draw):
        self.__class__ = type(baseObject.__class__.__name__,
                              (self.__class__, baseObject.__class__),
                              {})
        self.__dict__ = baseObject.__dict__
        self.sides_to_draw = list(sides_to_draw)
        self.mouse_init()

    def set_some_features_visibility(self, visible):
        for t in self.w_zaxis.get_ticklines() + self.w_zaxis.get_ticklabels():
            t.set_visible(visible)
        self.w_zaxis.line.set_visible(visible)
        self.w_zaxis.pane.set_visible(visible)
        self.w_zaxis.label.set_visible(visible)

    def draw(self, renderer):
        # set visibility of some features False 
        self.set_some_features_visibility(False)
        # draw the axes
        super(MyAxes3D, self).draw(renderer)
        # set visibility of some features True. 
        # This could be adapted to set your features to desired visibility, 
        # e.g. storing the previous values and restoring the values
        self.set_some_features_visibility(True)

        zaxis = self.zaxis
        draw_grid_old = zaxis.axes._draw_grid
        # disable draw grid
        zaxis.axes._draw_grid = False

        tmp_planes = zaxis._PLANES

        if 'l' in self.sides_to_draw :
            # draw zaxis on the left side
            zaxis._PLANES = (tmp_planes[2], tmp_planes[3],
                             tmp_planes[0], tmp_planes[1],
                             tmp_planes[4], tmp_planes[5])
            zaxis.draw(renderer)
        if 'r' in self.sides_to_draw :
            # draw zaxis on the right side
            zaxis._PLANES = (tmp_planes[3], tmp_planes[2], 
                             tmp_planes[1], tmp_planes[0], 
                             tmp_planes[4], tmp_planes[5])
            zaxis.draw(renderer)

        zaxis._PLANES = tmp_planes

        # disable draw grid
        zaxis.axes._draw_grid = draw_grid_old

def example_surface(ax):
    """ draw an example surface. code borrowed from http://matplotlib.org/examples/mplot3d/surface3d_demo.html """
    from matplotlib import cm
    import numpy as np
    X = np.arange(-5, 5, 0.25)
    Y = np.arange(-5, 5, 0.25)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)

if __name__ == '__main__':
    fig = plt.figure(figsize=(15, 5))
    ax = fig.add_subplot(131, projection='3d')
    ax.set_title('z-axis left side')
    ax = fig.add_axes(MyAxes3D(ax, 'l'))
    example_surface(ax) # draw an example surface
    ax = fig.add_subplot(132, projection='3d')
    ax.set_title('z-axis both sides')
    ax = fig.add_axes(MyAxes3D(ax, 'lr'))
    example_surface(ax) # draw an example surface
    ax = fig.add_subplot(133, projection='3d')
    ax.set_title('z-axis right side')
    ax = fig.add_axes(MyAxes3D(ax, 'r'))
    example_surface(ax) # draw an example surface
    plt.show()
票数 15
EN

Stack Overflow用户

发布于 2013-02-24 12:24:15

正如OP在下面的评论中指出的那样,下面建议的方法没有对原始问题提供充分的答案。

正如this笔记中提到的,axis3d中有许多硬编码的值,这使得自定义其行为变得困难。因此,我不认为在当前的API中有一个好的方法来做到这一点。您可以通过修改zaxis_PLANES参数来“黑”它,如下所示:

代码语言:javascript
复制
tmp_planes = ax.zaxis._PLANES 
ax.zaxis._PLANES = ( tmp_planes[2], tmp_planes[3], 
                     tmp_planes[0], tmp_planes[1], 
                     tmp_planes[4], tmp_planes[5])
view_1 = (25, -135)
view_2 = (25, -45)
init_view = view_2
ax.view_init(*init_view)

现在,无论您如何旋转图形,z轴都将始终位于图形的左侧(只要正z方向指向上方)。但是,x轴和y轴将继续翻转。您可以使用_PLANES,也许能够获得所有轴的所需行为,但这可能会在未来的matplotlib版本中中断。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15042129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档