Why is flush_events() taking longer than expected?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Why is flush_events() taking longer than expected?

David Aldrich
Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is flush_events() taking longer than expected?

Benjamin Root
When you say first and second flush_events(), do you mean the first and second iterations, or fig_A and fig_B?

Ben Root

On Fri, Jul 21, 2017 at 9:33 AM, David Aldrich <[hidden email]> wrote:
Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is flush_events() taking longer than expected?

David Aldrich

Hi Ben

 

>When you say first and second flush_events(), do you mean the

>first and second iterations, or fig_A and fig_B?

 

The latter:

 

    fig_A.canvas.flush_events()     ß 350ms
    fig_B.canvas.flush_events()     ß 1ms

 

These timings repeat on each iteration.

 

Best regards

 

David

 

From: Benjamin Root [mailto:[hidden email]]
Sent: 25 July 2017 15:46
To: David Aldrich <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

When you say first and second flush_events(), do you mean the first and second iterations, or fig_A and fig_B?

Ben Root

 

On Fri, Jul 21, 2017 at 9:33 AM, David Aldrich <[hidden email]> wrote:

Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

 

 

Click here to report this email as spam.


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is flush_events() taking longer than expected?

tcaswell
Exactly what `flush_events` is doing depends on the backend, however I suspect that in most cases the events are handled in a process-level singleton so both calls are talking to event queue so the first one does all the work and the second one finds and empty queue, declares victory and returns.  To test this switch the A/B order, I expect the first one to be slow in either case.

As to why it is going slow, I suspect it is the text on your 16 axes.  To get the speed you want you are probably are going to want to use 'blitting' which is a way of only re-drawing what has changed (rather than everything).  See https://matplotlib.org/api/animation_api.html#funcanimation for more details. 

Tom

On Tue, Jul 25, 2017 at 11:26 AM David Aldrich <[hidden email]> wrote:

Hi Ben

 

>When you say first and second flush_events(), do you mean the

>first and second iterations, or fig_A and fig_B?

 

The latter:

 

    fig_A.canvas.flush_events()     ß 350ms
    fig_B.canvas.flush_events()     ß 1ms

 

These timings repeat on each iteration.

 

Best regards

 

David

 

From: Benjamin Root [mailto:[hidden email]]
Sent: 25 July 2017 15:46
To: David Aldrich <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

When you say first and second flush_events(), do you mean the first and second iterations, or fig_A and fig_B?

Ben Root

 

On Fri, Jul 21, 2017 at 9:33 AM, David Aldrich <[hidden email]> wrote:

Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

 

 

Click here to report this email as spam.

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is flush_events() taking longer than expected?

David Aldrich

Hi Thomas

 

Thanks for your reply.

 

> To test this switch the A/B order, I expect the first one to be slow in either case.

 

Yes, I tried that and the first one is always slower, as you suggested.

 

> To get the speed you want you are probably are going to want to use 'blitting'

> which is a way of only re-drawing what has changed (rather than everything). 

 

I understand.  However, I expected the redraw work to be done in the plotting calls, not in `flush_events`.  Do you think drawing may be done in `flush_events`?

 

Best regards

 

David

 

From: Thomas Caswell [mailto:[hidden email]]
Sent: 31 July 2017 13:53
To: David Aldrich <[hidden email]>; Benjamin Root <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

Exactly what `flush_events` is doing depends on the backend, however I suspect that in most cases the events are handled in a process-level singleton so both calls are talking to event queue so the first one does all the work and the second one finds and empty queue, declares victory and returns.  To test this switch the A/B order, I expect the first one to be slow in either case.

 

As to why it is going slow, I suspect it is the text on your 16 axes.  To get the speed you want you are probably are going to want to use 'blitting' which is a way of only re-drawing what has changed (rather than everything).  See https://matplotlib.org/api/animation_api.html#funcanimation for more details. 

 

Tom

 

On Tue, Jul 25, 2017 at 11:26 AM David Aldrich <[hidden email]> wrote:

Hi Ben

 

>When you say first and second flush_events(), do you mean the

>first and second iterations, or fig_A and fig_B?

 

The latter:

 

    fig_A.canvas.flush_events()     ß 350ms
    fig_B.canvas.flush_events()     ß 1ms

 

These timings repeat on each iteration.

 

Best regards

 

David

 

From: Benjamin Root [mailto:[hidden email]]
Sent: 25 July 2017 15:46
To: David Aldrich <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

When you say first and second flush_events(), do you mean the first and second iterations, or fig_A and fig_B?

Ben Root

 

On Fri, Jul 21, 2017 at 9:33 AM, David Aldrich <[hidden email]> wrote:

Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

 

 

Click here to report this email as spam.

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is flush_events() taking longer than expected?

tcaswell
We have made an effort recently to make drawing as lazy as possible and not actually do the rendering until it as absolutely required. Ideally delayed off until the GUI needs the pixel values for the paint event.   This lets you do many `art.update(...); fig.canvas.draw_idle()` and only pay the draw cost once when you need to show it on the screen (rather than on every update).

Tom

On Mon, Jul 31, 2017 at 9:00 AM David Aldrich <[hidden email]> wrote:

Hi Thomas

 

Thanks for your reply.

 

> To test this switch the A/B order, I expect the first one to be slow in either case.

 

Yes, I tried that and the first one is always slower, as you suggested.

 

> To get the speed you want you are probably are going to want to use 'blitting'

> which is a way of only re-drawing what has changed (rather than everything). 

 

I understand.  However, I expected the redraw work to be done in the plotting calls, not in `flush_events`.  Do you think drawing may be done in `flush_events`?

 

Best regards

 

David

 

From: Thomas Caswell [mailto:[hidden email]]
Sent: 31 July 2017 13:53
To: David Aldrich <[hidden email]>; Benjamin Root <[hidden email]>


Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

Exactly what `flush_events` is doing depends on the backend, however I suspect that in most cases the events are handled in a process-level singleton so both calls are talking to event queue so the first one does all the work and the second one finds and empty queue, declares victory and returns.  To test this switch the A/B order, I expect the first one to be slow in either case.

 

As to why it is going slow, I suspect it is the text on your 16 axes.  To get the speed you want you are probably are going to want to use 'blitting' which is a way of only re-drawing what has changed (rather than everything).  See https://matplotlib.org/api/animation_api.html#funcanimation for more details. 

 

Tom

 

On Tue, Jul 25, 2017 at 11:26 AM David Aldrich <[hidden email]> wrote:

Hi Ben

 

>When you say first and second flush_events(), do you mean the

>first and second iterations, or fig_A and fig_B?

 

The latter:

 

    fig_A.canvas.flush_events()     ß 350ms
    fig_B.canvas.flush_events()     ß 1ms

 

These timings repeat on each iteration.

 

Best regards

 

David

 

From: Benjamin Root [mailto:[hidden email]]
Sent: 25 July 2017 15:46
To: David Aldrich <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

When you say first and second flush_events(), do you mean the first and second iterations, or fig_A and fig_B?

Ben Root

 

On Fri, Jul 21, 2017 at 9:33 AM, David Aldrich <[hidden email]> wrote:

Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

 

 

Click here to report this email as spam.

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Why is flush_events() taking longer than expected?

David Aldrich

Thanks, I understand.

 

David

 

From: Thomas Caswell [mailto:[hidden email]]
Sent: 31 July 2017 14:06
To: David Aldrich <[hidden email]>; Benjamin Root <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

We have made an effort recently to make drawing as lazy as possible and not actually do the rendering until it as absolutely required. Ideally delayed off until the GUI needs the pixel values for the paint event.   This lets you do many `art.update(...); fig.canvas.draw_idle()` and only pay the draw cost once when you need to show it on the screen (rather than on every update).

 

Tom

 

On Mon, Jul 31, 2017 at 9:00 AM David Aldrich <[hidden email]> wrote:

Hi Thomas

 

Thanks for your reply.

 

> To test this switch the A/B order, I expect the first one to be slow in either case.

 

Yes, I tried that and the first one is always slower, as you suggested.

 

> To get the speed you want you are probably are going to want to use 'blitting'

> which is a way of only re-drawing what has changed (rather than everything). 

 

I understand.  However, I expected the redraw work to be done in the plotting calls, not in `flush_events`.  Do you think drawing may be done in `flush_events`?

 

Best regards

 

David

 

From: Thomas Caswell [mailto:[hidden email]]
Sent: 31 July 2017 13:53
To: David Aldrich <[hidden email]>; Benjamin Root <[hidden email]>


Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

Exactly what `flush_events` is doing depends on the backend, however I suspect that in most cases the events are handled in a process-level singleton so both calls are talking to event queue so the first one does all the work and the second one finds and empty queue, declares victory and returns.  To test this switch the A/B order, I expect the first one to be slow in either case.

 

As to why it is going slow, I suspect it is the text on your 16 axes.  To get the speed you want you are probably are going to want to use 'blitting' which is a way of only re-drawing what has changed (rather than everything).  See https://matplotlib.org/api/animation_api.html#funcanimation for more details. 

 

Tom

 

On Tue, Jul 25, 2017 at 11:26 AM David Aldrich <[hidden email]> wrote:

Hi Ben

 

>When you say first and second flush_events(), do you mean the

>first and second iterations, or fig_A and fig_B?

 

The latter:

 

    fig_A.canvas.flush_events()     ß 350ms
    fig_B.canvas.flush_events()     ß 1ms

 

These timings repeat on each iteration.

 

Best regards

 

David

 

From: Benjamin Root [mailto:[hidden email]]
Sent: 25 July 2017 15:46
To: David Aldrich <[hidden email]>
Cc: [hidden email]
Subject: Re: [Matplotlib-users] Why is flush_events() taking longer than expected?

 

When you say first and second flush_events(), do you mean the first and second iterations, or fig_A and fig_B?

Ben Root

 

On Fri, Jul 21, 2017 at 9:33 AM, David Aldrich <[hidden email]> wrote:

Hi

I am using Matplotlib 2.0.2 in a Python 3.6 script on Windows 10 to plot data received over a network connection. New data arrives 4 times per second. I need to update two figures fig_A and fig_B, each containing 8 subplots. The hardware is a fast i7 laptop.

My main execution loop is:

plt.draw()

while (True):
    fig_A.canvas.flush_events()
    fig_B.canvas.flush_events()
    updatePlots()                          # Polls socket for data and updates scatter collections
    fig_A.canvas.draw_idle()
    fig_B.canvas.draw_idle()

plt.waitforbuttonpress()

I have timed the code and found that the first flush_events() call in the loop takes 350ms. The second flush_events() call takes <1ms.
350ms seems very long and is restricting the update rate.

Why might flush_events() be taking so long?

(I have not changed the backend from the default, which I believe is TkAgg).

Best regards

David

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

 

 

Click here to report this email as spam.

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users