疫情数据的可视化
1> matplotlib
导库命令:import matplot.pyplot as plt
2> 可视化流程
1.绘制画布及绘制图形
plt.figure(figsize=(8,6),dpi =80)
plt.plot(x,y,color,linestyle)
参数含义:color表示颜色,颜色的单词
linestyle表示线型,- -- : -.
plt.scatter(x,y,color,marker,s)
参数含义:color 同上
marker表示点的形状 o/v/^/s/*/D/d/h/H
s表示点的大小
2.添加元素
plt.title()
plt.xlabel()
plt.ylabel()
#表示x,y轴的取值范围
plt.xlim(元组)
plt.ylim(元组)
#表示x,y轴的刻度
plt.xticks(列表)
plt.yticks(列表)
3.显示
3>补充一些操作
pd.set_option('display.unicode.ambiguous_as_wide', True)#设置列名对齐
pd.set_option('display.unicode.east_asian_width', True)#设置列名对齐
pd.set_option('display.max_rows',None) #显示所有行
pd.set_option('display.max_columns',None)#显示所有列
pd.set_option('expand_frame_repr', False)#设置不换行
%matplotlib notebook
#功能3--数据可视化代码
#允许在notebook中显示动画
#动画绘图--animation
from matplotlib import animation #动画绘图库
import matplotlib.pyplot as plt #导入可视化库
import numpy as np
import random
from IPython.display import HTML #HTML中显示动画
import pandas as pd
pd.set_option('display.unicode.ambiguous_as_wide',True)
pd.set_option('display.unicode.east_asian_width',True) #设置列名对其
# 函数1--打开文件
def open_def(file_name):
file_name1 = open(file_name,encoding='utf-8') #用open命令按照utf-8格式打开
file_v1 = pd.read_csv(file_name1) #然后用pandas的命令读文件
file_v2 = file_v1.drop('Unnamed: 0',axis=1) #删除列,
return file_v2
file_name = "china_hist_data.csv"
file_v1 = open_def(file_name)
file_v3 = file_v1.iloc[:,[1,5,6,9,10,11,13]]
plt.rcParams['font.sans-serif'] = 'SimHei' #设置字体
plt.rcParams['axes.unicode_minus'] = False #字符显示,更多详见CSDN
fig = plt.figure(figsize = (8,6),dpi=80) #定义绘图画布
ax1 = fig.add_subplot(111) #绘制子图
x = list(file_v3.columns)[-1:-5:-1] #找出列标题中的后四个,
y = file_v3.values[:,3:] #找出数据值中,第3列开始的所有列
legend_v1 = list(file_v3['时间'].values) #以时间的值,作为图例项
def update(num): #要执行的函数
ax1.clear() #清除之前绘制的内容
color_v1=['r','b','y','g']
line_v1 = ['-','--','-.',':']
ax1.plot(x,y[num],color=random.choice(color_v1),linestyle=random.choice(line_v1))
for i in range(len(x)):
plt.annotate(xy=(i,y[num][i]+10),s=y[num][i])
plt.legend([legend_v1[num]])
return None
ani = animation.FuncAnimation(fig, update, frames=np.arange(y.shape[0]),interval= 1000,
blit=True,repeat=True)
#这里的frames在调用update函数是会将frames作为实参传递给“n”
# animation中的frames是,要执行函数多少次。一般是一个可迭代对象,将参数依次给了函数,
plt.show()
# HTML(ani.to_jshtml())