Home > database >  pyqtgrapgh: Image Item not in the center of GraphicsView
pyqtgrapgh: Image Item not in the center of GraphicsView

Time:05-05

I am using pyqtgraph to visualate 2D arrays. Due to the fact that I have to refresh the visualated Image because of a real time application which I want to evaluate I am using GraphicsView(). Then I create a ImageItem and I want to add the Item to the created window. Unfortunately the image is only visible in the left upper corner of the window. I know that I can show the full image without GraphicsView but as I said I will need GraphicsView later to visualate the updated values in real time for an application.

Here is my current code:

import numpy as np
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg

import struct
import sounddevice as sd
from scipy.fftpack import fft

import sys
import time

class Fenster(pg.GraphicsLayoutWidget):  #objektorientiert arbeiten!!
    def __init__(self):     #constructor
        super().__init__()  #ruft den constructor von QWidget auf
        self.initMe()  

class Fenster(pg.GraphicsLayoutWidget):  
    def __init__(self):     
        super().__init__()  
        self.initMe()       
        
        
    def initMe(self):
        
        self.win = pg.GraphicsView()
        self.win.show()
        
        self.plot_data = np.fromfunction(lambda i, j: (1 0.3*np.sin(i)) * (i)**2   (j)**2, (100, 100))
        self.img = pg.ImageItem(image=self.plot_data)
        self.cm = pg.colormap.get('CET-L9') 
        self.bar = pg.ColorBarItem( values= (0, 20_000), colorMap=self.cm ) 
        self.bar.setImageItem(self.img)
        
        self.win.setCentralItem(self.img)

w = Fenster() 

That is the output image: enter image description here

How can I set that the image fullfill the whole window? How can I do that the created ColorBarItem will be showed in the window? (optionally)

Thanks in advance.

CodePudding user response:

I found the solution. You have to create a PlotItem. Then add the ImageItem to the PlotItem and set the PlotItem in the center of GraphicsView

        self.win = pg.GraphicsView()
        self.win.show()
        self.p = pg.PlotItem() 
        self.win.setCentralItem(self.p)
        
        self.plot_data = np.fromfunction(lambda i, j: (1 0.3*np.sin(i)) * (i)**2   (j)**2, (100, 100))
        self.img = pg.ImageItem(image=self.plot_data)
        self.cm = pg.colormap.get('CET-L9') 
        self.bar = pg.ColorBarItem( values= (0, 20_000), colorMap=self.cm ) 
        self.bar.setImageItem(self.img)
    
        self.p.addItem(self.img)

I also figured out to update the plot (live plot). Leave a comment if I should share.

  • Related