python - 更改 3D 图(Matplotlib)的垂直(z)轴的位置?

标签 python matplotlib mplot3d

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

我想保持观点不变(查看数据的最佳方式)。有什么方法可以将轴强制到一侧或另一侧?

最佳答案

我需要类似的东西:在两侧绘制 zaxis。感谢@crayzeewulf 的回答,我采用了以下解决方法(左、右或两侧):

enter image description here

首先根据需要绘制 3d,然后再调用 show()包装 Axes3D使用一个简单地覆盖 draw() 的 Wrapper 类方法。

Wrapper 类调用只是将某些特征的可见性设置为 False,它会自行绘制并最终绘制带有修改后的 PLANES 的 zaxis。此包装类允许您在左侧、右侧或两侧绘制 zaxis。

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

关于python - 更改 3D 图(Matplotlib)的垂直(z)轴的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15042129/

相关文章:

python - 将 django sorl-thumbnail 与 django form-utils 一起使用

python-解决圆 table session (ZCO,2012)

python - 防止 matplotlib 将下划线解释为绘图标题中的下标

python-2.7 - plt.boxplot(data, vert = False) - 每个箱线图添加一个数据点 - python 2.7,Matplotlib 1.5.3

python - 将强度附加到 3D 图

python - 如何更改 matplotlib 中的 3d 轴设置

python - 下面的代码在cpython中做了什么?

python - Python 中区分大小写的字符串替换

python - matplotlib 图中的链接时间轴(x 轴)

python - 使用 Matplotlib 绘制 3D 绘图