英文:
How to make a pie chart with row data in Excel with Openpyxl?
问题
以下是您要翻译的内容:
Hello, I am trying to make a pie chart with row data using openpyxl. I have found several examples with column data but I need row data. The example is this one:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
['Sold', 50, 30, 10, 40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_col=1, min_row=1, max_col=5, max_row=1)
data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "D1")
结果如图所示。图片
我已经尝试修改min_row/col和max_row/col,但无法找到解决方案。
英文:
`Hello, I am trying to make a pie chart wit row data using openpyxl. I have found several examples with column data but I need row data. The example is this one:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
['Sold', 50, 30, 10 ,40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_col=1, min_row=1, max_col=5, max_row=1)
data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "D1")
#The result that I obtain is shown in the picture.Picture
#I have tried to modify the min_row/col and max_row/col but I do not manage to get the solution.`
答案1
得分: 0
以下是您要翻译的代码部分:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Series, # <-- Add Series
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
['Sold', 50, 30, 10, 40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_col=2, min_row=1, max_col=5, max_row=1)
# data = Reference(ws, min_col=2, min_row=2, max_col=5, max_row=2)
### Expand range for label
data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
# Add chart Series
# pie.add_data(data, titles_from_data=True)
series = Series(data, title_from_data=True)
pie.append(series)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "D1")
wb.save('pie_chart.xlsx')
英文:
In simple form add the data as a series;
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Series, # <-- Add Series
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Apple', 'Cherry', 'Pumpkin', 'Chocolate'],
['Sold', 50, 30, 10, 40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_col=2, min_row=1, max_col=5, max_row=1)
# data = Reference(ws, min_col=2, min_row=2, max_col=5, max_row=2)
### Expand range for label
data = Reference(ws, min_col=1, min_row=2, max_col=5, max_row=2)
# Add chart Series
# pie.add_data(data, titles_from_data=True)
series = Series(data, title_from_data=True)
pie.append(series)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "D1")
wb.save('pie_chart.xlsx')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论