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].

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user7246029
  • 31
  • 1
  • 5
  • 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 [ask] and [mcve]. – ImportanceOfBeingErnest Jun 17 '17 at 23:24
  • where is the question and answer you refer to? @ ImportanceOfBeingErnest – user7246029 Jun 17 '17 at 23:27

1 Answers1

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()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 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 https://www.youtube.com/watch?v=JkpbY08swyA – user7246029 Jun 18 '17 at 02:35
  • `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. – ImportanceOfBeingErnest Jun 18 '17 at 07:39
  • 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. – user7246029 Jun 18 '17 at 18:17
  • www.google.de/search?q=matplotlib+update+text+in+animation – ImportanceOfBeingErnest Jun 18 '17 at 18:24
  • 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 – user7246029 Jun 18 '17 at 18:31
  • how to save the animation - i know many people ask this. but i tried every solution online, finally get a win5 access denied. i downloaded the ffmpeg package and save in my local c disk. I write plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg' and then FFwriter = animation.FFMpegWriter() ani.save('test.mp4', writer=FFwriter) no luck. I guess is the path problem. but in the downloaded package of ffmpeg, which exact file should I use. Any advice/solution would be appreciated. Thanks! – user7246029 Jun 19 '17 at 18:56
  • The path to `animation.ffmpeg_path` needs to be the complete path to the ffmpeg file. But please do not ask completely new questions in the comments to another question. Instead, ask a new question sticking to [ask], i.e. provide the code that causes the problem, a full error traceback, a clear problem description including in how far other answers have not helped and all relevant details. – ImportanceOfBeingErnest Jun 20 '17 at 08:03