From 7e09022e67caaa350466ed51dba825516e0ef06d Mon Sep 17 00:00:00 2001 From: MingZZZZZZZZ <32181145+ReehcQ@users.noreply.github.com> Date: Fri, 21 Dec 2018 01:45:00 -0500 Subject: [PATCH 1/3] add legend for bar graph items --- pyqtgraph/graphicsItems/LegendItem.py | 29 +++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/pyqtgraph/graphicsItems/LegendItem.py b/pyqtgraph/graphicsItems/LegendItem.py index 200820fc..91b6e9a3 100644 --- a/pyqtgraph/graphicsItems/LegendItem.py +++ b/pyqtgraph/graphicsItems/LegendItem.py @@ -147,29 +147,28 @@ class ItemSample(GraphicsWidget): return QtCore.QRectF(0, 0, 20, 20) def paint(self, p, *args): - #p.setRenderHint(p.Antialiasing) # only if the data is antialiased. + # p.setRenderHint(p.Antialiasing) # only if the data is antialiased. opts = self.item.opts - - if opts.get('fillLevel',None) is not None and opts.get('fillBrush',None) is not None: - p.setBrush(fn.mkBrush(opts['fillBrush'])) - p.setPen(fn.mkPen(None)) - p.drawPolygon(QtGui.QPolygonF([QtCore.QPointF(2,18), QtCore.QPointF(18,2), QtCore.QPointF(18,18)])) - + if not isinstance(self.item, ScatterPlotItem): p.setPen(fn.mkPen(opts['pen'])) p.drawLine(2, 18, 18, 2) - + + if opts.get('fillLevel', None) is not None and opts.get('fillBrush', None) is not None: + p.setBrush(fn.mkBrush(opts['fillBrush'])) + p.setPen(fn.mkPen(opts['fillBrush'])) + p.drawPolygon(QtGui.QPolygonF([QtCore.QPointF(2, 18), QtCore.QPointF(18, 2), QtCore.QPointF(18, 18)])) + symbol = opts.get('symbol', None) if symbol is not None: if isinstance(self.item, PlotDataItem): opts = self.item.scatter.opts - - pen = fn.mkPen(opts['pen']) - brush = fn.mkBrush(opts['brush']) - size = opts['size'] - - p.translate(10,10) - path = drawSymbol(p, symbol, size, pen, brush) + p.translate(10, 10) + drawSymbol(p, symbol, opts['size'], fn.mkPen(opts['pen']), fn.mkBrush(opts['brush'])) + + if isinstance(self.item, BarGraphItem): + p.setBrush(fn.mkBrush(opts['brush'])) + p.drawRect(QtCore.QRectF(2, 2, 18, 18)) From 3f93e30b312c8966b695fde07054c62f34f9896c Mon Sep 17 00:00:00 2001 From: MingZZZZZZZZ <32181145+ReehcQ@users.noreply.github.com> Date: Fri, 21 Dec 2018 01:47:23 -0500 Subject: [PATCH 2/3] Update Legend.py add legend examples for line plot, bar graph and scatter plot. --- examples/Legend.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/examples/Legend.py b/examples/Legend.py index f7841151..3759c2e9 100644 --- a/examples/Legend.py +++ b/examples/Legend.py @@ -7,17 +7,37 @@ import initExample ## Add path to library (just for examples; you do not need th import pyqtgraph as pg from pyqtgraph.Qt import QtCore, QtGui +import numpy as np -plt = pg.plot() -plt.setWindowTitle('pyqtgraph example: Legend') -plt.addLegend() -#l = pg.LegendItem((100,60), offset=(70,30)) # args are (size, offset) -#l.setParentItem(plt.graphicsItem()) # Note we do NOT call plt.addItem in this case +win = pg.plot() +win.setWindowTitle('pyqtgraph example: BarGraphItem') -c1 = plt.plot([1,3,2,4], pen='r', symbol='o', symbolPen='r', symbolBrush=0.5, name='red plot') -c2 = plt.plot([2,1,4,3], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='green plot') -#l.addItem(c1, 'red plot') -#l.addItem(c2, 'green plot') +# # option1: only for .plot(), following c1,c2 for example----------------------- +# win.addLegend() + +# bar graph +x = np.arange(10) +y = np.sin(x+2) * 3 +bg1 = pg.BarGraphItem(x=x, height=y, width=0.3, brush='b', pen='w', name='bar') +win.addItem(bg1) + +# curve +c1 = win.plot([np.random.randint(0,8) for i in range(10)], pen='r', symbol='t', symbolPen='r', symbolBrush='g', name='curve1') +c2 = win.plot([2,1,4,3,1,3,2,4,3,2], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='curve2') + +# scatter plot +s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120), name='scatter') +spots = [{'pos': [i, np.random.randint(-3, 3)], 'data': 1} for i in range(10)] +s1.addPoints(spots) +win.addItem(s1) + +# # option2: generic method------------------------------------------------ +legend = pg.LegendItem((80,60), offset=(70,20)) +legend.setParentItem(win.graphicsItem()) +legend.addItem(bg1, 'bar') +legend.addItem(c1, 'curve1') +legend.addItem(c2, 'curve2') +legend.addItem(s1, 'scatter') ## Start Qt event loop unless running in interactive mode or using pyside. From b773b020a51e720ab07cedc654fb642e23b9011a Mon Sep 17 00:00:00 2001 From: MingZZZZZZZZ <32181145+ReehcQ@users.noreply.github.com> Date: Mon, 25 Mar 2019 18:02:04 -0400 Subject: [PATCH 3/3] Update LegendItem.py --- pyqtgraph/graphicsItems/LegendItem.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyqtgraph/graphicsItems/LegendItem.py b/pyqtgraph/graphicsItems/LegendItem.py index 91b6e9a3..418c428f 100644 --- a/pyqtgraph/graphicsItems/LegendItem.py +++ b/pyqtgraph/graphicsItems/LegendItem.py @@ -6,6 +6,7 @@ from ..Point import Point from .ScatterPlotItem import ScatterPlotItem, drawSymbol from .PlotDataItem import PlotDataItem from .GraphicsWidgetAnchor import GraphicsWidgetAnchor +from .BarGraphItem import BarGraphItem __all__ = ['LegendItem'] class LegendItem(GraphicsWidget, GraphicsWidgetAnchor):