批量根据excel数据绘制柱状图

要批量根据Excel数据绘制柱状图,可以使用Python中的pandas和matplotlib库来实现。下面是示例代码:

import pandas as pd

import matplotlib.pyplot as plt

import os

def draw_bar_chart_from_excel(file_path, x_column, y_column, output_folder):

读取Excel文件

df = pd.read_excel(file_path)

遍历数据,并绘制柱状图

for index, row in df.iterrows():

获取x轴和y轴数据

x_data = rowx_column

y_data = rowy_column

创建柱状图

plt.bar(x_data, y_data)

设置标题和坐标轴标签

plt.title(f"Bar Chart - {x_data}")

plt.xlabel(x_column)

plt.ylabel(y_column)

构造输出文件路径

output_file_name = f"{x_data}.png"

output_file_path = os.path.join(output_folder, output_file_name)

保存柱状图为PNG文件

plt.savefig(output_file_path)

清除图形以便绘制下一个柱状图

plt.clf()

调用函数并传入Excel文件路径、x轴列名称、y轴列名称和输出文件夹路径

draw_bar_chart_from_excel('your_excel_file.xlsx', 'x_column_name', 'y_column_name', 'output_folder_path')

请确保您已安装所需的依赖包(pandas和matplotlib)并将文件路径、x轴列名称、y轴列名称和输出文件夹路径替换为实际的值。这段代码会读取指定的Excel文件,并根据每行的数据绘制柱状图,然后将每个柱状图保存为以x轴数据命名的PNG文件。

注意:在运行代码之前,请确保在Python环境中已安装所需的库(如pandas和matplotlib)并导入它们。

相关推荐
用户0332126663677 小时前
使用 Python 设置 Excel 行列自适应 【代码示例】
python·excel
企业数字化笔记7 小时前
固定资产Excel批量导入怎么防重复?资产编码、重复检查和错误回执
java·后端·excel
CORNERSTONE36512 小时前
低代码开发有哪些应用场景?
低代码·excel·erp·mes·企业数字化
小葱炖豆腐1 天前
python绘制excel折线图
python·excel·numpy·pandas·matplotlib
cspttty2 天前
2026年财务分析岗JD中的Excel、SQL、BI与业务分析要求
大数据·sql·excel
cspttty2 天前
HR数字化校招准备路线:Excel、SQL、BI和AI工具怎么学
人工智能·sql·excel
鲲穹AI种草2 天前
Excel 表格批量处理与数据整理,多款表格工具能力客观记录
excel·表格处理
cspttty3 天前
2026秋招供应链分析师技能栈:SQL、Excel、BI与业务分析
数据库·sql·excel
Zzq_Fighting3 天前
Excel批量匹配数据
excel
E_ICEBLUE4 天前
Python 实现 Excel 转 Markdown,支持工作表、单元格区域和批量处理
python·excel·markdown·格式转换