0

I want to animate the scatter plot based on the actual timestamp from the csv file (see below). I'm not so good with matplotlib and I know of the animation function and the ion()-function but I'm not sure how to implement it. I read this but it seemed very difficult to implement it in my way. I have tried the code below but it only shows me every loop a new window with the actual data but I would like to have the animation in one window thanks in advance :):

import pandas as pd
import matplotlib.pyplot as plt

start_time = 86930.00
end_time = 86934.00
df = pd.read_csv('Data.csv', delimiter=',')

for timestamp in range(int(start_time), int(end_time), 1):
    act_data = df.loc[df['timestamp'] == float(timestamp)]
    X = act_data.x
    Y = act_data.y
    plt.scatter(X, Y)
    plt.show()

Data.csv:

timestamp,id,x,y
86930.00,1,1155.53,7155.05
86930.00,2,3495.08,8473.46
86931.00,1,3351.04,6402.27
86931.00,3,3510.59,8021.62
86931.00,2,2231.04,6221.27
86931.00,4,3710.59,8111.62
86932.00,2,3333.01,6221.27
86932.00,1,3532.59,3178.62
86933.00,3,1443.01,2323.27
86933.00,4,5332.59,1178.62

It would be cool if I could color the blobs by ID but not necessary :).

3
  • Is the IPython/Jupyter notebook an option here? Very easy to do that there. Commented Sep 14, 2016 at 11:32
  • No it's not an option. But sounds interesting. How would you do it there? Maybe I can port it. Commented Sep 14, 2016 at 11:36
  • In a Notebook, widgets can do such things easily. Commented Sep 14, 2016 at 11:38

1 Answer 1

3

The quickest way to animate on the same axis is to use interactive plots, plt.ion

import pandas as pd
import matplotlib.pyplot as plt

start_time = 86930.00
end_time = 86934.00
df = pd.read_csv('Data.csv', delimiter=',')


fig, ax = plt.subplots(1,1)
plt.ion()
plt.show()

for timestamp in range(int(start_time), int(end_time), 1):
    act_data = df.loc[df['timestamp'] == float(timestamp)]
    X = act_data.x
    Y = act_data.y
    ax.scatter(X, Y)
    plt.pause(1.0)

Although, I suspect from your title you want something interactive, which you can also use a matplotlib slider widget. With you data,

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

start_time = 86930.00
end_time = 86934.00
df = pd.read_csv('Data.csv', delimiter=',')


fig, ax = plt.subplots(1,1)
plt.subplots_adjust(bottom=0.25)
sax = plt.axes([0.25, 0.1, 0.65, 0.03])
slide = Slider(sax, 'time', start_time, end_time, valinit=start_time)

#Initial plot
act_data = df.loc[df['timestamp'] == float(int(start_time))]
s, = ax.plot(act_data.x, act_data.y, 'o')

def update(timestamp):
    act_data = df.loc[df['timestamp'] == float(int(timestamp))]
    X = act_data.x
    Y = act_data.y

    #Update data based on slider
    s.set_xdata(X)
    s.set_ydata(Y)

    #Reset axis limits
    ax.set_xlim([X.min()*0.9,X.max()*1.1])
    ax.set_ylim([Y.min()*0.9,Y.max()*1.1])

    fig.canvas.draw()

slide.on_changed(update)
plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

You're awesome!

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.