Python 3d 绘图 - 轴居中 [英] Python 3d plot - axis centered

查看:48
本文介绍了Python 3d 绘图 - 轴居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 中制作一个中间有轴的 3d 图,如下所示:

I'm trying to make a 3d plot in python with axis in the middle like so:

我已经尝试过使用 matplotlib 并没有成功.只是为了说明我的问题,我不想要如下图所示的图,您可以在其中看到轴位于数据之外:

I've tried with matplotlib and plotly without success. Just to specify my problem, i don't want a plot like the following image, where you can see the axis are outside the data:

推荐答案

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

I could not find a way to do this either, so this is my work around:

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

这篇关于Python 3d 绘图 - 轴居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆