Merge pull request #800 from ReehcQ/develop
add legend for bar graph and update the examples
This commit is contained in:
commit
dcef886a44
@ -7,17 +7,37 @@ import initExample ## Add path to library (just for examples; you do not need th
|
|||||||
|
|
||||||
import pyqtgraph as pg
|
import pyqtgraph as pg
|
||||||
from pyqtgraph.Qt import QtCore, QtGui
|
from pyqtgraph.Qt import QtCore, QtGui
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
plt = pg.plot()
|
win = pg.plot()
|
||||||
plt.setWindowTitle('pyqtgraph example: Legend')
|
win.setWindowTitle('pyqtgraph example: BarGraphItem')
|
||||||
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
|
|
||||||
|
|
||||||
c1 = plt.plot([1,3,2,4], pen='r', symbol='o', symbolPen='r', symbolBrush=0.5, name='red plot')
|
# # option1: only for .plot(), following c1,c2 for example-----------------------
|
||||||
c2 = plt.plot([2,1,4,3], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='green plot')
|
# win.addLegend()
|
||||||
#l.addItem(c1, 'red plot')
|
|
||||||
#l.addItem(c2, 'green plot')
|
# 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.
|
## Start Qt event loop unless running in interactive mode or using pyside.
|
||||||
|
@ -7,6 +7,7 @@ from ..Point import Point
|
|||||||
from .ScatterPlotItem import ScatterPlotItem, drawSymbol
|
from .ScatterPlotItem import ScatterPlotItem, drawSymbol
|
||||||
from .PlotDataItem import PlotDataItem
|
from .PlotDataItem import PlotDataItem
|
||||||
from .GraphicsWidgetAnchor import GraphicsWidgetAnchor
|
from .GraphicsWidgetAnchor import GraphicsWidgetAnchor
|
||||||
|
from .BarGraphItem import BarGraphItem
|
||||||
__all__ = ['LegendItem']
|
__all__ = ['LegendItem']
|
||||||
|
|
||||||
|
|
||||||
@ -241,14 +242,19 @@ class ItemSample(GraphicsWidget):
|
|||||||
p.setPen(fn.mkPen(opts['pen']))
|
p.setPen(fn.mkPen(opts['pen']))
|
||||||
p.drawLine(0, 11, 20, 11)
|
p.drawLine(0, 11, 20, 11)
|
||||||
|
|
||||||
|
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)
|
symbol = opts.get('symbol', None)
|
||||||
if symbol is not None:
|
if symbol is not None:
|
||||||
if isinstance(self.item, PlotDataItem):
|
if isinstance(self.item, PlotDataItem):
|
||||||
opts = self.item.scatter.opts
|
opts = self.item.scatter.opts
|
||||||
|
|
||||||
pen = fn.mkPen(opts['pen'])
|
|
||||||
brush = fn.mkBrush(opts['brush'])
|
|
||||||
size = opts['size']
|
|
||||||
|
|
||||||
p.translate(10, 10)
|
p.translate(10, 10)
|
||||||
path = drawSymbol(p, symbol, size, pen, brush)
|
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))
|
||||||
|
Loading…
Reference in New Issue
Block a user