Some beginner questions

Hi

I'm new to Matplotlib and am struggling a bit.

I'm using Matplotlib with the Kivy GUI framework, but that shouldn't be directly relevant. I want to show a single figure having 4 subplots, each displaying one line. My code looks like this (simplified):

class CMplGraph():
    pass

    def __init__(self, **kwargs):
        self.create_plot()

    def create_plot(self):
        self.fig, ((self.ax0, self.ax1), (self.ax2, self.ax3)) = plt.subplots(nrows=2, ncols=2)

        self.ax0.set_title("Title_0")
        self.ax1.set_title("Title_1")
        self.ax2.set_title("Title_2")
        self.ax3.set_title("Title_3")
        plt.show()

    def plot(self, plotType, xCoords, yCoords):

        if (plotType == "PLOT_0 "):
            ax = self.ax0
        elif (plotType == " PLOT_1"):
            ax = self.ax1
        elif (plotType == " PLOT_2"):
            ax = self.ax2
        elif (plotType == " PLOT_3"):
            ax = self.ax3
        else:
            raise BadPlotType(plotType)

        # remove previous line
        if len(ax.lines) > 0:
            ax.lines.pop(0)

        plt.draw()
        time.sleep(1) # Blink

        line = ax.plot(xCoords, yCoords, color='blue')

        canvas = self.fig.canvas
        canvas.draw()

Then my main app can call create_plot() once, followed by plot() whenever it wants to update the displayed data.

This seems to work but I'm not sure I'm doing it correctly. Here are my questions:

1) Is it ok that I created axis objects?

2) Am I removing the previous line correctly?

3) I would like display to 'blink' between successive calls to plot(). So I put in a 1s sleep after removing the previous line. There should therefore be a flash before the new data is displayed. But I don't see that blink - the line updates instantaneously. Why is that?

Best regards

David

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20151113/6eaf95e0/attachment-0001.html>

1) Yes you are doing that right. It is the preferred way to do object
oriented matplotlib. Avoid calling plt methods except for figure creation
and figure showing.
2) No, you aren't removing it correctly. The line object should have a
"remove()" method available that should do all the bookkeeping properly.
So, that means storing the line object somewhere. Remember that ax.plot()
returns a list of line objects.
3) I suspect it is because you are calling plt.draw() instead of
canvas.draw(), but I am not certain.

I hope this helps!
Ben Root

P.S., another approach is to simply modify the data in the Line2D object,
rather than removing it and creating a new one. You can imitate the
"blinking" effect by calling "set_visible(False)", draw the canvas, sleep
for a second, and then "set_visible(True)" and update its data before
drawing the canvas again. Remember, you would need to keep around the
returned line objects from ax.plot() like I noted in (2).

···

On Fri, Nov 13, 2015 at 7:52 AM, David Aldrich <David.Aldrich at emea.nec.com> wrote:

Hi

I?m new to Matplotlib and am struggling a bit.

I?m using Matplotlib with the Kivy GUI framework, but that shouldn?t be
directly relevant. I want to show a single figure having 4 subplots, each
displaying one line. My code looks like this (simplified):

class CMplGraph():

    pass

    def __init__(self, **kwargs):

        self.create_plot()

    def create_plot(self):

        self.fig, ((self.ax0, self.ax1), (self.ax2, self.ax3)) =
plt.subplots(nrows=2, ncols=2)

        self.ax0.set_title("Title_0")

        self.ax1.set_title("Title_1")

        self.ax2.set_title("Title_2")

        self.ax3.set_title("Title_3")

        plt.show()

    def plot(self, plotType, xCoords, yCoords):

        if (plotType == "PLOT_0 "):

            ax = self.ax0

        elif (plotType == " PLOT_1"):

            ax = self.ax1

        elif (plotType == " PLOT_2"):

            ax = self.ax2

        elif (plotType == " PLOT_3"):

            ax = self.ax3

        else:

            raise BadPlotType(plotType)

        # remove previous line

        if len(ax.lines) > 0:

            ax.lines.pop(0)

        plt.draw()

        time.sleep(1) # Blink

        line = ax.plot(xCoords, yCoords, color='blue')

        canvas = self.fig.canvas

        canvas.draw()

Then my main app can call create_plot() once, followed by plot() whenever
it wants to update the displayed data.

This seems to work but I?m not sure I?m doing it correctly. Here are my
questions:

1) Is it ok that I created axis objects?

2) Am I removing the previous line correctly?

3) I would like display to ?blink? between successive calls to
plot(). So I put in a 1s sleep after removing the previous line. There
should therefore be a flash before the new data is displayed. But I don?t
see that blink ? the line updates instantaneously. Why is that?

Best regards

David

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20151201/76e93bd7/attachment-0001.html>