'convert matplotlib figure to 2-D Array to show in pyqtgraph.ImageItem

i find an example in this Link which show 2-D array in pyqtgraph.ImageItem.

import matplotlib.pyplot as plt
import numpy as np
import pyqtgraph as pg

def main():
    imarray = np.random.rand(100, 100) * 515
    imarray = np.clip(imarray, 0, 515)
    plt_example(imarray)
    pyqtgraph_example(imarray)

def plt_example(data):
    plt.imshow(data, cmap='hot')
    c = plt.colorbar()
    plt.clim(0, 200)
    plt.show(block=False)

def pyqtgraph_example(data):
    app = pg.mkQApp("Test")
    win = pg.GraphicsLayoutWidget()
    win.setWindowTitle('pyqtgraph example: ImageItem')
    win.show()

    # add plot with correct axis orientation and locked aspect ratio
    plot = win.addPlot()
    plot.setAspectLocked(True)
    plot.invertY()

    # Create image item
    img = pg.ImageItem(data, axisOrder='row-major')
    plot.addItem(img)

    # Create color bar and have it control image levels    
    cmap = pg.colormap.getFromMatplotlib('hot')
    cbi = pg.ColorBarItem(colorMap=cmap)
    cbi.setImageItem(img, insert_in=plot)
    cbi.setLevels([0, 200]) # colormap range

    app.exec()

if __name__ == "__main__":
    main()

i want to create matplotlib figure and plot some graph like histogram into that

import matplotlib.pyplot as plt

x = [1,1,2,3,3,5,7,8,9,10,
 10,11,11,13,13,15,16,17,18,18,
 18,19,20,21,21,23,24,24,25,25,
 25,25,26,26,26,27,27,27,27,27,
 29,30,30,31,33,34,34,34,35,36,
 36,37,37,38,38,39,40,41,41,42,
 43,44,45,45,46,47,48,48,49,50,
 51,52,53,54,55,55,56,57,58,60,
 61,63,64,65,66,68,70,71,72,74,
 75,77,81,83,84,87,89,90,90,91
 ]

plt.hist(x, bins=10)

histogram graph output

now i looking for a way to set histogram graph to pyqtgraph.ImageItem. anyone can help me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source