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")