问题描述
我正在使用 QtDesigner 创建的应用程序中绘制图形,问题是,当显示图形时,图形空间和 mplwidget 空间之间会出现一个大的灰色边缘".这会使绘图变小,那么当我在主窗口中显示我的图表时,我该如何删除这个大的灰色边框"??
I am ploting a graph in an application created with QtDesigner, the problem is that, when the grapth is showed, a big "grey edge" appears between the graph space and the mplwidget space. That makes the plot smaller, so how could I delete this big "grey border" that appears when I show my graph in the main Window??
我希望我的图表填充小部件的所有可用空间.
I would like my graph to fill all the available space for the widget.
推荐答案
简短的回答是:使用 fig.tight_layout()
."
The short answer is: "Use fig.tight_layout()
."
不过,让我对发生的事情进行更多解释.
Let me give a bit more explanation about what's going on, though.
您会看到图形和轴之间的交互.
You're seeing the interaction between the figure and the axes.
Figure
包含一个或多个 Axes
(绘图/子绘图/等).一切都绘制在图形的 Canvas
上(基本上是后端特定的像素缓冲区或矢量页面).
A Figure
contains one or more Axes
(plots/subplots/etc). Everything is drawn on the figure's Canvas
(basically, the backend-specific pixel buffer or vector page).
当你制作一个轴时,它不会填满整个图形.
When you make an axes, it does not fill up all of the figure.
默认情况下,轴的左下角为图形宽度的 12.5% 和高度的 10%,轴占据图形宽度和高度的 90%图.(不对称是为左边的刻度标签留出空间.)
The default for a single axes is for the lower left corner of the axes to be a 12.5% of the width of the figure and 10% of the height and for the axes to take up 90% of the width and height of the figure. (The asymmetry is to leave room for the tick labels on the left.)
你设置的位置是下图中白框的范围.它不包括刻度标签、标题、坐标轴标签等(这就是坐标轴没有填满整个图形的原因).
The position you set is the extent of the white box in the figure below. It doesn't include the tick labels, title, axes labels, etc (which is why the axes doesn't fill up the entire figure).
默认值如下所示:
旁注:为了保持代码简短,我将使用pyplot
接口自动生成一个Figure
,Axes
,和 Canvas
,而您可能明确地创建了每一个以使用您的 gui 框架.不过结果是一样的.
Side note: To keep the code short, I'm going to use the pyplot
interface to automatically generate a Figure
, Axes
, and Canvas
, while you're probably explicitly creating each one to work with your gui framework. The result is the same, though.
每个坐标区实例占用的图形百分比是在创建时设置的.您可以明确指定它:
The percentage of the figure that each axes instance takes up is set at the time that it's created. You can either explicitly specify it:
fig = plt.figure()
ax = fig.add_axes([left, bottom, width, height])
或者使用子图的网格,通过fig.subplots_adjust
可以更容易调整:
Or use a grid of subplots, which can be easier to adjust through fig.subplots_adjust
:
fig, axes = plt.subplots(nrows=2, ncols=2)
# Expand the grid of subplots out (Notice that
fig.subplots_adjust(left=0.05, bottom=0.05, right=0.98, top=0.98)
tight_layout
所做的是计算刻度标签、标题、轴标签等的范围,并确定 fig.subplots_adjust
的参数,这样一切都会勉强图里面.(请记住,subplots_adjust
和轴位置规范控制白框"的范围——实际轴本身——并且不包括刻度标签等)
What tight_layout
does is to calculate the extent of the tick labels, title, axis labels, etc and determine parameters for fig.subplots_adjust
such that everything will be just barely inside the figure. (Remember that subplots_adjust
and the axes position specification control the extent of the "white box" -- the actual axes itself -- and doesn't include the tick labels, etc.)
所以,如果我们像以前那样做:
So, if we do just what we did before:
fig, ax = plt.subplots()
然后调用:
fig.tight_layout()
我们会得到一些边框"较少的东西,因为轴将占据图形的更大百分比:
We'll get something that has less of a "border", as the axes will take up a larger percentage of the figure:
如果要控制边框"的颜色,请使用 fig.set_facecolor(color)
(或 fig.patch.set_facecolor
).
If you want to control the color of the "border" use fig.set_facecolor(color)
(or fig.patch.set_facecolor
).
这篇关于如何使图形填满整个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!