Article directory
1. Data preparation for real-time data visualization
import pandas as pd
import matplotlib.pyplot as plt
# 设置一般的样例数据
x=[0,1,2,3,4] # x轴数据
y=[0,1,2,3,4] # y轴数据
# 设置多维数据
dev_x=[25,26,27,28,29,30] # 开发者的年龄
dev_y=[7567,8789,8900,11560,16789,25231] #收入情况
py_dev_y=[5567,6789,9098,15560,20789,23231] # python开发者
js_dev_y=[6567,7789,8098,12356,14789,20231] # java开发者
devsalary=pd.DataFrame([dev_x,dev_y,py_dev_y,js_dev_y])
01. Set the chart theme style
The classic style was used before:
plt.style.use('classic')
plt.plot(x,y)
Now change to 538 style:
plt.style.use('fivethirtyeight') # 538统计样式
from IPython.display import HTML # 在实现动态的过程中必须引入的库
plt.plot(x,y)
02 Using sample data
import random
from itertools import count
index=count()
x1=[]
y1=[]
x1.append(next(index))
y1.append(random.randint(0,50))
plt.plot(x1,y1)
Let's try the manual effect first:
this effect is the animation we want to achieve.
def animate(i):
x1.append(next(index))
y1.append(random.randint(0,50))
plt.plot(x1,y1)
from matplotlib.animation import FuncAnimation
ani=FuncAnimation(plt.gcf(),animate,interval=1000) # interval=1000代表时间间隔,数值越小,则时间间隔越短
HTML(ani.to_jshtml())
The above video is a demonstration of the generation process of the data, and you will find that the color changes every time it changes.
There is also a complete picture under the video, indicating the number of sequences generated by Python in the time node:
if you want the image to change on the original basis every time it changes, use the following:
plt.cla()
def animate(i):
x1.append(next(index))
y1.append(random.randint(0,50))
plt.cla() #每次变化的时候都是在原有基础上改变
plt.plot(x1,y1)
2. Use movie box office data to make animations
Dynamic real-time data is often related to the timeline. The data used this time: cnboo1.xlsx
I put it in my code cloud, please move if you need it: cnboo1.xlsx
import pandas as pd
cnbodf=pd.read_excel('cnboo1.xlsx')
cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)
def mkpoints(x,y):
return len(str(x))*(y/25)-3
cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)
cnbodfgb=cnbodfsort.groupby("TYPE").mean(["bo","prices","persons","points"])
cnbodfsort['type1']=cnbodfsort['TYPE'].apply(lambda x:x.split("/")[0])
cnbodfgb=cnbodfsort.groupby(["type1"])["ID","BO","PRICE","PERSONS","points"].mean()
cnbodfgbsort=cnbodfgb.sort_values("BO",ascending=False)
x=cnbodfsort['PERSONS']
y=cnbodfsort['PRICE']
plt.plot(x,y)
When we take the number of people and movie ticket prices as the data on the x and y axes, we can see that the data is more chaotic:
dynamic real-time data lines are often related to time.
So we need to redefine the data.
y1=y.to_list()
X1=x.to_list()
def animate(i):
x1.append(next(index))
y1.append(y[random.randint(1,49)]) # 表示在50条电影数据中随机选择一条
plt.cla() #每次变化的时候都是在原有基础上改变
plt.plot(x1,y1)
x1=[]
y1=[]
ani=FuncAnimation(plt.gcf(),animate,interval=1000)
HTML(ani.to_jshtml())
The final result is as follows: