如何隐藏 3d 绘图的轴

发布于 2024-12-03 10:00:48 字数 393 浏览 1 评论 0原文

如何在不显示轴的情况下绘制 3D 绘图?

绘制 3D 图时,Matplotlib 不仅绘制 x、y 和 z 轴,还在 xy、yz 和 xz 平面上绘制浅灰色网格。我想绘制一个“自由浮动”的 3D 绘图,没有这些元素。

我尝试过的东西:

# Doesn't work; this hides the plot, not the axes
my_3d_axes.set_visible(False)

# Doesn't do anything. Also, there's no get_zaxis() function.
my_3d_axes.get_xaxis().set_visible(False)
my_3d_axes.get_yaxis().set_visible(False)

How can I make a 3D plot without showing the axes?

When plotting a 3d plot, Matplotlib not only draws the x, y, and z axes, it draws light gray grids on the x-y, y-z, and x-z planes. I would like to draw a "free-floating" 3D plot, with none of these elements.

Stuff I've tried:

# Doesn't work; this hides the plot, not the axes
my_3d_axes.set_visible(False)

# Doesn't do anything. Also, there's no get_zaxis() function.
my_3d_axes.get_xaxis().set_visible(False)
my_3d_axes.get_yaxis().set_visible(False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染墨丶若流云 2024-12-10 10:00:48

Ben Root 提供了一个补丁来修复 1.0.1 的这个问题。它可以作为此主题的最后一封电子邮件的附件找到。引用本的话:

好吧,看起来隐藏 3d 轴是 v1.0 版本之后添加的功能(但在我开始使用 mplot3d 之前)。该补丁应该启用基本功能而不干扰现有功能。要隐藏轴,您必须将私有成员“_axis3don”设置为 False,如下所示:

ax = plt.gca(投影='3d')
ax._axis3don = False

如果你这样做,那么你现在就会得到你想要的,并且当你升级时你的代码仍然与 mplot3d 兼容(尽管首选方法是调用 set_axis_on() 或 set_axis_off())。< /p>

希望对您有所帮助!

本·鲁特

Ben Root provided a patch that fixes this for 1.0.1. It can be found as an attachment to the last email of this thread. To quote Ben:

Ok, looks like the hiding of the 3d axes was a feature added after the v1.0 release (but before I started working on mplot3d). This patch should enable the basic feature without interfering with existing functions. To hide the axes, you would have to set the private member "_axis3don" to False, like so:

ax = plt.gca(projection='3d')
ax._axis3don = False

If you do it this way, then you will get what you want now, and your code will still be compatible with mplot3d when you upgrade (although the preferred method would be to call set_axis_on() or set_axis_off()).

I hope that helps!

Ben Root

攒眉千度 2024-12-10 10:00:48

ax.set_axis_off()

只是为了提供 https://stackoverflow.com/a/7363931/895245

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d

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

# Draw a circle on the x=0 'wall'
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="x")
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="z")

ax.set_xlim(-1.2, 1.2)
ax.set_ylim(-1.2, 1.2)
ax.set_zlim(-1.2, 1.2)

plt.savefig('main.png', format='png', bbox_inches='tight')

输出:

在此处输入图像描述

不带 ax.set_axis_off() 它看起来像:

在此处输入图像描述

但是,您会注意到,这会在图形周围产生过大的空白边距,因为它只是隐藏轴但不会更改视图框。我尝试了 bbox_inches='tight'它不像在 2D 中那样有帮助。如何解决这个问题:删除 Axes3d (matplotlib) 中的空格

已测试matplotlib==3.2.2。

ax.set_axis_off()

Just to provide a concrete and direct example of what was mentioned at https://stackoverflow.com/a/7363931/895245

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import mpl_toolkits.mplot3d.art3d as art3d

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

# Draw a circle on the x=0 'wall'
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="x")
p = Circle((0, 0), 1, fill=False)
ax.add_patch(p)
art3d.pathpatch_2d_to_3d(p, zdir="z")

ax.set_xlim(-1.2, 1.2)
ax.set_ylim(-1.2, 1.2)
ax.set_zlim(-1.2, 1.2)

plt.savefig('main.png', format='png', bbox_inches='tight')

Output:

enter image description here

Without ax.set_axis_off() it would look like:

enter image description here

You will notice however that this produces an excessively large whitespace margin around the figure as it simply hides the axes but does not change the viewbox. I tried bbox_inches='tight' and it did not help as it does in 2D. How to solve that at: Remove white spaces in Axes3d (matplotlib)

Tested on matplotlib==3.2.2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文