pandas for drawing excel

python mainstream drawing tools: matplotlib, seaborn, pandas, openpyxl, xslwriter

openpyxl: First, that under this demo official website, look a bit ignorant, did not specify a multiple excel in reference to Figure barrier ws Rererence simply a little ignorant deepcopy use of force, anyway, I did not understand, and secondly to write multi-sheet It is also not expand into,

Read the next blog copy and paste a navy which is nothing new:

Below openpyxl 3d bar bar chart as an example: the demonstration effect histogram generation multi-sheet:

Official website: 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)

  Interpretation parameters: configurations is a list of many storage configranition: Each configration structure such as a comment:

= {Configuration "Data": None, "the Title": None, "SHEET_NAME": None, "index":} None 
    Data: [ 
    
    [value_name, value_col1, value_col2], 
    [value_name2, value_column2, value_column2] 
    ] 
Data write is the data comprising a header and a value, data [0] is the header, data [1:] all data, index index representative of the excel sheet is inserted is the first of several sheet, title is a histogram of the title:
Chart. height is the height of the chart, width is the width, add_chart inserting chart method "E5" specifies the insertion position excel,
rows.insert (0, configuration [ "axis_x"]) Here insert the name of a type of classification is rows.insert (0, [name, column1, column2])
To see a real effect of it on this type corresponds to Sunday saturation, and submit bug volume two

 View multiple sheet:

 

 

 Part II: Using pandas drawing binding xslwriter:

Official website: https: //xlsxwriter.readthedocs.io/example_pandas_chart_columns.html

I directly on the code data with its own built two 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")

  

 

 




Guess you like

Origin www.cnblogs.com/SunshineKimi/p/12131884.html