Rt Rtt
Rt Rtt

Reputation: 617

matplotlit + PyQt5: replace a canvas

To this question I am referring to:

Link1: https://matplotlib.org/1.5.3/examples/user_interfaces/embedding_in_qt5.html

To be exactly, I am referring to this part from above link:

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""

    def compute_initial_figure(self):
        t = arange(0.0, 3.0, 0.01)
        s = sin(2*pi*t)
        self.axes.plot(t, s)

I want to change the canvas above to this one:

Link2: https://matplotlib.org/gallery/pyplots/fig_x.html#sphx-glr-gallery-pyplots-fig-x-py

I am a newbie to matplotlib. Although I tried a lot but it still doesnot work. Thus I would like to ask if someone could do the replacement, which I could understand how matplotlib works.

Thanks for the help!

Upvotes: 1

Views: 267

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40737

It's not entirely clear what you are trying to achieve. Maybe it would be better to post a Minimal, Complete, and Verifiable example (keyword is minimal here), instead of trying to modify the code from the example, which is probably overly complex.

In any case to replace the sin wave used by the original matplotlib example with the cross for the second example, you need to store the reference to the Figure object in the base canvas __init__ function

import matplotlib.lines as lines

class MyMplCanvas(FigureCanvas):
    """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        self.fig = Figure(figsize=(width, height), dpi=dpi)  # <------
        # replace all references to fig by self.fig in the rest of the code

Then replace the code for the MyStaticMplCanvas class with the code from the example:

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a sine plot."""

    def compute_initial_figure(self):
        l1 = lines.Line2D([0, 1], [0, 1], transform=self.fig.transFigure, figure=self.fig)
        l2 = lines.Line2D([0, 1], [1, 0], transform=self.fig.transFigure, figure=self.fig)
        self.fig.lines.extend([l1, l2])

Upvotes: 1

Related Questions