如何消除 3D 投影图中的偏移?
我意识到我用 matplotlib 制作的 3D 绘图有轻微的“错位”。这是 MWE:
import numpy as np
from matplotlib import pyplot as plt
figure = plt.figure(figsize=(8,10.7))
ax = plt.gca(projection='3d')
ax.plot_surface(np.array([[0, 0], [30, 30]]),
np.array([[10, 10], [10, 10]]),
np.array([[10, 20], [10, 20]]),
rstride=1, cstride=1
)
ax.plot_surface(np.array([[0, 0], [30, 30]]),
np.array([[20, 20], [20, 20]]),
np.array([[10, 20], [10, 20]]),
rstride=1, cstride=1
)
plt.show()
plt.close()
显然,箱没有正确居中,因为表面似乎从 10.5 开始并以 20.5 结束,而不是急剧的 10 和 20。如何才能实现后者呢?
编辑:恐怕建议的答案有问题。 x 轴没有黑色实线,默认情况下就是这样:
当我取出建议的包装时,我得到:
不幸的是,当我拿出我的东西时在绘图时,这个问题在 Jupyter 笔记本中无法重现,但尽管如此,我想知道您是否能够向我指出我必须做什么,以便在我的情况下,x 轴有一个黑色又线了?
I realized a slight "misalignment" of a plot I'm making in 3D with matplotlib. Here is an MWE:
import numpy as np
from matplotlib import pyplot as plt
figure = plt.figure(figsize=(8,10.7))
ax = plt.gca(projection='3d')
ax.plot_surface(np.array([[0, 0], [30, 30]]),
np.array([[10, 10], [10, 10]]),
np.array([[10, 20], [10, 20]]),
rstride=1, cstride=1
)
ax.plot_surface(np.array([[0, 0], [30, 30]]),
np.array([[20, 20], [20, 20]]),
np.array([[10, 20], [10, 20]]),
rstride=1, cstride=1
)
plt.show()
plt.close()
Clearly, the bins are not correctly centered, as the surfaces seem to start at 10.5 and end at 20.5 instead of 10 and 20 sharply. How could one achieve the latter?
EDIT: I'm afraid that there is an issue with the suggested answer. The x-axis does not have a solid black line, as is the case by default:
When I take out the suggested wrapping, I get:
Unfortunately, when I take out the stuff that I'm plotting, this issue is not reproducible in a Jupyter notebook, but nevertheless, I was wondering about whether you might be able to point out to me what I'd have to do so that in my case, the x-axis has a black line again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由 matplotlib 对 3D Axis 坐标的处理引起的。它故意改变
mins
和maxs
来创建一些人工填充:从 v3.5.1 开始,没有参数可以控制此行为。
但是,我们可以使用
functools.wraps
< /a> 创建一个围绕Axis._get_coord_info
的包装器,用于取消移动mins
和maxs
。为了防止此包装器多次恢复原位(例如,重新运行其 Jupyter 单元时),请通过跟踪包装器状态 >_unpadded
属性:在绘图之前应用
unpad
包装器:This is caused by matplotlib's processing of 3D
Axis
coordinates. It deliberately shiftsmins
andmaxs
to create some artificial padding:As of v3.5.1, there is no parameter to control this behavior.
However, we can use
functools.wraps
to create a wrapper aroundAxis._get_coord_info
that unshiftsmins
andmaxs
. To prevent this wrapper from unshifting multiple times (e.g., when rerunning its Jupyter cell), track the wrapper state via an_unpadded
attribute:Apply the
unpad
wrapper before plotting: