python主流绘图工具:matplotlib ,seaborn,pandas ,openpyxl ,xslwriter

openpyxl :首先说下这个官网的demo,看的有点懵,没有具体说明多个图在一个excel引用不通ws Rererence 只是简单的一个deepcopy 用的一点懵逼,反正我是没看懂,其次多sheet写入也未做拓展,

看了下博客哪些水军一篇粘贴复制毫无新意:

下面以openpyxl 3d bar 柱状图为例:演示效果多sheet 生成柱状图:

官网:https://openpyxl.readthedocs.io/en/latest/charts/bar.html#d-bar-charts

from openpyxl import Workbook
from openpyxl.chart import (
Reference,
Series,
BarChart3D,
) def bar_3d(configurations: dict):
""""
paint 3d bar in the excel ,
configuration={"data":None,"Title":None,"sheet_name":None,"index":None}
data:[
[姓名,column1,column2],
[value_name,value_col1,value_col2],
[value_name2,value_column2,value_column2]
]
"""
wb = Workbook()
for configuration in configurations:
sheet = configuration["sheet_name"]
ws = wb.create_sheet(sheet, index=configuration["index"])
rows = configuration["data"]
rows.insert(0, configuration["axis_x"])
for row in rows:
ws.append(row)
data = Reference(ws, min_col=2, min_row=1, max_col=3, max_row=7)
titles = Reference(ws, min_col=1, min_row=2, max_row=7)
chart = BarChart3D()
chart.title = configuration["Title"]
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)
chart.height=16
chart.width=28
chart.shape="box" ws.add_chart(chart, "E5")
save_path = "test_static.xlsx"
wb.save(save_path)

  解释参数:configurations 是一个存放许多configranition 的列表:每个configration 结构如注释:

configuration={"data":None,"Title":None,"sheet_name":None,"index":None}
data:[ [value_name,value_col1,value_col2],
[value_name2,value_column2,value_column2]
]
data就是要写入的数据包含header 和value,data[0]是表头,data[1:]全部是数据,index代表插入excel的sheet的index也就是第几个sheet,Title是绘制柱状图的title:
chart.height是图表的高度,width是宽度,add_chart 是插入chart 方法“E5"指定在excel插入位置,
rows.insert(0, configuration["axis_x"]) 这里插入是type分类的名称也就是rows.insert(0,[姓名,column1,column2])
,看个实际效果吧这个type就对应周日饱和,和bug提交量两列

多个sheet查看:

第二部分:使用pandas 绘图结合xslwriter:

官网:https://xlsxwriter.readthedocs.io/example_pandas_chart_columns.html

我就直接上代码了数据自己造了个用pandas:

import pandas as pd

def panda_chart(df_list, cols, title_x, title_y):
"""
data of narray
index of data_frame: [0,1,2,3]
cols numbers of static columns
""" writer = pd.ExcelWriter('pandas_chart_columns2.xlsx', engine='xlsxwriter')
for i, df in enumerate(df_list):
# df = pd.DataFrame(data, index=None, columns=["姓名", "饱和度", "人力"])
sheet_name = f'Sheet{i}'
df.to_excel(writer, sheet_name=sheet_name,index=False)
workbook = writer.book
worksheet = writer.sheets[sheet_name]
chart = workbook.add_chart({'type': 'column'})
# set colors for the chart each type .
colors = ['#E41A1C', '#377EB8'] # , '#4DAF4A', '#984EA3', '#FF7F00']
# Configure the series of the chart from the dataframe data.
for col_num in range(1, cols + 1):
chart.add_series({
'name': [f'{sheet_name}', 0, col_num],
'categories': [f'{sheet_name}', 1, 0, 4, 0], # axis_x start row ,start col,end row ,end col
'values': [f'{sheet_name}', 1, col_num, 4, col_num], # axis_y value of
'fill': {'color': colors[col_num - 1]}, # each type color choose
'overlap': -10,
}) # Configure the chart axes.
chart.set_x_axis({'name': f'{title_x}'})
chart.set_y_axis({'name': f'{title_y}', 'major_gridlines': {'visible': False}})
chart.set_size({'width': 900, 'height': 400})
# Insert the chart into the worksheet.
worksheet.insert_chart('H2', chart)
writer.save() if __name__ == '__main__':
data=[("a",2,4),("b",5,7)]
df = pd.DataFrame(data, index=None, columns=["姓名", "饱和度", "人力"])
panda_chart([df],2,"title x","title y")

  


pandas 进行excel绘图的更多相关文章

  1. 深入理解pandas读取excel,txt,csv文件等命令

    pandas读取文件官方提供的文档 在使用pandas读取文件之前,必备的内容,必然属于官方文档,官方文档查阅地址 http://pandas.pydata.org/pandas-docs/versi ...

  2. Python利用pandas处理Excel数据的应用

    Python利用pandas处理Excel数据的应用   最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做 ...

  3. 51-python3 pandas读写excel

    转载自:https://blog.csdn.net/brink_compiling/article/details/76890198?locationNum=7&fps=1 0. 前言Pyth ...

  4. pandas 将excel一列拆分成多列重新保存

    利用pd.read_excel   做到将第二列“EVT-LBL”按“-”分割后重新加三列在df后面 1 读取表格df 2. 分割第二列短横连接的数字,保存到df2---- 参考:str.spilt( ...

  5. 【python基础】利用pandas处理Excel数据

    参考:https://www.cnblogs.com/liulinghua90/p/9935642.html 一.安装第三方库xlrd和pandas 1:pandas依赖处理Excel的xlrd模块, ...

  6. Python 使用Pandas读取Excel的学习笔记

    这里介绍Python中使用Pandas读取Excel的方法 一.软件环境: OS:Win7 64位 Python 3.7 二.文件准备 1.项目结构: 2.在当前实验文件夹下建立一个Source文件夹 ...

  7. 机器学习之数据预处理,Pandas读取excel数据

    Python读写excel的工具库很多,比如最耳熟能详的xlrd.xlwt,xlutils,openpyxl等.其中xlrd和xlwt库通常配合使用,一个用于读,一个用于写excel.xlutils结 ...

  8. pandas read excel文件碰到的一个小问题

    今天利用pandas读取excel时,爆出如下错误: 代码为: import pandas as pd db_eua=pd.read_excel('db_eua.xlsx',sheetname='EU ...

  9. Python用Pandas读写Excel

    Pandas是python的一个数据分析包,纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.Pandas提供了大量能使我们快速便捷地处理数据的函数和方法. Pandas官方文档 ...

随机推荐

  1. Runtime.addShutdownHook用法

    一.什么是ShutdownHook? 在Java程序中可以通过添加关闭钩子,实现在程序退出时关闭资源.平滑退出的功能. 使用Runtime.addShutdownHook(Thread hook)方法 ...

  2. 本地.local域名访问实现

    苹果的 Bonjour 的多址广播域名服务(mDNS) 使用".local "后缀来识别 Bonjour 可访问的设备 比如树莓派可以用用raspberrypi.local去访问非 ...

  3. JDK 动态代理的实现

    JDK 动态代理的实现 虽然在常用的 Java 框架(Spring.MyBaits 等)中,经常见到 JDK 动态代理的使用,也知道了怎么去写一个 JDK 动态代理的 Demo,但是并不清楚实现原理. ...

  4. Tram POJ - 1847 spfa

    #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #inc ...

  5. lampp ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'lepus'

    解决方法: 在[mysqlld]段下增加如下代码:skip-grant-tables: 1.which mysql 查看mysql位置,例如:/opt/lampp/bin/mysql 2.进入配置my ...

  6. [TJOI2015] 线性代数 - 最大权闭合子图

    展开 \(D=(AB-C)A^T\\ =\sum_{i=1}^n(\sum_{j=1}^na_jb_{j,i}-c_i)a_i\\ =\sum_{i=1}^n\sum_{j=1}^na_ia_jb_{ ...

  7. PHP生成登录图片验证码

    很久之前写的 感觉登录还挺常用 记录一下. public function makeLoginCodeAction() //生成登录页的验证码 { Header("Content-type: ...

  8. 题解 AT859 【元素の系統名】

    题目传送门. 介绍一种使用string字符串的方法. \(string\)是\(C++\).\(java\).\(VB\)等编程语言中的字符串,字符串是一个特殊的对象,属于引用类型. \(C++\)标 ...

  9. eclipse中tomcat正常启动,但浏览器访问不了tomcat首页之tomcat配置问题

    症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的tomc ...

  10. 【做题笔记】洛谷P1506 拯救oibh总部

    跑一遍染色法,最后判断哪些位置没被染色即可 一些技巧: 为了判断方便,把字符转换成 int 型的数字. 注意边界问题 详细解释见代码 #include <iostream> #includ ...