pyplot+pandas实现操作excel及画图

1、安装jupyter lab

pip install jupyterlab

启动 建议在指定的项目文件夹下 开启cmd窗口并执行

jupyter lab

启动后会自动打开浏览器访问

2、安装依赖

pip install matplotlib

pip install xlrd

pip install pandas

3、读取excel

复制代码
import pandas as pd

df = pd.read_excel('his.xls')


# 读取列名
for i in df.columns:
    print(i)

df.columns[0]


# 读取指定sheet 名
filePath = 'his.xlsx'
df = pd.read_excel(r'his.xlsx', sheet_name=1)

# 场景2:excel 中第 2 行才是我们想要的标题(即:header=1)
df = pd.read_excel(filePath, header=1)

# 读取 Excel,指定索引列
df= pd.read_excel(filePath, index_col='ID')

# 读取前 3 行数据(默认 5 行)
print(df.head(3))

# 读取后 3 行数据(默认 5 行)
print(df.tail(3))

# 数据筛选
# 读取第一列所有数据
df.iloc[:, 0]

遍历所有sheet

复制代码
import pandas as pd
excel_file = pd.ExcelFile('his.xls')

for sheet_name in excel_file.sheet_names:
    print(sheet_name) 
    df = excel_file.parse(sheet_name)
    print(df.columns)

4、画图

多个子图

复制代码
import pandas as pd
import numpy as np 
import matplotlib.pyplot as plt 

# 从上一步得到excel的列
columns = df.columns
size = columns.size

#新建一个画布
fig = plt.figure()  

# 设置多行 一列的表格 figsize 用于设置画布大小  是否共享x轴坐标值 sharex
f, ax = plt.subplots(size, 1, figsize=(10,30), sharex= True, sharey=False) 


for i in range(size):
    # 将每一列数据作为一个表格,用于绘制,如果是多列,则ax[i:xx]
    ax[i].plot(df.iloc[:, i])
    ax[i].set_title(columns[i])

plt.show()

多组数据一组图

复制代码
# c='black' 设置线条颜色
ax[0].plot(df.iloc[:, 0])
ax[0].plot(df.iloc[:, 1])
相关推荐
代码洲学长几秒前
卷积神经网络CNN
人工智能·神经网络·cnn
l1t4 分钟前
利用小米mimo为精确覆盖矩形问题C程序添加打乱函数求出更大的解
c语言·开发语言·javascript·人工智能·算法
weixin_398187754 分钟前
YOLOv11 轻量级移动端网络ShuffleNetV2集成指南(详注)
人工智能·yolo
_Li.7 分钟前
机器学习-贝叶斯公式
人工智能·机器学习·概率论
luoganttcc10 分钟前
详细分析一下 国富论里里面 十一章 关于白银价格的 论述
人工智能
GEO AI搜索优化助手20 分钟前
生态震荡——当“摘要”成为终点,知识价值链的重塑与博弈
人工智能·搜索引擎·生成式引擎优化·ai优化·geo搜索优化
IT_陈寒21 分钟前
JavaScript 性能优化:5个被低估的V8引擎技巧让你的代码提速50%
前端·人工智能·后端
哔哩哔哩技术26 分钟前
SABER: 模式切换的混合思考模型训练范式
人工智能
baby_hua28 分钟前
20251011_Pytorch从入门到精通
人工智能·pytorch·python
لا معنى له33 分钟前
学习笔记:循环神经网络(RNN)
人工智能·笔记·学习·机器学习