简体   繁体   English

我们如何在python的“openpyxl”包中绘制两个数据系列(折线图)

[英]How we can draw two series of data in “openpyxl” package of python (line-chart)

Suppose that we have this code: 假设我们有这个代码:

  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")

How can i plot second column values to same line-chart? 如何将第二列值绘制到相同的折线图? (and add legend )? (并添加legend )?

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. 据我所知,除非我误解了这个问题,否则Excel会自动创建图例。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我们如何在 python 中重复非数字列值之间的 plot 折线图,包含两列以上的信息? - How can we plot line-chart between repeating non-numeric column values in python, containing information of more than two columns? 如何使用python的openpyxl在Excel上绘制带有日期格式的折线图 - how to draw line chart with date fomat on excel using openpyxl of python 我们如何在 Python openpyxl 包中使用 iter_rows()? - How we can use iter_rows() in Python openpyxl package? Python matplotlib - 如何绘制多个系列的折线图? - Python matplotlib - How to draw line chart with many series? 如何从 Excel 图表图例中删除系列 - Python Openpyxl - How to Remove Series from Excel Chart Legend - Python Openpyxl Python OpenPyxl:根据按列排列的数据创建折线图 - Python OpenPyxl: Create a Line Chart from data arranged in columns 如何使用Python和Openpyxl将高级样式添加到Excel折线图中 - How can I add advanced styles to my Excel Line Chart Using Python & Openpyxl 如何使用pyspark绘制时间序列折线图 - How to draw time series line chart using pyspark 如何使用Python绘制时间序列图表的时间和价值 - How to draw time-series chart on time and value by using Python Django 和 Chart.js:具有多个数据集的折线图 - Django and Chart.js: Line-Chart with multiple Datasets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM