我想在活动绘图期间完全删除我的坐标轴周围的空白(而不是其他人要求的save_fig )。这里我们不能使用bbox_inches='tight'
。我可以使用tight_layout(pad=0)。当轴处于启用状态时,它可以正常工作,它会显示所有刻度和x-y标签。
但是,在某些情况下,我将轴设置为off。我所期望的是看到内容扩展以填满轴所在的空白区域。然而,这是行不通的。它仍然保留填充,因为仍然有x-y标签和轴。
如何删除不可见轴对象的空白?
编辑:
我知道我可以使用ax.set_yticks([])
和ax.set_xticks([])
来关闭它们。但这很笨拙,在清除它们之前,我必须记住它们。如果我去掉-然后-添加那些勾号。记号不能再自动更新。我想知道有没有更直接的方法来做这件事?
我们仍然可以看到,即使在删除所有刻度之后,仍然有一个很小的边框间距。如果有人能想出一种方法来移除它的话。这将是非常棒的。
如果有的话,我也想保留这个头衔。因此,硬编码的ax.set_position([0,0,1,x])
对于这种用法并不是很好。当然,当有标题时,我们仍然可以尝试获取顶部间距,但如果有人能提供更直接/更简单的方法来处理这一点,那将是首选。
示例代码:
def demo_tight_layout(w=10, h=6, axisoff=False, removeticks=False):
fig,ax = plt.subplots()
fig.set_facecolor((0.8, 0.8, 0.8))
rect = patches.Rectangle((-w/2, -h/2), w, h, color='#00ffff', alpha=0.5)
ax.add_patch(rect)
ax.plot([-w/2,w/2], [-h/2,h/2])
ax.plot([-w/2,w/2], [h/2,-h/2])
ax.set_ylabel("ylabel")
ax.margins(0)
_texts = []
if axisoff:
ax.set_axis_off()
_texts.append("axisoff")
if removeticks:
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylabel("")
_texts.append("removeticks")
fig.text(0.5, 0.6, " ".join(_texts))
fig.tight_layout(pad=0)
plt.show()
return fig, ax, text
发布于 2018-05-28 19:01:35
您可以根据是否关闭轴来调整子图参数。
import matplotlib.pyplot as plt
from matplotlib import patches
def demo_tight_layout(w=10, h=6, axisoff=False):
fig,ax = plt.subplots()
fig.set_facecolor((0.8, 0.8, 0.8))
rect = patches.Rectangle((-w/2, -h/2), w, h, color='#00ffff', alpha=0.5)
ax.add_patch(rect)
ax.plot([-w/2,w/2], [-h/2,h/2])
ax.plot([-w/2,w/2], [h/2,-h/2])
ax.set_ylabel("ylabel")
ax.margins(0)
_texts = []
fig.tight_layout()
if axisoff:
ax.set_axis_off()
_texts.append("axisoff")
params = dict(bottom=0, left=0, right=1)
if ax.get_title() == "":
params.update(top=1)
fig.subplots_adjust(**params)
fig.text(0.5, 0.6, " ".join(_texts))
plt.show()
现在demo_tight_layout(axisoff=True)
生产
和demo_tight_layout(axisoff=False)
生产的
发布于 2018-05-28 18:27:20
您需要设置轴位置以填充该图。如果您使用创建图形和绘图
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca()
ax.plot(some_x_data, some_y_data)
您需要添加以下行,以便使用轴填充该图:
ax.set_position([0, 0, 1, 1], which='both')
这将通过以下方式设置相对于图形大小的轴位置:
[left, bottom, width, height]
因此,要完全填充该图,请使用如上所示的[0, 0, 1, 1]
。
所以在你的代码中,应该是这样的(使用fill_figure
bool来检查):
def demo_tight_layout(w=10, h=6, axisoff=False, removeticks=False, fill_figure=False):
fig,ax = plt.subplots()
fig.set_facecolor((0.8, 0.8, 0.8))
rect = patches.Rectangle((-w/2, -h/2), w, h, color='#00ffff', alpha=0.5)
ax.add_patch(rect)
ax.plot([-w/2,w/2], [-h/2,h/2])
ax.plot([-w/2,w/2], [h/2,-h/2])
ax.set_ylabel("ylabel")
ax.margins(0)
_texts = []
if axisoff:
ax.set_axis_off()
_texts.append("axisoff")
if removeticks:
ax.set_xticks([])
ax.set_yticks([])
ax.set_ylabel("")
_texts.append("removeticks")
fig.text(0.5, 0.6, " ".join(_texts))
fig.tight_layout(pad=0)
if fill_figure:
ax.set_position([0, 0, 1, 1], which='both')
plt.show()
return fig, ax, text
ax.set_position
的目标是fig.tight_layout
。如果需要图形标题,则没有直接的方法。不幸的是,这是无法避免的。您需要手动调整height
参数,以便标题适合图,例如:
ax.set_position([0, 0, 1, .9], which='both')
https://stackoverflow.com/questions/50562430
复制相似问题