1

I'm trying to embed matplotlib in a PyQt4+Python 2.7 application so that I can visualize image (i.e. 2D) data in my UI. I'd like to make use of some of matplotlib's functionality such as colormaps, zooming, etc. I've found a few tutorials online but the closest thing that comes to my problem is the following SO question:

Embedding matplotlib in pyqt4 using imshow

Like the author, I'd also like to use "imshow(...)". By following a tutorial online I can embed matplotlib in my UI as a QWidget as follows:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class matPlotLibImage(FigureCanvas):
    def __init__(self):
        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.x = np.arange(0.0, 3.0, 0.01)
        self.y = np.cos(2*np.pi*self.x)
        self.axes.plot(self.x, self.y)
        super(MatPlotLibImage, self).__init__(self.fig)

In my QDockWidget-derived class code I can then embed the above matplotlib widget via:

self.mainImage = MatPlotLibImage()
self.setWidget(self.mainImage)

This works and displays a simple plot as expected. Now, however, I'd like to display image data represented as numpy arrays. All the examples I'm seeing online are using variants of the following:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

img = mpimg.imread("someImage.png")
imgplot = plt.imshow(img)

My question is: how do I combine the two approaches so that I can use 'imshow()' but embedded as I did for the simple plot? Thank you in advance.

Clarification: Strictly speaking, it's not crucial that I use 'imshow(whatever)', I'm just interested in displaying image data (in a numpy array) in my embedded matplotlib widget. However, imshow does have some useful things that I'd like to leverage.

LKeene
  • 627
  • 1
  • 8
  • 22

1 Answers1

1

It sounds like you only need to replace the line self.axes.plot(self.x, self.y) with

self.axes.imshow(img)

where img is the numpy array of the image, that you will have to have loaded previous to that.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712