Python 删除三维打印中的轴边距

Python 删除三维打印中的轴边距,python,matplotlib,margins,axes,Python,Matplotlib,Margins,Axes,我花了几天时间试图找到一种方法,在3D绘图中删除轴上的微小边距。我尝试了ax.margins(0)和ax.autoscale\u view('tight')和其他方法,但这些小的边距仍然存在。特别是,我不喜欢条形柱状图被抬高,也就是说,它们的底部不在零级——参见示例图像 在gnuplot中,我将使用“将xyplane设置为0”。在matplotlib中,由于两侧的每个轴上都有边距,因此能够控制每个轴将是非常棒的 编辑: HYRY下面的解决方案运行良好,但“X”轴在Y=0时在其上绘制了一条网格

我花了几天时间试图找到一种方法,在3D绘图中删除轴上的微小边距。我尝试了
ax.margins(0)
ax.autoscale\u view('tight')
和其他方法,但这些小的边距仍然存在。特别是,我不喜欢条形柱状图被抬高,也就是说,它们的底部不在零级——参见示例图像

在gnuplot中,我将使用“将xyplane设置为0”。在matplotlib中,由于两侧的每个轴上都有边距,因此能够控制每个轴将是非常棒的

编辑: HYRY下面的解决方案运行良好,但“X”轴在Y=0时在其上绘制了一条网格线:


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

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()
结果是:

编辑

要更改网格线的颜色,请执行以下操作:

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

设置X&Y直线度:

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

我不得不稍微调整已接受的解决方案,因为在我的例子中,x轴和y轴(而不是z轴)有一个额外的边距,通过打印
mins、maxs、delta
,结果是
delta*6.0/11
。以下是在我的案例中运行良好的更新补丁

###patch start###
from mpl_toolkits.mplot3d.axis3d import Axis
def _get_coord_info_new(self, renderer):
    mins, maxs, cs, deltas, tc, highs = self._get_coord_info_old(renderer)
    correction = deltas * [1.0/4 + 6.0/11,
                           1.0/4 + 6.0/11,
                           1.0/4]
    mins += correction
    maxs -= correction
    return mins, maxs, cs, deltas, tc, highs
if not hasattr(Axis, "_get_coord_info_old"):
    Axis._get_coord_info_old = Axis._get_coord_info  
Axis._get_coord_info = _get_coord_info_new
###patch end###

(我还改变了一点补丁逻辑,这样编辑函数并重新加载其模块现在就可以在Jupyter中正常工作了。)

如果您可以添加用于绘制图的代码,这将非常有帮助,因此我们有了一个起点。这样人们就更容易复制粘贴代码,然后找到解决这个特定问题的方法。很多示例代码(“条形图”示例与我上面的示例类似)。谢谢,它很有效。。。(1) 如果我在zip(['r','g','b','y'],[30,20,10,2])中更改c,z的
,为什么“X”轴会有粗虚线样式?(2) 如何更改网格线的颜色
ax.grid(color='blue')
不起作用。@dolphin,你能发布(1)的结果吗?对于(2),颜色不能被某些API修改,我添加了更改隐藏的_axinfodict的代码。感谢(2)!令人遗憾的是,matplotlib的复杂性和灵活性导致了许多需要单独搜索的“黑客”,而直观的解决方案有时有效,有时无效。关于(1),我无法在评论中发布图像,因此我粘贴了一段代码,希望您能够复制它:-)现在我在原始问题中添加了另一个图像作为编辑。@dolphin,(1)因为轴线与网格线重叠,您可以调用
ax.set_ylim3d()
来更改Y轴范围以避免出现这种情况。确实如此。我认为这是一个错误,因为这是唯一的轴透支的网格,但也许它是设计/预期的方式。。。非常感谢。