Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move figure window in interactive mode in matplotlib?

I'm trying to monitor real-time data with matplotlib.

I found that I can update plot dynamically with interactive mode in Pyplot.

And it worked well, but one problem is 'I cannot manipulate the figure window at all'. For example, move or re-size the figure window.

Here is my code.

This is cons of interactive mode? or I'm using it incorrectly?

import matplotlib.pyplot as plt
import time
import math

# generate data
x = [0.1*_a for _a in range(1000)]
y = map(lambda x : math.sin(x), x)

# interactive mode
plt.ion() # identical plt.interactive(True)

fig, ax = plt.subplots()
# ax = plt.gca()
lines,  = ax.plot([], [])

# ax.set_ylim(-1, 1)
ax.grid()

MAX_N_DATA = 100
x_data = []
y_data = []
for i in range(len(x)):
    # New data received
    x_data.append(x[i])
    y_data.append(y[i])

    # limit data length
    if x_data.__len__() > MAX_N_DATA:
        x_data.pop(0)
        y_data.pop(0)

    # Set Data
    lines.set_xdata(x_data)
    lines.set_ydata(y_data)

    # The data limits are not updated automatically.
    ax.relim()
    # with tight True, graph flows smoothly.
    ax.autoscale_view(tight=True, scalex=True, scaley=True)

    # draw
    plt.draw()
    time.sleep(0.01)

Thank you.

like image 899
JaeJun LEE Avatar asked Nov 09 '22 08:11

JaeJun LEE


1 Answers

As shown in this answer to another question, replace plt.draw() with plt.pause(0.05). This solved the problem for me.

like image 98
luator Avatar answered Nov 14 '22 21:11

luator