将 matplotlib 3D 图形的框架更改为 x、y 和 z 箭头

use*_*933 8 python plot visualization data-visualization matplotlib

可以通过在 x、y 和 z 轴顶部叠加箭头来将图形的箭头更改为箭头,以创建轴是箭头的错觉,或者直接将框架的设置更改为Matplot lib 框架,以便在 3D 绘图上获得相同的结果,用箭头显示 (x,y,z)?

转动这个

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# generate sample points and straight line
z = np.repeat(0, 100) 
x = np.repeat(1.0, 100) 
y = np.linspace(start=3.0, stop=6.0, num=100) 
ax.plot(x, y, z, c='red') # draw straight line
ax.view_init(45, -150) # angle to show

# set axes limits and labels
ax.set_xlabel(r"$x$"); ax.set_ylabel(r"$y$"); ax.set_zlabel(r"$z$")
ax.set_xlim(0,1.1) ;ax.set_ylim(6,3) ;ax.set_zlim(0,1.75)

#  Remove tick marks
ax.set_xticks([0,0.25,0.5,0.75,1]) ; ax.set_xticklabels(['0','1','2','4','T'])
ax.set_yticks([6.0,5.5,5,4.5,4.0,3.5,3]) ; ax.set_yticklabels(["","","","","","",""])
ax.set_zticks([1.75,1.25,0.75,0.25]) ax.set_zticklabels(['','','',''])

# change background colour to white
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))

#plt.savefig("sample.png", type="png",dbi=400) # save image 
plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

变成这样的东西:

r-b*_*ers 6

我通常不使用 3D 图表,我做了很多研究来回答你的问题。这是我发现的一个很好的方法。我创建了一个新的 Arrow 3D 类并实现了它。在您的代码中,我添加了该类并向 x、y 和 z 轴添加了箭头。我手动移动它们的位置以使它们在轴上对齐。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.proj3d import proj_transform
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d

class Arrow3D(FancyArrowPatch):

    def __init__(self, x, y, z, dx, dy, dz, *args, **kwargs):
        super().__init__((0, 0), (0, 0), *args, **kwargs)
        self._xyz = (x, y, z)
        self._dxdydz = (dx, dy, dz)

    def draw(self, renderer):
        x1, y1, z1 = self._xyz
        dx, dy, dz = self._dxdydz
        x2, y2, z2 = (x1 + dx, y1 + dy, z1 + dz)

        xs, ys, zs = proj_transform((x1, x2), (y1, y2), (z1, z2), self.axes.M)
        self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
        super().draw(renderer)

def _arrow3D(ax, x, y, z, dx, dy, dz, *args, **kwargs):
    '''Add an 3d arrow to an `Axes3D` instance.'''

    arrow = Arrow3D(x, y, z, dx, dy, dz, *args, **kwargs)
    ax.add_artist(arrow)

setattr(Axes3D, 'arrow3D', _arrow3D)

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111, projection='3d')

# generate sample points and straight line
z = np.repeat(0, 100) 
x = np.repeat(1.0, 100) 
y = np.linspace(start=3.0, stop=6.0, num=100) 
ax.plot(x, y, z, c='red') # draw straight line
ax.view_init(45, -150) # angle to show

# set axes limits and labels
ax.set_xlabel(r"$x$"); ax.set_ylabel(r"$y$"); ax.set_zlabel(r"$z$")
ax.set_xlim(0,1.1) ;ax.set_ylim(6,3) ;ax.set_zlim(0,1.75)

#  Remove tick marks
ax.set_xticks([0,0.25,0.5,0.75,1])
ax.set_xticklabels(['0','1','2','4','T'])
ax.set_yticks([6.0,5.5,5,4.5,4.0,3.5,3])
ax.set_yticklabels(["","","","","","",""])
ax.set_zticks([1.75,1.25,0.75,0.25])
ax.set_zticklabels(['','','',''])

# change background colour to white
ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))

xlim = plt.gca().get_xlim()
ylim = plt.gca().get_ylim()
zlim = plt.gca().get_zlim()
# print(xlim,ylim,zlim)
# (0.0, 1.1) (6.0, 3.0) (0.0, 1.75)
ax.arrow3D(-0.03, ylim[0]+0.06, 0, xlim[1]+0.05, 0, 0, mutation_scale=20, arrowstyle='<|-|>',fc='k') # x axis
ax.arrow3D(-0.03, ylim[1], 0, 0, ylim[1]+0.1, 0, mutation_scale=20, arrowstyle='<|-|>', fc='k') # y axis
ax.arrow3D(-0.05, ylim[1], 0, 0, 0, zlim[1]+0.1, mutation_scale=20, arrowstyle='<|-|>', fc='k') # z axis

ax.text2D(0.05, 0.65,r'$\mathcal{Z}$', fontsize=18, ha='center', transform=ax.transAxes)
ax.text2D(0.60, -0.03,r'$\mathcal{Y}$', fontsize=18, ha='center', transform=ax.transAxes)
ax.text2D(0.95, 0.40,r'$\mathcal{X}$', fontsize=18, ha='center', transform=ax.transAxes)

plt.tick_params(axis='both', color='white')
#plt.savefig("sample.png", type="png",dbi=400) # save image 
# plt.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述