饼图不包括所有数据
Pie chart not including all data
我在官方文档中使用 this example 创建了饼图。这是我的代码
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和饼图
中的数据
饼图未选择最后一个数据点:“金钥匙”。我阅读了 Reference
对象的文档,但无法理解导致问题的原因。在第 2 行和第 3 行中,我将 max_rows 值调整了 1,但没有解决。
任何help/ideas?
titles_from_data=True
用于指定第一项数据(单元格C1)为标题。
但是看你的截图,没有标题。因此,您可以将其设置为 false 或根本不设置该参数。
给re-create你的截图:
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")
我在官方文档中使用 this example 创建了饼图。这是我的代码
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和饼图
中的数据饼图未选择最后一个数据点:“金钥匙”。我阅读了 Reference
对象的文档,但无法理解导致问题的原因。在第 2 行和第 3 行中,我将 max_rows 值调整了 1,但没有解决。
任何help/ideas?
titles_from_data=True
用于指定第一项数据(单元格C1)为标题。
但是看你的截图,没有标题。因此,您可以将其设置为 false 或根本不设置该参数。
给re-create你的截图:
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")