I figured out how to do this.  I wrote a new class inheriting the 
LegendItem class and overriding two functions. in paint()  I changed 
color/opacity of the background and boarder and in addItem() I changed  the 
label color to change the font color.

Then to add to plot use:

self.legend1 = myLegend(offset=(-100,50))
self.legend1.setParentItem(self.plt1.graphicsItem())


Where self.plt1 is a plotWidget

Here is the code:

from pyqtgraph import LegendItem, PlotDataItem, ScatterPlotItem, LabelItem
from pyqtgraph.Qt import QtCore, QtGui
from pyqtgraph.graphicsItems.ScatterPlotItem import drawSymbol
from pyqtgraph.graphicsItems.LegendItem import ItemSample
import pyqtgraph.functions as fn

class myLegend(LegendItem):
    def __init__(self, size=None, offset=None):
        LegendItem.__init__(self, size, offset)

    def paint(self, p, *args):
        p.setPen(fn.mkPen(0,0,0)) # outline
        p.setBrush(fn.mkBrush(255,255,255))   # background
        p.drawRect(self.boundingRect())

    def addItem(self, item, name):
        """
        Add a new entry to the legend.

        ==============  ========================================================
        **Arguments:**
        item            A PlotDataItem from which the line and point style
                        of the item will be determined or an instance of
                        ItemSample (or a subclass), allowing the item display
                        to be customized.
        title           The title to display for this item. Simple HTML allowed.
        ==============  ========================================================
        """
        label = LabelItem(name, color=(0,0,0))
        if isinstance(item, ItemSample):
            sample = item
        else:
            sample = ItemSample(item)
        row = self.layout.rowCount()
        self.items.append((sample, label))
        self.layout.addItem(sample, row, 0)
        self.layout.addItem(label, row, 1)
        self.updateSize()




On Monday, July 17, 2017 at 3:04:37 PM UTC-6, Patrick Kreitzberg wrote:
>
> Hi,
>
> I want to change the plot legend to have a solid white background, is this 
> possible?
>
> Thanks
>

-- 
You received this message because you are subscribed to the Google Groups 
"pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pyqtgraph/f6413835-b5df-4a15-9533-955e011170d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to