xlsxwriter - Excel饼图的图例 - python

时间:2016-11-16 18:32:49

标签: python excel pandas xlsxwriter

我有一个包含6个标签(工作表)的Excel文件。每个工作表具有相同的结构并包含两列 - Col 1包含品牌名称,Col 2包含与每个品牌对应的值。对于excel文件中的每个工作表,我想制作一个饼图,显示每个品牌的%份额。

您可以用来运行脚本的示例xls文件是here

我编写的代码非常简单并生成图表。问题是图表的图例采用序列号名称而不是品牌名称。

import pandas as pd
import xlsxwriter as excel

       df = pd.read_excel("/Users/jack/Documents/python-pptx/filename", sheetname=None)


        workbook = excel.Workbook('/Users/jack/Documents/python-pptx/chart_pie.xlsx')

        for sheetname, data in df.iteritems():
            if len(data) > 0: 
                worksheet = workbook.add_worksheet(sheetname)
                chart = workbook.add_chart({'type': 'pie'})
                worksheet.write_column('A1', data['Brand'])
                worksheet.write_column('B1', data['Share_of_interactions'])
                chart.add_series({'categories': '='+sheetname+'!$A$1:$A$'+str(len(data)),
                                  'values':     '='+sheetname+'!$B$1:$B$'+str(len(data)),
                                  'name':       '='+sheetname+'!$A$1:$A$'+str(len(data))})

                ## insert chart into the worksheet
                worksheet.insert_chart('C3', chart)

                ## Close the workbook
        workbook.close()

以下是图表的屏幕截图:

enter image description here

如果您在图表中注意到图例中的1,2,3 .. 。 7。它实际应该是品牌名称。我已根据chart.add_series - http://xlsxwriter.readthedocs.io/chart.html的文档中提到的xlsxwriter添加了name参数。任何帮助将非常感激。

2 个答案:

答案 0 :(得分:5)

问题是您的工作表名称中有一个空格,例如Sheet 1。您需要将其用单引号括起来:

df = pd.read_excel("/Users/julien/Downloads/SO_Example_Df.xlsx", sheetname=None)


workbook = excel.Workbook('/Users/julien/Downloads/SO_chart_pie.xlsx')

for sheetname, data in df.items():
    if len(data) > 0: 
        worksheet = workbook.add_worksheet(sheetname)
        chart = workbook.add_chart({'type': 'pie'})
        worksheet.write_column('A1', data['Brand'])
        worksheet.write_column('B1', data['Share_of_interactions'])
        # Here, add single quotes around the sheetname
        chart.add_series({'categories': "='"+sheetname+"'!$A$1:$A$"+str(len(data)),
                          'values':     "='"+sheetname+"'!$B$1:$B$"+str(len(data)),
                          'name':       'My pie chart'})

        ## insert chart into the worksheet
        worksheet.insert_chart('C3', chart)

## Close the workbook
workbook.close()

enter image description here

答案 1 :(得分:1)

在Excel和XlsxWriter中,饼图中数据点的名称来自"类别"。这与其他" 2D"图表类型,其中名称来自系列名称。这是因为饼图是单个系列图表的特例。

无论如何,如果您将类别指向您想要的名称,它们将被显示。像这样:

import pandas as pd

# Some sample data to plot.
data = {'apples': 10, 'berries': 32, 'squash': 21, 'melons': 13, 'corn': 18}

# Create a Pandas dataframe from the data.
df = pd.DataFrame([data], index=['Farm'])

# Create a Pandas Excel writer using XlsxWriter as the engine.
excel_file = 'pie.xlsx'
sheet_name = 'Sheet1'

writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)

# Access the XlsxWriter workbook and worksheet objects from the dataframe.
workbook = writer.book
worksheet = writer.sheets[sheet_name]

# Create a chart object.
chart = workbook.add_chart({'type': 'pie'})

# Configure the chart from the dataframe data.
chart.add_series({
    'categories': ['Sheet1', 0, 1, 0, 5],
    'values':     ['Sheet1', 1, 1, 1, 5],
})

# Insert the chart into the worksheet.
worksheet.insert_chart('A4', chart)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

另外,请注意使用类别和值的列表而不是范围字符串。在处理可变数据时,此可选格式更容易处理任何工作表名称。

输出:

enter image description here