I want to update a line plot with matplotlib and wonder, if there is a good modification of the code, such that the line plotted simply gets updated instead of getting redrawn every time. Here is a sample code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
matplotlib.style.use('ggplot')
plt.ion()
fig=plt.figure()
i=0
df = pd.DataFrame({"time": [pd.datetime.now()], "value": 0}).set_index("time")
plt.plot(df);
while True:
temp_y=np.random.random();
df2 = pd.DataFrame({"time": [pd.datetime.now()], "value": temp_y}).set_index("time")
df = df.append(df2)
plt.plot(df)
i+=1
plt.show()
plt.pause(0.000001)
As you see, the plotting gets slower and slower after a while and I think the line chart is redrawn every iteration since it changes colours.