如何使用openpyxl在excel图表中仅显示一个数据标签?

时间:2018-10-21 18:20:52

标签: python charts label openpyxl

我下面的代码使用openpyxl创建了一个简单的excel工作表和图表(代码来自documentation-编辑以解释需求)

from datetime import date

from openpyxl import Workbook
from openpyxl.chart import (
    LineChart,
    Reference,
)
from openpyxl.chart.axis import DateAxis
from openpyxl.chart.label import DataLabelList
wb = Workbook()
ws = wb.active

rows = [
    ['Date', 'Batch 1', 'Batch 2', 'Batch 3'],
    [date(2015,9, 1), 41, 30, 25],
    [date(2015,9, 2), 41, 25, 30],
    [date(2015,9, 3), 41, 30, 45],
    [date(2015,9, 4), 41, 25, 40],
    [date(2015,9, 5), 41, 35, 30],
    [date(2015,9, 6), 41, 40, 35],
]

for row in rows:
    ws.append(row)

c1 = LineChart()
c1.title = "Line Chart"
c1.style = 13
c1.y_axis.title = 'Size'
c1.x_axis.title = 'Test Number'

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=7)
c1.add_data(data, titles_from_data=True)

s2 = c1.series[2]
s2.smooth = True # Make the line smooth

c1.dataLabels = DataLabelList()
###########################################################
#Display data label and series name
#I need this to be displayed only for the first data point
#I can do this in excel by displaying the label only for the
#data point required

c1.dataLabels.showVal = True
c1.dataLabels.showSerName = True

ws.add_chart(c1, "A10")

wb.save("line.xlsx")

我正在得到图表

enter image description here

我想要的图表-如何获得这样的图表?

enter image description here

仅显示一个数据点的标签(系列名称和值)...

1 个答案:

答案 0 :(得分:0)

我可以正常工作了,下面是带有解释的代码:

systask command "sas -noterminal -rsasuser -batch -log simple_run_2.log -print simple_run_2.lst -sysparm 'param1=&param1.,param2=&param2.' simple_run_2.sas" taskname = simple_run_
2;

结果

enter image description here

仍然无法设置标签文本属性!