Exemple #1
0
class PlotCanvas(FigureCanvas):
    """
    A Widget view that presents a plot or image
    """

    current_figure = None
    axes = None

    def __init__(self, parent=None, width=10, height=10, dpi=100):
        self.current_figure = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.current_figure.add_subplot(111)

        FigureCanvas.__init__(self, self.current_figure)
        self.current_figure.patch.set_facecolor("none")
        self.setStyleSheet("background-color:rgba(0, 0, 0, 0)")
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self, QSizePolicy.Expanding,
                                   QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)

    def plot_graph(self, x_axis, y_axis, title=""):
        """
        Plots a graph in the view

        :param x_axis: The x-axis to display
        :type x_axis: Numpy array

        :param y_axis: The y-axis to display
        :type y_axis: Numpy array

        :param title: The title to display
        :type title: str
        """
        self.clear_figure()
        self.current_figure = self.figure.add_subplot(111)
        self.current_figure.plot(x_axis, y_axis)
        self.current_figure.set_title(title)
        self.draw()

    def plot_image(self, image, title=""):
        """
        Displays a image in a view

        :param image: The image to display
        :type image: Numpy array

        :param title: The title to display
        :type title: str
        """
        self.clear_figure()
        self.current_figure = self.figure.add_subplot(111)
        self.current_figure.axis("off")
        if image.ndim == 2:
            self.current_figure.imshow(image, plt.cm.gray)
        else:
            self.current_figure.imshow(image)
        self.current_figure.set_title(title)
        self.draw()

    def clear_figure(self):
        """
        Clears the current figure
        """
        if self.current_figure is not None:
            plt.close(111)
            self.current_figure = None