【问题标题】:Removing axes margins in 3D plot删除 3D 绘图中的轴边距
【发布时间】:2013-05-05 11:42:16
【问题描述】:

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

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

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

【问题讨论】:

  • 如果您可以添加用于制作绘图的代码,这将非常有帮助,因此我们有一个起点。这样人们就更容易复制粘贴代码,然后为这个特定问题找到解决方案。
  • 大量示例代码here(“条形图”示例与我上面的案例类似)。

标签: python matplotlib margins axes


【解决方案1】:

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

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

编辑2

设置 X 和 Y 限制:

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

【讨论】:

  • 谢谢,它成功了... (1) 如果我更改for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 2]),为什么'X' 轴会变成粗虚线样式? (2) 如何改变网格线的颜色? ax.grid(color='blue') 不起作用。
  • @dolphin,你能发布(1)的结果吗?对于(2),某些API无法修改颜色,我添加了更改隐藏的_axinfo dict的代码。
  • 感谢 (2)!遗憾的是,matplotlib 的复杂性和灵活性导致需要单独搜索如此多的“黑客”,而直观的解决方案有时有效,有时无效。关于(1),我无法在评论中发布图片,所以我粘贴了一段代码,希望您可以复制它:-) 现在我在原始问题中添加了另一张图片作为编辑。
  • @dolphin,(1)是因为轴线与网格线重叠,可以调用ax.set_ylim3d()改变Y轴范围来避免这种情况。
  • 确实如此。我认为这是一个错误,因为这是唯一被网格透支的轴,但也许它是这样设计/预期的......谢谢!
【解决方案2】:

我不得不稍微调整接受的解决方案,因为在我的例子中,x 和 y 轴(但不是 z)有一个额外的边距,通过打印 mins, maxs, deltas,结果是 deltas * 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 中按预期工作。)

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 2011-12-31
    • 2010-11-12
    • 2016-07-10
    相关资源
    最近更新 更多