GradientLegend positioning

48 views
Skip to first unread message

Israel Brewster

unread,
May 29, 2019, 1:28:27 PM5/29/19
to pyqtgraph
When I add a legend to a graph using the plotItem.addLegend() function, I get a nice, stationary legend that doesn't move around as I move/scale the graph. This is great, but I need a color scale (I have a scatter plot with colored points, and I need to show a scale bar for the colors). To this end, I found the "GradientLegend" class, and managed to get it to show my color map and add it to the graph using the following code:

self._plot_widget = pg.PlotWidget()
self._plot_item = self._plot_widget.getPlotItem()
...
gl = pg.GradientLegend((10, 500), (10, 30))
gl.setGradient(self._color_map.getGradient())
gl.setParentItem(self._plot_item.getViewBox())

This works, but I have to questions/issues with it:

1) Unlike the legend added with addLegend, this "legend" is not stationary - it moves around as I move/scale the graph. Generally speaking, it moves back to the correct location more often than not, but the effect is that it "jiggles" around on the screen whenever I do something with the graph.And while, as I said, it "generally" ends up back where it is supposed to, this is not always the case - some times it winds up half off the screen, and I have to "bump" the graph somehow to get it back on. How can I fix this so it is truly stationary, in the same way a "formal" legend is?

2) Is there a way to set the size as a percentage of the window? At the moment if the user scales their window down, it often cuts off the legend

Assuming that at some level the GradientLegend item is/behaves like a normal QtWidget, I could easily just add it to a layout and set it to expanding, or at least have a minimum size constraint, so if that's possible, I can work with that. Thanks.

Israel Brewster

unread,
May 30, 2019, 2:13:18 PM5/30/19
to pyqtgraph
Never Mind. I realized that the getGradient() function on a pg.ColorMap object gave me a QLinearGradient object, so I just used that to create my own color scale in a separate widget, thus satisfying all my requirements.
Reply all
Reply to author
Forward
0 new messages