max_col 使用 openpyxl 创建饼图时只选择两列
max_col only selecting two columns when creating pie-chart using openpyxl
我正在尝试创建一个饼图,其中数据在行中,标签在列中。我想绘制第 2 行和第 4 列中的数据。但是当我 运行 时,它只选择两列。下面是代码:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Sold','Added','New','extra'],
['Apple', 50,3,4,5],
['Cherry', 30,5,6,7],
['Pumpkin', 10,7,8,99],
['Chocolate', 40,10,29,23],
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_row=1, min_col=2, max_col=4)
data = Reference(ws, min_col=2, min_row=2, max_col=4)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "G20")
ws = wb.create_sheet(title="Projection")
wb.save("pie.xlsx")
这是我的结果:
但我预计:
我的 problem:I 尝试将 max_col 更改为不同的数字,但它只选择了 2
我没有经常使用 Openxlpy,但看看下面几行是否可以帮助解决与循环相关的查询..
import openpyxl
wb = openpyxl.load_workbook(r'yourfile.xlsx')
ws = wb['yoursheet']
for rowno, rowval in enumerate(ws.iter_rows( min_col=2, min_row=2,max_col=4)):
for cell in rowval:
label = ws.cell(row=cell.row, column=1).value
print(label)
data = ws.cell(row=cell.row, column=2).value
print(data)
在查看源代码后,我找到了解决方案。 pie.add_data
默认情况下将每一列视为数据系列。因此,我需要将 pie.add_data
中的 from_rows
值作为数据系列添加到 select 行,并且必须更新 data = Reference(ws, min_col=1, min_row=2, max_col=4)
中的 min_col
。以下是所需的工作代码:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Sold','Added','New','extra'],
['Apple', 50,3,4,5],
['Cherry', 30,5,6,7],
['Pumpkin', 10,7,8,99],
['Chocolate', 40,10,29,23],
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_row=1, min_col=2, max_col=4)
data = Reference(ws, min_col=1, min_row=2, max_col=4)
#data = Reference(ws, range_string='Sheet!$B:$D')
pie.add_data(data, from_rows=2,titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "H2")
ws = wb.create_sheet(title="Projection")
wb.save("pie.xlsx")
我正在尝试创建一个饼图,其中数据在行中,标签在列中。我想绘制第 2 行和第 4 列中的数据。但是当我 运行 时,它只选择两列。下面是代码:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Sold','Added','New','extra'],
['Apple', 50,3,4,5],
['Cherry', 30,5,6,7],
['Pumpkin', 10,7,8,99],
['Chocolate', 40,10,29,23],
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_row=1, min_col=2, max_col=4)
data = Reference(ws, min_col=2, min_row=2, max_col=4)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "G20")
ws = wb.create_sheet(title="Projection")
wb.save("pie.xlsx")
这是我的结果:
但我预计:
我的 problem:I 尝试将 max_col 更改为不同的数字,但它只选择了 2
我没有经常使用 Openxlpy,但看看下面几行是否可以帮助解决与循环相关的查询..
import openpyxl
wb = openpyxl.load_workbook(r'yourfile.xlsx')
ws = wb['yoursheet']
for rowno, rowval in enumerate(ws.iter_rows( min_col=2, min_row=2,max_col=4)):
for cell in rowval:
label = ws.cell(row=cell.row, column=1).value
print(label)
data = ws.cell(row=cell.row, column=2).value
print(data)
在查看源代码后,我找到了解决方案。 pie.add_data
默认情况下将每一列视为数据系列。因此,我需要将 pie.add_data
中的 from_rows
值作为数据系列添加到 select 行,并且必须更新 data = Reference(ws, min_col=1, min_row=2, max_col=4)
中的 min_col
。以下是所需的工作代码:
from openpyxl import Workbook
from openpyxl.chart import (
PieChart,
ProjectedPieChart,
Reference
)
from openpyxl.chart.series import DataPoint
data = [
['Pie', 'Sold','Added','New','extra'],
['Apple', 50,3,4,5],
['Cherry', 30,5,6,7],
['Pumpkin', 10,7,8,99],
['Chocolate', 40,10,29,23],
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws, min_row=1, min_col=2, max_col=4)
data = Reference(ws, min_col=1, min_row=2, max_col=4)
#data = Reference(ws, range_string='Sheet!$B:$D')
pie.add_data(data, from_rows=2,titles_from_data=True)
pie.set_categories(labels)
pie.title = "Pies sold by category"
ws.add_chart(pie, "H2")
ws = wb.create_sheet(title="Projection")
wb.save("pie.xlsx")