【问题标题】:PyQt4 and matplotlib: Legend outside of the plot is cut offPyQt4和matplotlib:图外的图例被截断
【发布时间】:2014-12-08 19:36:24
【问题描述】:

我在谷歌上搜索了很多,但我没有找到解决方案。 问题和这里一样:Moving matplotlib legend outside of the axis makes it cutoff by the figure box 但我不想保存图形,我只想在整个图形中有图例。我也尝试过tight_layout,但没有奏效。我对 matplotlib 完全陌生,无法弄清楚。这是我的源代码(我将 matplot 嵌入到 PyQt4 中):

class GraphACanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=5, dpi=100):        
        self.fig = Figure(figsize=(width, height), facecolor=backColorHexName, dpi=dpi)
        self.axes = self.fig.add_subplot(1, 1, 1, autoscale_on=False)

        self.axes.set_xticks(arange(-0.1, 1.4, 0.1))
        self.axes.set_yticks(arange(-0.1, 1.4, 0.1))

        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QtGui.QSizePolicy.Expanding, Gui.QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

def computeFigure(self):
    col = []
    for i in xrange(0, 100):
        x = i/100
        y = y/100
        line, = self.axes.plot(x, y, 'o', color=blue, label='label')
        col.append(line.get_c())

    self.axes.set_xlabel('x', labelpad=10, rotation=0)
    self.axes.set_ylabel('y', labelpad=10, rotation=0)

    leg = self.axes.legend(loc=2, bbox_to_anchor=(1.0, 0.5), borderaxespad=0., fontsize='x-small', framealpha=0.25, markerscale=0.5, ncol=1, numpoints=1)

    for color,text in zip(col,leg.get_texts()):
        text.set_color(color)

test = GraphACanvas()
test.computeFigure()

我在这里输入的只是虚拟值。但是在应用程序中,用户可以选择节点,根据他选择的节点数量,更大/更小是图例。我想缩小 x 轴侧以便为图例留出更多位置。 --> subplot 不应该填写图形的 100%(宽/高),而是 100% 的高度和 80% 的宽度,这样我的图例就有足够的空间。

【问题讨论】:

    标签: matplotlib pyqt4 embedding


    【解决方案1】:

    这一行:

    self.axes = self.fig.add_subplot(1, 1, 1, autoscale_on=False)

    将按照您的描述创建一个填充整个图形(或多或少)的轴。

    试试:

    self.axes = self.fig.add_axes([0.1, 0.1, 0.7, 0.8])

    该列表的元素是:

    [left_edge, bottom_edge, width, height] 其中所有值都是图形总尺寸的分数。

    【讨论】:

    • 谢谢 :D 我会试试的。我还找到了一个解决方案:figure.subplot_adjust(right=0.2)。
    猜你喜欢
    • 2012-04-23
    • 2021-12-16
    • 1970-01-01
    • 2017-02-11
    • 2016-05-06
    • 1970-01-01
    • 2023-02-24
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多