Python 饼图不包括所有数据
我在官方文档中创建了饼图。这是我的密码Python 饼图不包括所有数据,python,charts,openpyxl,Python,Charts,Openpyxl,我在官方文档中创建了饼图。这是我的密码 1 pie = PieChart() 2 labels = Reference(sheet, min_col=2, min_row=1, max_row=5) 3 data = Reference(sheet, min_col=3, min_row=1, max_row=5) 4 pie.add_data(data, titles_from_data=True) 5 pie.set_categories(labels) 6 pie.title
1 pie = PieChart()
2 labels = Reference(sheet, min_col=2, min_row=1, max_row=5)
3 data = Reference(sheet, min_col=3, min_row=1, max_row=5)
4 pie.add_data(data, titles_from_data=True)
5 pie.set_categories(labels)
6 pie.title = "What did you like best about SKY Schools?"
7 sheet.add_chart(pie, "E1")
这是Excel和饼图中的数据
饼图并没有选取最后一个数据点:“金钥匙”。我阅读了对象的文档,但无法理解是什么导致了问题。在第2行和第3行中,我将max_rows值调整了1,但没有解决
任何帮助/想法?
titles\u from\u data=True
用于指定第一项数据(单元格C1)为标题
然而,看看你的截图,没有标题。因此,您可以将其设置为false,也可以根本不设置参数
重新创建屏幕截图:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['What did you like best about SKY Schools?', 'Games', 48],
['What did you like best about SKY Schools?', 'All of it', 36],
['What did you like best about SKY Schools?', 'Breathing', 25],
['What did you like best about SKY Schools?', 'Yoda', 23],
['What did you like best about SKY Schools?', 'The Golden Keys', 16]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
ws.merge_cells('A1:A5')
pie = PieChart()
labels = Reference(ws, min_col=2, min_row=1, max_row=5)
data = Reference(ws, min_col=3, min_row=1, max_row=5)
pie.add_data(data)
pie.set_categories(labels)
pie.title = ws.cell(1, 1).value
ws.add_chart(pie, "E1")
wb.save("results.xlsx")
要包括第5行,您需要将max_row设置为6。@Greg谢谢。我这样做了,但没有解决问题。
titles\u from\u data=True
是罪魁祸首。谢谢你的帮助。