I had the opportunity to create a heatmap in real time, and I used pyqtgraph because I was worried about the processing speed of matplotlib. However, pyqtgraph has little information in Japanese, and there is no color bar used for heat map by default, so I will describe the results of various investigations.
Mac OS Python 3.8.1
Installed with pip
colour 0.1.5 numpy 1.19.2 pgcolorbar 1.0.0 PyQt5 5.15.2 PyQt5-sip 12.8.1 pyqtgraph 0.11.0
pip install colour numpy pgcolorbar PyQt5 PyQt5-sip pyqtgraph
pgcolorbar A library that creates color bars with pyqtgraph. After installing with pip, you can see the demo with the following command.
pgcolorbar_demo
Real-time drawing will be postponed this time and only the heat map will be explained. Finally, create the following screen. (2021/01/19 before correction)
This is sample data used for explanation. I'm doing various things, but it doesn't mean anything. Since it is a two-dimensional array, there is no RGB information.
#Sample array
data = np.random.normal(size=(200, 200))
data[40:80, 40:120] += 4
data = pg.gaussianFilter(data, (15, 15))
data += np.random.normal(size=(200, 200)) * 0.1
After that, add to the code below to create a heat map. What you are doing
is
"""Minimum code to display an image with pyqtgraph(To)"""
import sys
import numpy as np
from PyQt5 import QtWidgets
import pyqtgraph as pg
#Sample data code omitted
#GUI control object
app = QtWidgets.QApplication(sys.argv)
#Window creation
window = pg.GraphicsLayoutWidget()
#Image object creation&Set image
image = pg.ImageItem()
image.setImage(data)
#Create a box to store images&Set image object
view_box = pg.ViewBox()
view_box.addItem(image)
#Plot object creation&View created above_set box
plot = pg.PlotItem(viewBox=view_box)
#Add plot to window
window.addItem(plot)
#Window display
window.show()
#The end of the program
sys.exit(app.exec_())
If you execute it in this state, the following screen will be created.
ViewBox Add the following:
view_box.setAspectLocked(lock=True)
Fixed the aspect ratio of view_box. Because the image size is a 200x200 square, I don't want the shape to change when the window size changes.
Added peripheral code
view_box = pg.ViewBox()
view_box.setAspectLocked(lock=True) # Add
view_box.addItem(image)
Screen when executed
PlotItem There is no place to edit.
ImageItem The added parts and their surroundings are described below.
################## Add #######################
from colour import Color
blue, red = Color('blue'), Color('red')
colors = blue.range_to(red, 256)
colors_array = np.array([np.array(color.get_rgb()) * 255 for color in colors])
look_up_table = colors_array.astype(np.uint8)
################## Add #######################
image = pg.ImageItem()
image.setLookupTable(look_up_table) # Add
image.setImage(data)
Screen when executed
from colour import Color I will describe the details of the contents. What you want to create in this section is a two-dimensional array of RGB information (unsigned 8-bit integer) [[R, G, B], [R, G, B], ... [R, G, B],]
blue, red = Color('blue'), Color('red')
# >>> <class 'colour.Color'> <class 'colour.Color'>
blue.get_rgb()
# >>> (0.0, 0.0, 1.0)
colors = blue.range_to(red, 256) #256 divisions of colors from blue to red
# >>>If you look at the contents in the generator list
# >>> [<Color blue>, <Color #0004ff>, <Color #0008ff>, ..., <Color red>]
colors_array = np.array([np.array(color.get_rgb()) * 255 for color in colors])
# >>> [[ 0. 0. 255.]
# [ 0. 4. 255.]
# [ 0. 8. 255.]
# ...
# [255. 0. 0.]] float64
look_up_table = colors_array.astype(np.uint8) #Convert to unsigned 8-bit integer
# >>> [[ 0 0 255]
# [ 0 3 255]
# ...
# [255 0 0]] uint8
Look Up Table
Table that looks up (references) the output color data corresponding to the input color data 1st LUT Basics | TV Logic
It seems. (I'm sorry I'm not very familiar with it ...) It is an image like a table that converts the value of sample data into RGB information.
image.setOpts(axisOrder='row-major')
Please add the above code.
By default, pg.ImageItem
reads a 2D array in columns-> rows.
Since a 2D array is basically a matrix, it is necessary to read it in rows-> columns.
If not written, it will be read like a transposed matrix of the original data.
Modified code
################## Add #######################
from colour import Color
blue, red = Color('blue'), Color('red')
colors = blue.range_to(red, 256)
colors_array = np.array([np.array(color.get_rgb()) * 255 for color in colors])
look_up_table = colors_array.astype(np.uint8)
################## Add #######################
image = pg.ImageItem()
image.setOpts(axisOrder='row-major') # 2021/01/19 Add
image.setLookupTable(look_up_table) # Add
image.setImage(data)
ColorLegendItem Create a color bar Add the following:
################## Add #######################
from pgcolorbar.colorlegend import ColorLegendItem
color_bar = ColorLegendItem(imageItem=image, showHistogram=True)
color_bar.resetColorLevels()
window.addItem(color_bar)
################## Add #######################
Pass pg.imgItem
to imgeItem
. show Histogram
can decide whether to display the histogram next to the color bar. The default is True.
Use resetColorLevels ()
to set the color bar range to the maximum and minimum values of the data.
Executed screen (2021/01/19 before modification)
You can set the color bar label with the label
argument.
color_bar = ColorLegendItem(imageItem=self.image, showHistogram=True, label='sample')
Whole code
"""Whole code"""
import sys
from colour import Color
import numpy as np
from pgcolorbar.colorlegend import ColorLegendItem
from PyQt5 import QtWidgets
import pyqtgraph as pg
#Sample array
data = np.random.normal(size=(200, 200))
data[40:80, 40:120] += 4
data = pg.gaussianFilter(data, (15, 15))
data += np.random.normal(size=(200, 200)) * 0.1
app = QtWidgets.QApplication(sys.argv)
window = pg.GraphicsLayoutWidget()
blue, red = Color('blue'), Color('red')
colors = blue.range_to(red, 256)
colors_array = np.array([np.array(color.get_rgb()) * 255 for color in colors])
look_up_table = colors_array.astype(np.uint8)
image = pg.ImageItem()
image.setOpts(axisOrder='row-major') # 2021/01/19 Add
image.setLookupTable(look_up_table)
image.setImage(data)
view_box = pg.ViewBox()
view_box.setAspectLocked(lock=True)
view_box.addItem(image)
plot = pg.PlotItem(viewBox=view_box)
color_bar = ColorLegendItem(imageItem=image, showHistogram=True, label='sample') # 2021/01/20 add label
color_bar.resetColorLevels()
window.addItem(plot)
window.addItem(color_bar)
window.show()
sys.exit(app.exec_())
GitHub - titusjan/pgcolorbar: Color bar to use in PyQtGraph plots
Recommended Posts