4

I would like to create a animation where my data points would gradually appear on my graph and freeze when all the data points have appeared. I've seen in done with correlations i'm just not too sure how to do it with just individual points themselves

This isn't something that will show anything particularly useful but i though it would look cool since i am trying to visualize some location data on a map

I know this isn't very clear so please as for clarifications, I'm not too sure how to phrase my problem very well.

Thanks

asts bins
  • 43
  • 1
  • 1
  • 6

1 Answers1

15

matplotlib.animation.FuncAnimation is the right tool for you. First create an empty graph, and then gradually add data points to it in the function. The following piece of code will illustrate it:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = np.arange(10)
y = np.random.random(10)

fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 1)
graph, = plt.plot([], [], 'o')

def animate(i):
    graph.set_data(x[:i+1], y[:i+1])
    return graph

ani = FuncAnimation(fig, animate, frames=10, interval=200)
plt.show()

The result (saved as gif file) is shown below: enter image description here

EDIT: To make the animation look stopped when finished in matplotlib window, you need to make it infinite (omit frames parameter in FuncAnimation), and set the frame counter to the last number in your frame series:

def animate(i):
    if i > 9:
        i = 9
    graph.set_data(x[:i+1], y[:i+1])
    return graph

ani = FuncAnimation(fig, animate, interval=200)

Or, which is better, you can set repeat parameter in FuncAnimation to False, as per answer to this question.

EDIT 2: To animate a scatter plot, you need a whole bunch of other methods. A piece of code is worth a thousand words:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

x = np.arange(10)
y = np.random.random(10)
size = np.random.randint(150, size=10)
colors = np.random.choice(["r", "g", "b"], size=10)

fig = plt.figure()
plt.xlim(0, 10)
plt.ylim(0, 1)
graph = plt.scatter([], [])

def animate(i):
    graph.set_offsets(np.vstack((x[:i+1], y[:i+1])).T)
    graph.set_sizes(size[:i+1])
    graph.set_facecolors(colors[:i+1])
    return graph

ani = FuncAnimation(fig, animate, repeat=False, interval=200)
plt.show()
Community
  • 1
  • 1
Andrey Sobolev
  • 12,353
  • 3
  • 48
  • 52
  • Also is there a way to set_data by individual points rather than lists? – asts bins Oct 22 '15 at 08:07
  • First question: that depends on whether you want to show the graph in matplotlib window, or save it as a file and embed into a presentation. Yes, it is possible in matplotlib window - see edit for details. If you'd like to stop animated file instead - that's up to presentation software. – Andrey Sobolev Oct 22 '15 at 08:44
  • Second question: Why do you need that? If you have a bunch of individual points that you want to display, you always have the possibility to put them in a list. – Andrey Sobolev Oct 22 '15 at 08:52
  • can .i change the marker sizes and color of the points in .data_set? – asts bins Oct 22 '15 at 09:32
  • i needed to do so and that is why i asked if i could .set data by individual points rather than lists. – asts bins Oct 22 '15 at 09:42
  • Ok, then you need a scatter plot. Scatter plots (`PathCollections`) instead of `set_data` have `set_facecolors`, `set_sizes` and `set_offsets` methods. `set_offsets` takes 2d array as an argument (contrary to `set_data`). See another edit. – Andrey Sobolev Oct 22 '15 at 10:21
  • Thanks that covers about everything – asts bins Oct 23 '15 at 06:53
  • actually 2 small things, for the 'i' in animate(i), its that the frame number? and what is the purpose of the '.T ' in .graph_offset? – asts bins Oct 23 '15 at 07:09
  • `i` is the frame number, and `.T ` stands for transposing test data, as `graph.set_offsets` expects as input 2D array with eath row representing one point. – Andrey Sobolev Oct 23 '15 at 08:41
  • Thank you so much for your answers, it would took me so much time to try to do this with just with whats out there. – asts bins Oct 23 '15 at 10:15
  • Can you please share how to set the color using a color map based on a third variable for the scatter plot? – user42 Jul 02 '21 at 08:07