首先画条形图
import pandas as pd
import matplotlib.pyplot as plt
#这个数据是鸢尾花数据,随手拿来用了,动态图为每个特征值随行数增长发生的变化
data = pd.read_csv('C:/Users/Dell/Desktop/郑佳重要/python/data/iris.csv')
#获得名称
objects = data.columns
#得到第一行数据
performance = data.iloc[0]
#添加颜色
colors = ['#90d595','#444693','#fedcbd','#ef5b9c','#494e8f','#F46141']
fig,ax = plt.subplots(figsize = (15,8))
plt.barh(objects, performance, align='center',color = colors, alpha=0.7)
plt.show()
本文中动态化使用的库是matplotlib.animation.FuncAnimation
它的原理是将图片封装为函数,然后改变某个值使函数图像发生变化,此时可以得到很多张图片,加速播放图片就变成视频了。
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation
data = pd.read_csv('C:/Users/Dell/Desktop/郑佳重要/python/data/iris.csv')
#获得名称
objects = data.columns
fig,ax = plt.subplots(figsize = (15,8))
#得到动态数据
def draw(i):
performance=data.iloc[i]
colors = ['#90d595', '#444693', '#fedcbd', '#ef5b9c', '#494e8f', '#F46141']
plt.barh(objects, performance, align='center', color=colors)
ani = animation.FuncAnimation(fig = fig,func = draw, frames =len(data))
plt.show()
此时得到的动态图并不是按照数据大小排序的,需要在画图之前,选取数据时进行调整。
理论上没错,但是运行时有问题。
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
data = pd.read_csv('C:/Users/Dell/Desktop/郑佳重要/不确定理论/决策树/t3g32.csv')
#获得名称
fig,ax = plt.subplots(figsize = (15,8))
#得到动态数据
def draw(i):
data1 = data.sort_values(by=i, axis=1)
performance=data1.iloc[i]
colors = ['#90d595', '#444693', '#fedcbd', '#ef5b9c', '#494e8f', '#F46141']
objects = data1.columns
plt.barh(objects, performance, align='center', color=colors)
ani = animation.FuncAnimation(fig,draw, len(data))
plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)