news 2026/7/13 3:02:34

Python学习之使用pycharts

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python学习之使用pycharts

简介:

通过读取【charts_demo.xlsx】文件数据,使用pycharts生成名为【page_simple_layout.html】的HTML图表。

from pyecharts.charts import Bar,Page from pyecharts import options as opts from openpyxl import load_workbook import numpy as np import math from pyecharts.globals import ThemeType import os #split the arr into N chunks def chunks(arr, m): n = int(math.ceil(len(arr) / float(m))) return [arr[i:i + n] for i in range(0, len(arr), n)] def build_bar(): hor_num = ws['C5'].value ord_num = ws['C4'].value xtype=[] xline=[] yline=[] yline2=[[]] for col in ws.iter_cols(min_col=8, max_col=8+ord_num-1, min_row=3, max_row=3): for cell in col: xtype.append(cell.value) for col in ws.iter_cols(min_col=7, max_col=7, min_row=4, max_row=hor_num+3): for cell in col: xline.append(cell.value) for col in ws.iter_cols(min_col=8, max_col=8+ord_num-1, min_row=4, max_row=hor_num+3): for cell in col: yline.append(format((cell.value),'.0f'))#yline.append(format((cell.value),'.1f')) yline2 = chunks(yline,ord_num) #print(xtype) #print(xline) #print(yline) #print(yline2) bar2 = Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,width='1200px')) #修改源文件使保存的图片背景为白色 opts.ToolBoxFeatureSaveAsImageOpts(background_color="white") bar2.add_xaxis(xline) j=0 for i in yline2: bar2.add_yaxis(xtype[j],i) j=j+1 if j>3: j=0 bar2.set_global_opts( title_opts=opts.TitleOpts(title=str(ws['G3'].value)), tooltip_opts=opts.TooltipOpts(trigger="axis"), toolbox_opts=opts.ToolboxOpts(is_show=True,feature=opts.ToolBoxFeatureOpts(save_as_image=opts.ToolBoxFeatureSaveAsImageOpts(background_color="#F7F7F7"))), xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=True), #datazoom_opts=opts.DataZoomOpts(range_start=10,range_end=90), # 坐标轴进行缩放 ) return bar2 excel_name = "charts_demo.xlsx" html_name = "page_simple_layout.html" if __name__=='__main__': current_path = os.getcwd() excel_path = current_path+"\\"+excel_name html_patch = current_path+"\\"+html_name wb = load_workbook(excel_path, data_only=True) ws=wb['Sheet1'] # 简单布局 page = Page(layout=Page.SimplePageLayout) # 将上面定义好的图添加到 page page.add(build_bar()) page.render(html_patch) print('finish')

通过指定单元格G3的内容为图表名称,指定纵坐标个数【语文、数学、总分】,横坐标个数【张三、李四、王五、赵六、孙七、周八、吴九】:

运行py程序后,可以通过右上角一排按钮转换为折线图、柱状图,保存图片等。效果如下:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/13 10:22:25

Three.js沉浸式数字展馆开发终极指南:从零构建Web3D交互体验

在当今Web技术快速发展的时代,基于Three.js的3D展示项目正成为企业展示、产品营销和教育培训的重要载体。然而,许多开发者在构建Web3D应用时常常面临性能瓶颈、交互体验不佳等技术难题。本文将深入解析一个成熟的数字展馆项目,为你提供完整的…

作者头像 李华
网站建设 2026/7/12 11:23:16

9、Eclipse集成开发环境使用指南

Eclipse集成开发环境使用指南 1. 快捷启动Eclipse 可以在 /usr/bin 目录下创建一个名为 eclipse 的脚本文件,内容如下: #!/bin/bash /usr/local/eclipse/eclipse &第一行表明这是一个shell脚本,第二行用于执行Eclipse。 & 符号表示在后台运行该程序并返回…

作者头像 李华
网站建设 2026/7/13 17:49:42

EnergyPlus 建筑能源模拟完整指南:从基础原理到高级应用

EnergyPlus 建筑能源模拟完整指南:从基础原理到高级应用 【免费下载链接】EnergyPlus EnergyPlus™ is a whole building energy simulation program that engineers, architects, and researchers use to model both energy consumption and water use in building…

作者头像 李华
网站建设 2026/7/12 4:51:20

轻松高效生成音频字幕:OpenLRC完整指南与多语言LRC文件制作

轻松高效生成音频字幕:OpenLRC完整指南与多语言LRC文件制作 【免费下载链接】openlrc Transcribe and translate voice into LRC file using Whisper and LLMs (GPT, Claude, et,al). 使用whisper和LLM(GPT,Claude等)来转录、翻译你的音频为字幕文件。 …

作者头像 李华
网站建设 2026/7/13 12:08:47

鸿蒙投屏神器HOScrcpy:5分钟快速上手完整指南

鸿蒙投屏神器HOScrcpy:5分钟快速上手完整指南 【免费下载链接】鸿蒙远程真机工具 该工具主要提供鸿蒙系统下基于视频流的投屏功能,帧率基本持平真机帧率,达到远程真机的效果。 项目地址: https://gitcode.com/OpenHarmonyToolkitsPlaza/HOS…

作者头像 李华