0

To this question I am referring to this example: https://matplotlib.org/examples/user_interfaces/embedding_in_qt5.html

I want to replace the sine plot to with a simple rectangle plot without coordinate system shown.

To be exactly, I want to change this part of the original code:

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 found the rectangle class in Matplotlib: https://matplotlib.org/devdocs/api/_as_gen/matplotlib.patches.Rectangle.html#examples-using-matplotlib-patches-rectangle

This should be a simple task. But I tried a lot on my own without sucess. One of my many tries looks like: (But it does not work)

class MyStaticMplCanvas(MyMplCanvas):
    """Simple canvas with a rectangle plot."""
    def compute_initial_figure(self):
        x = 10
        y = 20
        width = 10
        height = 20

        fig,ax = plt.subplots(1)
        rect = mpatches.Rectangle((x, y), width, height)

        ax.add_patch(rect)
        self.axes.plot(rect)

Could anyone please help me out? Any help will be highly appreciated!

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Rt Rtt
  • 595
  • 2
  • 13
  • 33

1 Answers1

2

You can add the Rectangle to self.axes with add_patch. This method does not automatically change the limits of the axis. Consider where you are trying to place the Rectangle patch.

class MyStaticMplCanvas(MyMplCanvas):

    """Simple canvas with a rectangle plot."""
    def compute_initial_figure(self):
        x = 10
        y = 20
        width = 10
        height = 20

        rect = mpatches.Rectangle((x, y), width, height)
        self.axes.add_patch(rect)
        # Change limits so we can see rect
        self.axes.set_xlim((0, 50))
        self.axes.set_ylim((0, 50))
user3419537
  • 4,740
  • 2
  • 24
  • 42