数据可视化:python matplotlib小试牛刀

Matplotlib有两个模块:

1) 绘图API:pyplot, 可这样导入import matplotlib.pyplot as plt   

2)集成库:pylab, 是matplotlib Scipy Numpy的集成库

import pandas as pd
import matplotlib.pyplot as plt
from pylab import mpl
#设置标题字体
mpl.rcParams['font.sans-serif']=['FangSong']

df = pd.read_csv('data.csv', index_col = '年份')
df.head()
#dataframe读取数据的方式,行属性
x=df.index.values
#dataframe读取数据的方式,列名
y=df['人均GDP(元)'].values
#构造figure对象 ,和子窗口
fig,ax = plt.subplots()
ax.plot(x,y,'r--o')
ax.set(title='GDP走势',xlabel='年份')
print(df.head())
plt.show()
#第二种画折线图的方式2
#fig=plt.figure()
#设置subplot(x,y,z) x:行数 y:列数,z:接下来Plot画图位置,放在第几个自窗口
#ax=fig.add_subplot(111)

#柱形图
#df['人均GDP(元)'].plot(kind='bar')
#plt.show()

  

加上!chcp 65001,解决pycharm里的python console不识别中文的问题

 

3)通过matplotlib可以作动画不,回答是肯定的,以下是结合matplotlib.animation, 做了一个在sin曲线上运动的小动画。

主要通过调用animation.FuncAnimation来实现的。


import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def update_points(num):
    print(num)
    point_ani.set_data(x[num], y[num])
    return  point_ani,

x = np.linspace(0, 2*np.pi,100)
y = np.sin(x)

fig = plt.figure(tight_layout = True)
plt.plot(x,y)
point_ani, = plt.plot(x[0], y[0], "ro")
plt.grid(ls="--")
ani = animation.FuncAnimation(fig, update_points , np.arange(0,100),interval =100,blit =True)
### where to draw, how to draw , init list
plt.show()

posted @ 2020-03-01 10:51  七星望  阅读(336)  评论(0编辑  收藏  举报