在python中创建饼图 [英] Creating pie chart in python

查看:83
本文介绍了在python中创建饼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了饼图,但现在我正在使用一系列单元格,如下所示:

I have created my pie chart but right now I am using a range of cells like this:

chart3.add_series({
    'name': 'Pie data',
    'categories': '=Pivots!$A$3:$A$10',
    'values':     '=Pivots!$F$3:$F$10'})

它为我提供了一个饼图,其中包含在 A3-A10 中找到的类别以及与单元格 F3-F10 对应的值.不幸的是,我遇到的问题并不总是在每个单元格中都有数据.所以我可能在一张工作表中有数据,其中的类别和值可能在 A3-A6 和值 = F3-F6 之间,有时它只有在 A3-A9 和值 = F3-F9 之间的数据.我的问题是,当我为不同的工作表显示他们缺少字段的图例时,我想知道它们是否是一种方法来获取 A 的长度来获取类别和 F3 的长度来获取值,所以当我的图例是显示它只显示和代表有数据的行.

which gives me a pie chart with the categories that are found in A3-A10 and values that correspond from cells F3-F10. The problem I am having is unfortunately I am not always going to have data in each of these cells. so I may have data in one worksheet that has categories and values that may range from A3-A6 and values = F3-F6 and sometimes it only has data that ranges from A3-A9 and values = F3-F9. My problem is when I show my legend for the different worksheets they have missing fields and I was wondering if their was a way to just get the length of A to get the categories and the length of F3 to get the values so when my legend is showing it shows and represents only the rows with data.

推荐答案

假设您可以选择动态隐藏没有数据的行,那么就有一个相当简单的解决方案.

Assuming that it's an option for you to dynamically hide the rows without data, then there's a fairly straightforward solution.

我重新编写了 xlsxwriter 文档中使用的一些示例数据,然后查找了任何 Nan 值.如果有,则隐藏它们,因此不在饼图中.

I reworked some sample data that are used in the xlsxwriter docs and then looked for any Nan values. If there are any then those are hidden and thus not in the pie chart.

import xlsxwriter
import pandas as pd

headings = ['Category', 'Values']
data = [
['Apple', 30],['Cherry', 20],['Pecan',15],['Blueberry', 10],['Pumpkin', 10],
['Mince', float('nan')],['Custard', 3],['Potato', 2],
]


pies = pd.DataFrame(data, columns = headings)

nan_rows = pies.loc[pies['Values'].isnull()].index.values
hide_rows = nan_rows + 2

workbook = xlsxwriter.Workbook('test.xlsx', {'strings_to_numbers':  True,
                                             'strings_to_formulas': True,
                                             'nan_inf_to_errors': True})

ws = workbook.add_worksheet('Pivots')

ws.write('A2', headings[0])
ws.write('F2', headings[1])
ws.write_column('A3', pies['Category'])
ws.write_column('F3', pies['Values'])


chart3 = workbook.add_chart({'type': 'pie'})

chart3.add_series({
'name': 'Pie data',
'categories': '=Pivots!$A$3:$A$10',
'values':     '=Pivots!$F$3:$F$10'})

chart3.set_title({'name': 'Popular Pie Types'})

chart3.set_style(10)
ws.insert_chart('G11', chart3, {'x_offset': 25, 'y_offset': 10})

#This is where we hide the rows in list called hide_rows
for row_position in hide_rows:
    ws.set_row(row_position, None, None, {'hidden': True})

workbook.close()

这篇关于在python中创建饼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆