如何使用Openpyxl在Excel中使用行数据制作饼图?

huangapple go评论64阅读模式
英文:

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,  # &lt;-- Add Series
    Reference
)
from openpyxl.chart.series import DataPoint

data = [
    [&#39;Pie&#39;, &#39;Apple&#39;, &#39;Cherry&#39;, &#39;Pumpkin&#39;, &#39;Chocolate&#39;],
    [&#39;Sold&#39;, 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 = &quot;Pies sold by category&quot;
ws.add_chart(pie, &quot;D1&quot;)

wb.save(&#39;pie_chart.xlsx&#39;)

huangapple
  • 本文由 发表于 2023年3月4日 02:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定