Python数据科学入门学习笔记——Matloptlib
什么是matloptlib
matloptilib是一个Python包,用于2D绘图,3D绘图也可以安装额外的包,强大流行,有很多扩展。
1.Matplotlib Architecture
- Backend:主要处理把图像显示到哪里和画到哪里?
- Artist:图像显示成什么样? 大小位置等。
- Scripting:pyplot,Python语法和API
一、matplotlib 简单绘图示例
import numpy as np
import matplotlib.pyplot as plt
a = [1,2,3]
b = [4,5,6]
# 如果不想每次都调用 plt.show() 来显示图像
# 使用 %matplotlib inline matplotlib 内置的魔法函数
#每次调用plot() 就会默认显示图像,不用再调用show()
plt.plot(a,b,'r--')
plt.show()
c = [2,5,8]
d = [9,4,1]
plt.plot(a,b,'r*',c,d,'b--')
t = np.arange(0.0,2.0,0.1)
s = np.sin(t*np.pi)
plt.plot(t,s,'y--',label='AA')
plt.plot(t*2,s,'r*',label='BB')
plt.xlabel('t')
plt.ylabel('s')
plt.title('Demo')
plt.legend() # 显示示例label
二、subplot 子图绘制
import numpy as np
import matplotlib.pyplot as plt
t1 = np.arange(0.0,2.0,0.1)
t2 = t1 * 2
s = np.sin(t*np.pi)
plt.subplot(2,1,1) # 表示两行一列 图1
plt.plot(t1,s,'y--',label='AA')
plt.subplot(212) # 不用逗号也可以,表示两行一列 图二
plt.plot(t2,s,'r*',label='BB')
plt.xlabel('t2')
plt.ylabel('s')
subplots
figure,ax = plt.subplots(2,2) # 表示两行两列的画布
ax[0][0].plot(t1,s)
ax[0][1].plot(t2,s)
三、Pandas绘图之Series
import numpy as np
import pandas as pd
from pandas import Series
import matplotlib.pyplot as plt
# cumsum() 表示累加的和
s1 = Series(np.random.randn(10)).cumsum()
s2 = Series(np.random.randn(10)).cumsum()
# bar 表示 条形图 默认 line 线形图
# gird 是否显示 格子
s1.plot(kind='line',grid=True,label='AAA',title='Demo',style='--')
s2.plot(label='BBB')
plt.legend() # 显示图例
plt.show()
sublots 子图绘制
fig, ax = plt.subplots(2,1)
ax[0].plot(s1) # 方法一
s2.plot(ax=ax[1],kind='bar') # 方法二
四、Pandas绘图之DataFrame
import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
df = DataFrame(
np.random.randint(1,10,40).reshape(10,4),
columns=['A','B','C','D']
)
df.head()
A | B | C | D | |
---|---|---|---|---|
0 | 3 | 9 | 8 | 5 |
1 | 4 | 9 | 3 | 5 |
2 | 6 | 6 | 6 | 5 |
3 | 6 | 5 | 2 | 6 |
4 | 3 | 6 | 5 | 6 |
# barh 横向 条形图
# stacked 是否堆叠
df.plot(kind='bar',stacked='True')
# area 填充的图
# 默认对行进行 绘图 可通过 df.T.plot() 转置后改为对'列'绘图
df.plot(kind='area')
# iloc[5] 第五行
df.iloc[5].plot()
# 一行行画图
for i in df.index:
df.iloc[i].plot(label=str(i))
plt.legend()
plt.show()
五、直方图
import numpy as np
import pandas as pd
from pandas import Series
import matplotlib.pyplot as plt
s = Series(np.random.randn(1000))
# 频数直方图 分布图 下图的横轴表示 某个范围内的频数
# bins 表示分区数目
re = plt.hist(s,rwidth=0.9,bins=20,color='r')
print(type(re))
print('--------------------')
print(len(re)) # 长度
print('--------------------')
print(re[0]) # 频数
print('--------------------')
print(re[1]) # 范围
print('--------------------')
print(re[2]) # 数据类型 和 (区间)数目
<class 'tuple'>
--------------------
3
--------------------
[ 1. 0. 4. 8. 15. 36. 45. 74. 128. 128. 125. 134. 115. 77.
43. 36. 15. 11. 4. 1.]
--------------------
[-3.66698017 -3.31914432 -2.97130846 -2.62347261 -2.27563675 -1.92780089
-1.57996504 -1.23212918 -0.88429333 -0.53645747 -0.18862162 0.15921424
0.50705009 0.85488595 1.20272181 1.55055766 1.89839352 2.24622937
2.59406523 2.94190108 3.28973694]
--------------------
<a list of 20 Patch objects>
# 数据直接绘图 对比上图
plt.plot(s)
# 密度图
s.plot(kind='kde')