SourceForge logo
SourceForge logo
Menu

Re: [matplotlib-devel] Plot 1 point at a time, results in very bad performance and the figuring entering a state of non-response

On Fri, Aug 29, 2008 at 12:01 PM, Ray Salem <ray...@gm...> wrote:
> This is my first post, I really enjoy using python/numpy/scipy/matplotlib
> - great replacement for matlab. I am having a problem, with plot 1 point at
> a time. i am attempt to emulate an issue in the lab, where we get samples
> every second and plot them. The figure becomes non-responsive and eventually
> stops plotting, does anyone have an idea.
Yep, this is a bad idea. The line object in matplotlib is a fairly
heavy object which has a lot of stuff under the hood. Each plot
command creates a new line object. What you want to do is create just
one (or a few) line objects, each of which handles many points. Below
is the first pass naive implementation to show you how to do this --
you will probably want to improve on this by using a better data
structure to store the growing points, eg a buffer that drops old
points off the end, or a dynamically resizing numpy array which grows
intelligently when it gets full. The main point is that we create a
single line object and add the new data to it....
import time
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections as collections
plt.ion() # interactive plotting
fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False)
xdata, ydata = [0.], [0.]
line, = ax.plot(xdata, ydata, 'o')
dt = 0.1
for i in range(100):
 x = xdata[-1] + dt
 y = np.sin(2*np.pi*x)
 xdata.append(x)
 ydata.append(y)
 line.set_data(xdata, ydata)
 ax.set_xlim(x-1, x)
 ax.set_ylim(-1.1, 1.1)
 fig.canvas.draw()
 time.sleep(0.1)
plt.show()

View entire thread

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.
Thanks for helping keep SourceForge clean.
X





Briefly describe the problem (required):
Upload screenshot of ad (required):
Select a file, or drag & drop file here.
Screenshot instructions:

Click URL instructions:
Right-click on the ad, choose "Copy Link", then paste here →
(This may not be possible with some types of ads)

More information about our ad policies

Ad destination/click URL:

AltStyle によって変換されたページ (->オリジナル) /