Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive mode in matplotlib

I want to dynamically update the scatter plot based on the y-axis data received from a socket connection. I used python matplot lib in interactive mode to do this, but during dynamic updation if i move the window to a different location or minimize the window then the plot updation stops abruptly. How to do this?

I have attached a sample dynamic updation code used here and the same problem appears here also.

import matplotlib.pyplot as plt
import random
import time
items = [25.5,26.7,23.4,22.5,20,13.4,15.6,-12,-16,20]
x = [1,2,3,4,5,6,7,8,9,10]

plt.ion() #  Interactive on

for i in range(1,100):
    plt.title('graph plotting')
    plt.ylabel('temperature') 
    plt.xlabel('time')
    random.shuffle(items)
    plt.plot(x,items,'ob-')
    plt.axis([0, 10, -40, 40])
    plt.draw()
    #time.sleep(2)
    plt.clf()
    plt.close()
like image 400
Sharath Avatar asked Dec 21 '09 14:12

Sharath


People also ask

Can you make matplotlib interactive?

The plot widget can be resized by the UI.The ipyml backend enables interactivity in matplotlib and all the libraries built on top of matplotlib like Pandas, Geopandas, Seaborn, etc.

Is matplotlib asynchronous?

Asynchronous Plotting in Matplotlib: rather than call savefig directly, add plots to an asynchronous queue to avoid holding up the main program. Makes use of multiple processes to speed up the writing out.

What is matplotlib use (' AGG ')?

The last, Agg, is a non-interactive backend that can only write to files. It is used on Linux, if Matplotlib cannot connect to either an X display or a Wayland display.


2 Answers

This page contains a couple of examples of dynamic plots with matplotlib and wxPython. And here is a version with PyQt.

like image 57
Eli Bendersky Avatar answered Sep 20 '22 17:09

Eli Bendersky


For this to work, you need to have a main loop for event handling, and your own event handler to redraw the plot when the window is resized or refreshed.

You'll find many examples for this on the web, or in the tutorials.

I think this is best handled by using a UI toolkit (e.g. wxPython), not using matplotlib interactive mode. I had a similar question in the past and got some good answers.

like image 37
Ber Avatar answered Sep 19 '22 17:09

Ber