0

I have a dataset that receives manual input that adds rows one-by-one. I want to create a scatterplot to plot the data and update when more data is added. Using matplotlib, how would I go about updating the existing plot, instead of redrawing the entire thing? I have the plot all laid out, it's just adding the points. So here's what I have so far:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


df = pd.DataFrame([[1, 3.00]], columns=['At_Bat', 'Stand'])

plt.ion()

x = np.arange(0, 999, 0.1)

y1 = -5
y2 = .75
y3 = 2.25
y4 = 5

plt.fill_between(x, y1, y2, color='lawngreen', alpha='.6')
plt.fill_between(x, y2, y3, color='yellow', alpha='.6')
plt.fill_between(x, y3, y4, color='red', alpha='.6')

plt.scatter(df.At_Bat, df.Stand)
plt.plot(df.At_Bat, df.Stand)
plt.axhline(y=0, color='black')
plt.xticks(np.arange(0, df.Stand.max() + 1))
plt.ylim([-4, 4])
plt.xlim([0, df.Stand.max() + 1])

df2 = pd.DataFrame([[2, 2.50]])
df = np.append(df, df2, axis = 0)

So df contains the first point, and then each additional point is added using df2 and append.

1

1 Answer 1

1

Just add

plt.scatter(df2[0], df2[1])

after your code - this should append the next point of data stored in df2 to the existing plot.

If you don't like the different color, you can save the first point in your code in a variable like

ps = plt.scatter(df.At_Bat, df.Stand)

and add new data afterwards then like

plt.scatter(df2[0], df2[1], fc = ps.get_facecolor())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.