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

Matplotlib有两个模块:

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 @   七星望  阅读(360)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示