在3D图中删除轴边距 [英] Removing axes margins in 3D plot

查看:142
本文介绍了在3D图中删除轴边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天,我试图找到一种方法来消除3D图中轴上的微小空白.我尝试了ax.margins(0)ax.autoscale_view('tight')以及其他方法,但是这些小的空白仍然存在.特别是,我不喜欢条形图的柱状图升高,即它们的底部不在零水平-参见示例图像.

I spent last few days trying to find a way to remove tiny margins from axes in a 3D plot. I tried ax.margins(0) and ax.autoscale_view('tight') and other approaches, but these small margins are still there. In particular, I don't like that the bar histograms are elevated, i.e., their bottom is not at the zero level -- see example image.

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

In gnuplot, I would use "set xyplane at 0". In matplotlib, since there are margins on every axis on both sides, it would be great to be able to control each of them.

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

HYRY's solution below works well, but the 'X' axis gets a grid line drawn over it at Y=0:

推荐答案

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

There is not property or method that can modify this margins. You need to patch the source code. Here is an example:

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()

结果是:

修改

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

To change the color of the grid lines:

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

Edit2

设置X&林:

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

这篇关于在3D图中删除轴边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆