Python 3d图-轴居中

问题描述 投票:1回答:1

我正在尝试在python中绘制3d图,其中轴位于中间,如下所示:

enter image description here

我已经尝试过使用matplotlib,但失败了。只是为了说明我的问题,我不希望出现如下图所示的图,您可以在其中看到轴位于数据之外:

enter image description here

python matplotlib plot 3d plotly
1个回答
0
投票

我也找不到任何方法,所以这是我的解决方法:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.set_aspect('equal') 

# Draw centered axes
val = [1,0,0]
labels = ['x', 'y', 'z']
colors = ['r', 'g', 'b']
for v in range(3):
    x = [val[v-0], -val[v-0]]
    y = [val[v-1], -val[v-1]]
    z = [val[v-2], -val[v-2]]
    ax.plot(x,y,z,'k-', linewidth=1)
    ax.text(val[v-0], val[v-1], val[v-2], labels[v], color=colors[v], fontsize=20)


# Hide everything else
# Hide axes ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])
# make the panes transparent
ax.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
ax.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# Hide box axes
ax._axis3don = False

# Expand to remove white space
ax.set_xlim(np.array([-1,1])*.57)
ax.set_ylim(np.array([-1,1])*.57)
ax.set_zlim(np.array([-1,1])*.57)

plt.tight_layout()
plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.