【问题标题】:matplotlib: `tight_layout()` didn't work in PyQt widgetmatplotlib:`tight_layout()` 在 PyQt 小部件中不起作用
【发布时间】:2015-09-10 08:33:29
【问题描述】:

我已经使用gridspec 构建了一个 PyQt 应用程序来显示三个子图:

class Mesh(FigureCanvas):

    def __init__(self, Max_i, Max_j, file='',
             mesh_string='', nkx=0, nky=0, factor=1.1, figsize=(12, 8),
             facecolor='none', dpi=100):

    self.figure = Figure(figsize=figsize, dpi=dpi, facecolor=facecolor)


    FigureCanvas.__init__(self, self.figure)

    ...
    gs = gridspec.GridSpec(3, 4)
    ax_main = self.figure.add_subplot(gs[:2, :])
    ax_dx = self.figure.add_subplot(gs[2, :2])
    ax_dy = self.figure.add_subplot(gs[2, 2:])
    gs.update(wspace=100)

    ax_dx.plot(Arr_x[: Max_i], Arr_dx, 'k-', lw=1.2)
    ax_dy.plot(Arr_y[: Max_j], Arr_dy, 'k-', lw=1.2)
    ax_dx.set_xlim(Arr_x[0], Arr_x[Max_i - 1])
    ax_dx.set_ylim(0, Arr_dx.max() * factor)
    ax_dy.set_xlim(Arr_y[0], Arr_y[Max_j - 1])
    ax_dy.set_ylim(0, Arr_dy.max() * factor)
    ax_dx.set_xlabel('x')
    ax_dx.set_ylabel('dx')
    ax_dy.set_xlabel('y')
    ax_dy.set_ylabel('dy')
    ...

    #set tight_layout here!
    gs.tight_layout(self.figure)

但是,不同轴的标签仍然相互重叠,没有任何变化,似乎tight_layout()Qt小部件中不起作用,那么还有另一种方法可以在Qt小部件中排列子图吗?

【问题讨论】:

    标签: python qt matplotlib pyqt5


    【解决方案1】:

    您可以使用subplots_adjust 更改绘图和图形边缘之间的间距:

    self.figure.subplots_adjust(left=0.1,right=0.9,
                                bottom=0.1,top=0.9,
                                hspace=0.2,wspace=0.2)
    

    其中leftrightbottomtop调整图形边缘的边距宽度,hspacewspace分别改变绘图之间的垂直和水平间距.

    所以,听起来您需要增加您的hspacewspace。可能需要进行一些试验才能找到适合您身材的配置。

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 2021-01-16
      • 2022-06-10
      • 2012-01-05
      • 1970-01-01
      • 2013-05-08
      相关资源
      最近更新 更多