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])
相关推荐
weixin_4462608514 小时前
AgentsCAD:基于多智能体大模型推理与几何特征识别的FDM增材制造自动化设计
人工智能·自动化·制造
bkl_921314 小时前
AI Agent 零基础入门:基于GPT-5.5搭建自动化工作流全实操
人工智能·gpt·自动化
Xi-Xu14 小时前
什么时候需要 Multi-agent:不是分工,而是运行边界
人工智能·经验分享·安全
冬哥聊AI14 小时前
蚂蚁三面追问:Agent 有哪些设计模式?别背名词了,四层框架才是正确答法
人工智能
啥都生14 小时前
arXiv正式单飞,承诺继续免费
人工智能
水如烟14 小时前
孤能子视角:三十六计之隔岸观火——时序相位选择
人工智能
‿hhh14 小时前
Dify核心模块详解:从文本生成到智能体
人工智能·学习·microsoft·agent·上下文·记忆
大鱼>14 小时前
模型可解释性:特征重要性/SHAP/LIME
人工智能·python·机器学习·lstm
霖大侠14 小时前
Decoupled and Reusable Adaptation for Efficient Cross-Modal Transfer
人工智能·深度学习·算法·机器学习·transformer