Python数据科学入门(matplotlib)笔记04

本文介绍了Python中Matplotlib的基本用法,包括简单的绘图示例、子图绘制等,并展示了如何利用Pandas进行数据绘图,涵盖Series与DataFrame的多种图表形式,如条形图、面积图及直方图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
ABCD
03985
14935
26665
36526
43656
# 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')

图三

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值