我们如何在"openpyxl"中绘制两个系列的数据? python包(折线图) [英] How we can draw two series of data in "openpyxl" package of python (line-chart)
本文介绍了我们如何在"openpyxl"中绘制两个系列的数据? python包(折线图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我们有以下代码:
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
for row in range(1,10):
value = ws.cell(row=row,column=1).value = row+5
for row in range(1,10):
value2 = ws.cell(row=row,column=2).value = row
wb.save("SampleChart.xlsx")
from openpyxl.charts import Reference, Series,LineChart
values = Reference(ws, (1, 1), (9, 1))
series = Series(values, title="First series of values")
chart = LineChart()
chart.append(series)
chart.drawing.name = 'This is my chart'
ws.add_chart(chart)
wb.save("SampleChart.xlsx")
如何将第二列的值绘制到相同的折线图中? (并添加legend
)?
How can i plot second column values to same line-chart? (and add legend
)?
推荐答案
我稍稍重新排列了代码,以便在创建系列之前声明图表及其属性.然后,只需在第二个值范围上重复系列创建即可.据我所知,除非我对这个问题有误解,否则Excel会自动创建图例.
I rearranged your code, slightly, so that the chart and its properties are declared before the series are created. Then, it's simply a matter of repeating your series creation on the second range of values. As far as I know, unless I'm misunderstanding the question, Excel creates the legend automatically.
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
for row in range(1,10):
value = ws.cell(row=row,column=1).value = row+5
for row in range(1,10):
value2 = ws.cell(row=row,column=2).value = row
wb.save("SampleChart.xlsx")
from openpyxl.charts import Reference, Series,LineChart
# setup the chart
chart = LineChart()
chart.drawing.name = 'This is my chart'
# setup and append the first series
values = Reference(ws, (1, 1), (9, 1))
series = Series(values, title="First series of values")
chart.append(series)
# setup and append the second series
values = Reference(ws, (1, 2), (9, 2))
series = Series(values, title="Second series of values")
chart.append(series)
ws.add_chart(chart)
wb.save("SampleChart.xlsx")
这篇关于我们如何在"openpyxl"中绘制两个系列的数据? python包(折线图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文