LegendItem covering the plot

3,825 views
Skip to first unread message

jure.b...@gmail.com

unread,
Apr 7, 2014, 6:57:50 AM4/7/14
to pyqt...@googlegroups.com
Hi,

Is it possible to have a LegendItem on the side (for example to the right of the plot), instead of inside the plot? Sometimes it covers up some spots on the plot and it can be moved by user, but I would prefer if it could be just drawn outside of the plot altogether...

Cheers,
Jure

Luke Campagnola

unread,
Apr 8, 2014, 3:41:52 PM4/8/14
to pyqt...@googlegroups.com
On Mon, Apr 7, 2014 at 6:57 AM, <jure.b...@gmail.com> wrote:
Is it possible to have a LegendItem on the side (for example to the right of the plot), instead of inside the plot? Sometimes it covers up some spots on the plot and it can be moved by user, but I would prefer if it could be just drawn outside of the plot altogether...

Good question.. the first thing that comes to mind is that you can use an empty ViewBox to create space next to the plot, and add the legend there:

import pyqtgraph as pg
win = pg.GraphicsWindow()
win.addPlot()
vb = win.addViewBox()
vb.setMaximumWidth(100)
legend = pg.LegendItem()
vb.addItem(legend)

In the future, I'd like to make LegendItem a proper subclass of QGraphicsWidget so that it can be added directly to the layout without a ViewBox to hold space for it.
 

jure.b...@gmail.com

unread,
Apr 10, 2014, 11:06:25 AM4/10/14
to pyqt...@googlegroups.com
Hi Luke,

The code below displays LegendItem on the the side, extending the example you have given. How can the legend be moved to the right top corner of the window? Right now, it shows on the bottom right, below the bottom axis, and is not fully visible? It seems to me, I don't understand Qt enough to make it work...

My last try was like this: (it is also in the code below, commented out)
legend.setPos(legend.mapFromItem(legend, QtCore.QPointF(675,38)))
but unfortunatelly, it does not move the legend...


from PyQt4 import QtGui
import pyqtgraph as pg
from PyQt4 import QtCore

win = pg.GraphicsWindow()
plot = win.addPlot()

curve1 = plot.plot([1,3,2,4], pen='r', name='red plot')
curve2 = plot.plot([1, 2, 2.5], [3, 4, 3], pen='g', name='green plot')

vb = win.addViewBox()
legend = pg.LegendItem()
vb.addItem(legend)

legend.addItem(curve1, name=curve1.opts['name'])
legend.addItem(curve2, name=curve2.opts['name'])
## legend.setPos(legend.mapFromItem(legend, QtCore.QPointF(675,38)))

vb.setMaximumWidth(legend.boundingRect().width() + 10) # get legend.boundingRect() after items have been added


if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

Thanks,
Jure

Luke Campagnola

unread,
Apr 10, 2014, 12:08:44 PM4/10/14
to pyqt...@googlegroups.com
On Thu, Apr 10, 2014 at 11:06 AM, <jure.b...@gmail.com> wrote:
Hi Luke,

The code below displays LegendItem on the the side, extending the example you have given. How can the legend be moved to the right top corner of the window? Right now, it shows on the bottom right, below the bottom axis, and is not fully visible? It seems to me, I don't understand Qt enough to make it work...

LegendItem uses anchoring to make sure that it maintains some position relative to its parent, even after the parent resizes. Here's the example, updated to use LegendItem.anchor(). Please note that I also fixed a related issue--we need to use legend.setParentItem rather than vb.addItem for anchoring to work properly.

import pyqtgraph as pg
win = pg.GraphicsWindow()
win.addPlot()
vb = win.addViewBox()
vb.setMaximumWidth(100)
legend = pg.LegendItem()
legend.setParentItem(vb)
# Anchor the upper-left corner of the legend to the upper-left corner of its parent
legend.anchor((0,0), (0,0))



Reply all
Reply to author
Forward
0 new messages