如何使图形填满所有窗口 [英] how to make a graph fill all the window

查看:34
本文介绍了如何使图形填满所有窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用 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 (plots/subplots/etc).一切都绘制在图形的 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 接口自动生成 FigureAxes,和 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).

这篇关于如何使图形填满所有窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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