<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""
Live_Plot_Simple.py

    A simple example using matplotlib with the TkAgg backend
    to creat a plot as you watch, with updating text.
    This version slows down as more and more points are plotted.

    Inspired by
        matplotlib.sourceforge.net/examples/animation/simple_anim_tkagg.html
    
    Copyright (c) 2011 University of Toronto
    Original Version:   17 August 2011 by David Bailey 
    Contact: David Bailey &lt;dbailey@physics.utoronto.ca&gt;
                (www.physics.utoronto.ca/~dbailey)
    License: Released under the MIT License; the full terms are appended
                to the end of this file, and are also available
                at www.opensource.org/licenses/mit-license.php
"""
ticks = []  # timer for speed optimization
from time import clock
ticks.append(clock()) # time at start

from numpy import exp, sin, random
import matplotlib
matplotlib.use('TkAgg') # Select TkAgg before importing pyplot
    # see matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend

from matplotlib import pyplot

fig = pyplot.figure()
fig.set_size_inches((8,6), forward=True) # Set size of plot window
# Create subplot in top half of window, leaving bottom half for text
plt = fig.add_subplot(211) 

def LivePlot():
    ticks.append(clock()) # time at start of plotting
    x = 0                 # initial value for x (horizontal axis)
    n_points = 5000       # maximum number of points to plot
    xy_data = []          # create store for data
    text = pyplot.figtext(0.05,0.25,"") # initialize text section
    for i in range(0,n_points):
        x += 0.1
        # y will be an exponentially decaying sine with gaussian noise
        y = exp(-i*0.005)*sin(x)+0.1*random.randn()
        xy_data += [[x,y]]
        # Draw line from previous point to current point
        plt.plot( [xy_data[i-1][0],xy_data[i][0]],
                  [xy_data[i-1][1],xy_data[i][1]],
                  color="blue")
        fig.texts.remove(text)  # Remove previous text to avoid overprinting
        # Print the current point number and x,y value below plot.
        text = pyplot.figtext(0.05,0.25,
                                "Point # " + str(i+1) +
                                "\nx,y = " + str(x) + ", " + str(y))
        fig.canvas.draw() # draw the new line segment and text

    ticks.append(clock()) # time at end of plotting

fig.canvas.manager.window.after(100,LivePlot) # After 100ms, call LivePlot
pyplot.show()
ticks.append(clock()) # time at end

# Print timings
print( "Initialization took : {} seconds".format(ticks[1]-ticks[0]) )
print( "Drawing took        : {} seconds".format(ticks[2]-ticks[1]) )
print( "Finishing took      : {} seconds".format(ticks[3]-ticks[2]) )

# End of Live_Plot_Simple.py


"""
Full text of MIT License:

    Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

</pre></body></html>