如何在通过Openpyxl创建的图表中显示数据标签

问题描述 投票:3回答:2

我正在使用带有以下代码的openpyxl创建图表。默认情况下,它不显示数据标签-因此,我必须右键单击该图表,然后手动选择“添加数据标签”。如何使用Openpyxl命令执行此操作?预先感谢!

data = Reference(ws, min_col=2, min_row=1, max_col=6, max_row=10)
titles = Reference(ws, min_col=1, min_row=2, max_row=10)
chart = BarChart3D()
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)
ws.add_chart(chart, "C10")
charts label openpyxl
2个回答
5
投票

这在折线图上(对组合图而言)对我有用:openpyxls版本:2.3.2:

from openpyxl.chart.label import DataLabelList
chart2 = LineChart()
.... code to build chart like add_data()
and: # Style the lines
s1 = chart2.series[0]
s1.marker.symbol = "diamond"
... your data labels added here:
chart2.dataLabels = DataLabelList() 
chart2.dataLabels.showVal = True

希望这对某人有帮助


0
投票

数据标签是按图表,系列甚至系列中的每个项目设置的。目前,您必须查看源代码并确定其工作方式,但是chart.dataLabelschart.series[0].label是开始的地方。

© www.soinside.com 2019 - 2024. All rights reserved.