问题描述
如何扩展绘图区域以占据整个画布并从根本上去除图像中的灰色区域?我环顾四周,但没有找到任何解决方案,尽管可能是因为我不知道该区域的名称,所以搜索非常困难.
使用 fig.tight_layout()
减少灰色但不会删除它.
from matplotlib import pyplot as plty = [1,3,5,6,7,8,12,13,15,15,16,25,26,28,29,36]x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]无花果,ax = plt.subplots(figsize =(8,4),dpi = 85)ax.stackplot(x, y, color='w', colors=('#ededed',))ax.xaxis.set_visible(False)ax.yaxis.set_visible(False)ax.set_ylim([0,max(y)*1.2])ax.set_xlim([0,30])plt.show()
指定创建 Axes 时,
Axes
占据了整个 Figure
.代码>-
fig = plt.figure()ax = fig.add_axes((0,0,1,1), frameon=False)ax.plot([2,5,4])
现在,很多地方都没有空间了,例如外部刻度标签;Tkinter可能会自己做一些填充操作;但这与您可以制作
Axes
一样大.( (0,0,1,1)
是图形坐标中的范围.)
How can I extend the plot area to take up the whole canvas and essentially remove the gray area in the image? I've looked around and I haven't found any solutions, although maybe it's because I don't know what to call that area so searching is challenging.
Using
fig.tight_layout()
reduces the gray but does not remove it.
from matplotlib import pyplot as plt
y = [1,3,5,6,7,8,12,13,15,15,16,25,26,28,29,36]
x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
fig, ax = plt.subplots(figsize=(8,4), dpi=85)
ax.stackplot(x, y, color='w', colors=('#ededed',))
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
ax.set_ylim([0,max(y)*1.2])
ax.set_xlim([0,30])
plt.show()
解决方案
Specify that the
Axes
take up the whole Figure
when you create the Axes
--
fig = plt.figure()
ax = fig.add_axes((0,0,1,1), frameon=False)
ax.plot([2,5,4])
now, there's a lot there isn't room for any more, e.g. external tick labels; and Tkinter may be doing some padding itself; but this is as large as you can make an
Axes
. ( (0,0,1,1)
is extent in Figure coordinates.)
这篇关于在 Matplotlib 中隐藏轴外的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!