BarGraphItem: calculate boundingRect without drawing#2599
Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom Feb 26, 2023
Merged
BarGraphItem: calculate boundingRect without drawing#2599j9ac9k merged 2 commits intopyqtgraph:masterfrom
j9ac9k merged 2 commits intopyqtgraph:masterfrom
Conversation
Contributor
Author
|
A demonstration of the over-generalized nature of import numpy as np
import random
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets
size = 32
x = np.arange(size)
y = np.arange(size)[:, np.newaxis]
bitmap = (y & 1) ^ (x & 1)
X, Y = np.meshgrid(x, y)
nzidx = bitmap.nonzero()
pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
no_pen = pg.mkPen(None)
back = QtWidgets.QGraphicsRectItem(0, 0, size, size)
back.setPen(no_pen)
back.setBrush(QtCore.Qt.GlobalColor.white)
num_rects = len(nzidx[0])
brush = (255, 255, 255)
brushes = [(0,0,random.randint(100,255)) for _ in range(num_rects)]
bgi = pg.BarGraphItem(x0=X[nzidx], y0=Y[nzidx], width=1, height=1, pen=no_pen, brushes=brushes)
pw.addItem(back)
pw.addItem(bgi)
pg.exec() |
Contributor
Author
|
An additional bugfix wrt import pyqtgraph as pg
pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
pw.addLegend()
bgi = pg.BarGraphItem(name='BGI', x=[0,1,2,3,4], width=1, y1=[1,2,3,4,5])
pw.addItem(bgi)
pg.exec() |
5103af9 to
21e48a5
Compare
Contributor
Author
|
An example of drawing many rectangles with each different color handled by one import pyqtgraph as pg
import numpy as np
import scipy.misc
z = scipy.misc.ascent()
ncolors = 256
# reduce the number of colors if needed
decimate = 1
z //= decimate
ncolors //= decimate
pg.mkQApp()
cm = pg.colormap.get("viridis")
lut_qcolor = cm.getLookupTable(0, 1, ncolors, mode=pg.ColorMap.QCOLOR)
lut_brushes = [pg.mkBrush(x) for x in lut_qcolor]
x = np.arange(z.shape[1])
y = np.arange(z.shape[0])[:, np.newaxis]
X, Y = np.meshgrid(x, y)
X = X.ravel()
Y = Y.ravel()
Z = z.ravel()
brushes = [lut_brushes[x] for x in Z]
pw = pg.PlotWidget()
pw.invertY(True)
pw.show()
no_pen = pg.mkPen(None)
for idx in range(ncolors):
mask = Z == idx
bgi = pg.BarGraphItem(x0=X[mask], y0=Y[mask], width=1, height=1, pen=no_pen, brush=lut_brushes[idx])
pw.addItem(bgi)
pg.exec() |
Contributor
Author
|
Just for fun, the equivalent using import pyqtgraph as pg
from pyqtgraph.graphicsItems.NonUniformImage import NonUniformImage
import numpy as np
import scipy.misc
z = scipy.misc.ascent()
# decimate, because it's really slow
deci = 2
z = z[::deci, ::deci]
# non-uniform uses transposed axes
z = z.T.copy()
x = np.arange(z.shape[0])
y = np.arange(z.shape[1])
pg.mkQApp()
pw = pg.PlotWidget()
pw.invertY(True)
pw.show()
img = NonUniformImage(x, y, z)
cm = pg.colormap.get("viridis")
img.setColorMap(cm)
pw.addItem(img)
pg.exec() |
Member
12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a rewrite/restructuring of
BarGraphItemto address some issues with the existing implementation:boundingRect,dataBoundsandpixelPaddingall trigger the rendering code.QPens andQBrushes, a copy will be made.QPens andQBrushes are not copied. The user is able to use the sameQPenorQBrushinstance for multiple bars.QRectFs are done one by one in a Python loop.QRectFsdrawRects()is not used.drawRects()is used if only a single color is being used. This path also skips creation ofQPicture.A animated script to exercise and benchmark the various combinations of using
BarGraphItem.It seems to demonstrate a largish speedup vs master.
Remarks:
BarGraphItemprobably wasn't intended for use in animated updates.