gpt4 book ai didi

python - 删除 3D 绘图中的轴边距

转载 作者:太空狗 更新时间:2023-10-29 18:32:57 29 4
gpt4 key购买 nike

我花了几天时间试图找到一种方法来去除 3D 绘图中轴的微小边距。我尝试了 ax.margins(0)ax.autoscale_view('tight') 和其他方法,但这些小边距仍然存在。特别是,我不喜欢柱状图被抬高,即它们的底部不在零水平——参见示例图像。

unwanted margins on all axes

在 gnuplot 中,我会使用“将 xyplane 设置为 0”。在 matplotlib 中,由于两侧的每个轴上都有边距,如果能够控制它们中的每一个就太好了。

编辑:HYRY 下面的解决方案效果很好,但“X”轴在 Y=0 处绘制了一条网格线:

strange axis

最佳答案

没有可以修改此边距的属性或方法。您需要修补源代码。这是一个例子:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
###patch start###
from mpl_toolkits.mplot3d.axis3d import Axis
if not hasattr(Axis, "_get_coord_info_old"):
def _get_coord_info_new(self, renderer):
mins, maxs, centers, deltas, tc, highs = self._get_coord_info_old(renderer)
mins += deltas / 4
maxs -= deltas / 4
return mins, maxs, centers, deltas, tc, highs
Axis._get_coord_info_old = Axis._get_coord_info
Axis._get_coord_info = _get_coord_info_new
###patch end###

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)

# You can provide either a single color or an array. To demonstrate this,
# the first bar of each set will be colored cyan.
cs = [c] * len(xs)
cs[0] = 'c'
ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

结果是:

enter image description here

编辑

改变网格线的颜色:

for axis in (ax.xaxis, ax.yaxis, ax.zaxis):
axis._axinfo['grid']['color'] = 0.7, 1.0, 0.7, 1.0

编辑2

设置 X 和 Y 限制:

ax.set_ylim3d(-1, 31)
ax.set_xlim3d(-1, 21)

关于python - 删除 3D 绘图中的轴边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16488182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com