1
Time    M1  M2  M3  M4  M5
1       1   2   3   1   2
2       1   3   3   1   2
3       1   3   2   1   3
4       2   2   3   1   2
5       3   3   3   1   3
6       2   3   4   1   4
7       2   3   4   3   3
8       3   4   4   3   4
9       4   4   5   3   3
10      4   4   5   5   4

Above is a data table with 10 periods of time. For each time, there are 5 metrics. How to make an animated scatter (bubble) chart over time (10 time periods) using Python?

Please note, x-axis is fixed list = [1, 2, 3, 4, 5] and y-axis is list of 5 elements i.e. time 1, y=[1, 2, 3, 1, 2]; time 2, y=[1, 3, 3, 1, 2].

2
  • What exactly is the problem? There are enough examples showing how to animate a graph. If you have a specific problem implementing any of the solutions you find, you may show the code, add a clear problem description and nominate the question for reopening. In the meantime you may read How to Ask and minimal reproducible example. Commented Jun 17, 2017 at 23:24
  • where is the question and answer you refer to? @ ImportanceOfBeingErnest Commented Jun 17, 2017 at 23:27

1 Answer 1

2

There are just so many errors in your code, I can't list them all. But the following works.

import matplotlib.pyplot as plt
from matplotlib import animation as animation
import numpy as np
import pandas as pd
import io

u = u"""Time    M1  M2  M3  M4  M5
1       1   2   3   1   2
2       1   3   3   1   2
3       1   3   2   1   3
4       2   2   3   1   2
5       3   3   3   1   3
6       2   3   4   1   4
7       2   3   4   3   3
8       3   4   4   3   4
9       4   4   5   3   3
10      4   4   5   5   4"""

df_Bubble = pd.read_csv(io.StringIO(u), delim_whitespace=True)

time_count = len(df_Bubble) 
colors = np.arange(1, 6) 
x = np.arange(1, 6) 
max_radius = 25 
fig, ax = plt.subplots() 
pic = ax.scatter(x, df_Bubble.iloc[0, 1:], s=100, c=colors) 
pic.set_offsets([[np.nan]*len(colors)]*2)
ax.axis([0,7,0,7])

def init(): 
    pic.set_offsets([[np.nan]*len(colors)]*2)
    return pic,

def updateData(i): 
    y = df_Bubble.iloc[i, 1:] 
    area = np.pi * (max_radius * y / 10.0) ** 2 
    pic.set_offsets([x, y.values]) 
    pic._sizes = area
    i+=1 
    return pic, 

ani = animation.FuncAnimation(fig, updateData, frames=10, interval = 50, blit=True, init_func=init) 

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

7 Comments

what if i do not want to use pic.set_offsets, I just want to update the data of y axis for each time period. Also, How to show in the plot of time period? I want similar function like youtube.com/watch?v=JkpbY08swyA
pic.set_offsets updates the data. You cannot update the y data only for a scatter plot. "How to show in the plot of time period?" is not understandable. The code for the graph in the video is can be found in the link below the video.
In the function def updateData(i) block, how can I add a time label (string/text) on the upper left corner of the plot, and as time changes (from 1 to 10), on the plot it will show "Time: 1", "Time: 2", ....."Time: 10"? Thanks very much. I am new to visualization using Python.
www.google.de/search?q=matplotlib+update+text+in+animation
Below works: pic = ax.scatter(x, df_Bubble.iloc[i, 1:], s=area, c=colors) text = ax.text(0.5, 5.5, 'Time: ' + str(i), fontsize=12) #pic.set_data([x, y]) #pic._sizes = area i+=1 return pic, text
|

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.