Search code examples
pythonqtpyqt5pyqtgraph

How to change alignment of PyQtGraph legend?


I am trying to make a good-looking graph inside my interface and due to this, the legend has to be on a right side of the widget. Is it possible to do this?

As a default, the legend appears sticking to the left axis and only way I found is to set the offset parameter, calculating position relative to that axis. But having to get correct offset value every time I build a window and resize it would be painful. Thank you for any advise.


Solution

  • According to the docs of LegendItem: https://pyqtgraph.readthedocs.io/en/latest/api_reference/graphicsItems/legenditem.html

    For the offset argument:

    Positive values offset from the left or top; negative values offset from the right or bottom.

    You can use small negative value like -1 as your offset and then the legend should be aligned to the right axis.