简体   繁体   中英

how to plot real time in python without keeping previous plots?

I have used Theano to do a simple linear regression. Now I want to show the dataset and the line which its slope is optimizing each time. I have made a dynamic real time plot but the problem is that it keeps the previous plots. I want to keep the oroginal dataset and plot the new line each time. Here is my code:

import theano
from theano import tensor as T
import numpy as np
import matplotlib.pyplot as plt


trX = np.linspace(-1, 1, 10)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33
# PLOT THE ORIGINAL DATASET 
plt.figure()
plt.ion()
plt.scatter(trX,trY)

X = T.scalar()
Y = T.scalar()

def model(X, w):
    return X * w

w = theano.shared(np.asarray(0., dtype=theano.config.floatX))
y = model(X, w)

cost = T.mean(T.sqr(y - Y))
gradient = T.grad(cost=cost, wrt=w)
updates = [[w, w - gradient * 0.01]]

train = theano.function(inputs=[X, Y], outputs=cost, updates=updates,     allow_input_downcast=True)

for i in range(10):
    for x, y in zip(trX, trY):
        train(x, y)
        Weight = w.get_value()
        ablineValues = []
        for i in trX:
             ablineValues.append(Weight*i)
        # PLOT THE NEW OPTIMISED LINE
        plt.plot(trX,ablineValues,'r')
        plt.pause(0.0000001)

plt.show()

Do you know how I can do that? I have read other related problems but still I can not make it. Please direct me to a good page if you think can help me.

Maybe I am not understanding your question (I wanted to write a comment, but I can't...) Why don't you delete the previous graph every time you create a new one? Or give a name to the file in which the graph is created, so every time you create a new one it will delete the file with the same name. Maybe you will find this useful.

Using plot_handle.set_ydata (and vectorizing a bit)

import theano
from theano import tensor as T
import numpy as np
import matplotlib.pyplot as plt


trX = np.linspace(-1, 1, 10)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33
# PLOT THE ORIGINAL DATASET
plt.figure()
plt.ion()
plt.scatter(trX,trY)

X = T.scalar()
Y = T.scalar()

def model(X, w):
    return X * w

w = theano.shared(np.asarray(0., dtype=theano.config.floatX))
y = model(X, w)

cost = T.mean(T.sqr(y - Y))
gradient = T.grad(cost=cost, wrt=w)
updates = [[w, w - gradient * 0.01]]

train = theano.function(inputs=[X, Y], outputs=cost, updates=updates,     allow_input_downcast=True)

h, = plt.plot(trX,np.zeros(trX.shape)+np.nan,'r')  # INITIALIZE YOUR PLOT
for i in range(10):
    for x, y in zip(trX, trY):
        train(x, y)
        Weight = w.get_value()
        ablineValues = Weight*trX  # VECTORIZE!
        # PLOT THE NEW OPTIMISED LINE
        h.set_ydata(ablineValues)  # SET DATA INSTEAD OF REPLOTTING IT
        plt.pause(0.0000001)

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM