首页 > 解决方案 > 如何向包含两个系列的图表添加图例?

问题描述

以下脚本创建一个包含两列的工作表。从这两列中,我创建了一个图表。然后我尝试在轴的右侧放置一个图例,但由于某种原因看不到图例。你能告诉我为什么我看不到传说吗?

顺便说一句,我已经尝试过没有任何成功的方法(其中一个被标记为注释)

chart = LineChart()
chart.title = "Device Time drift (2 samples/hour)"
chart.legend.position = 'r'
# chart.legend.layout = Layout(
#     manualLayout=ManualLayout(
#         yMode='edge',
#         xMode='edge',
#         x=0, y=0.9,
#         h=0.1, w=0.5
#     )
# )
chart.style = 10
chart.y_axis.title = 'Time Delta [mSec]'
chart.x_axis.title = 'Sample No.'
chart.legend = None
chart.height = 20  # default is 7.5
chart.width = 40  # default is 15

font = Font(typeface='Verdana')
size = 1200  # 14 point size
cp = CharacterProperties(latin=font, sz=size, b=False)  # Not bold
pp = ParagraphProperties(defRPr=cp)

# X and Y axes titles
chart.x_axis.title.tx.rich.p[0].pPr = pp
chart.y_axis.title.tx.rich.p[0].pPr = pp

data = Reference(worksheet=ws_write_timedel, min_col=1, min_row=1,
                 max_row=len(ws_write_timedel['A']), max_col=2, )

chart.add_data(data, titles_from_data=True,)
s1 = chart.series[0]
s1.graphicalProperties.line.solidFill = "00AAAA"
s1.graphicalProperties.line.dashStyle = "sysDot"
s1.graphicalProperties.line.width = 100050  # width in EMUs

s2 = chart.series[1]
s2.smooth = True

ws_write_timedel.add_chart(chart, "D1")enter code here

标签: pythonopenpyxl

解决方案


以下脚本是固定版本,现在图例已添加到图表中

chart = LineChart()
chart.title = "Device Time drift (2 samples/hour)"
chart.legend.position = 'r'
chart.style = 10
chart.y_axis.title = 'Time Delta [mSec]'
chart.x_axis.title = 'Sample No.'
chart.height = 20  # default is 7.5
chart.width = 40  # default is 15

font = Font(typeface='Verdana')
size = 1200  # 14 point size
cp = CharacterProperties(latin=font, sz=size, b=False)  # Not bold
pp = ParagraphProperties(defRPr=cp)

# X and Y axes titles
chart.x_axis.title.tx.rich.p[0].pPr = pp
chart.y_axis.title.tx.rich.p[0].pPr = pp

data = Reference(worksheet=ws_write_timedel, min_col=1, min_row=1,
             max_row=len(ws_write_timedel['A']), max_col=2, )

chart.add_data(data, titles_from_data=True,)
s1 = chart.series[0]
s1.graphicalProperties.line.solidFill = "00AAAA"
s1.graphicalProperties.line.dashStyle = "sysDot"
s1.graphicalProperties.line.width = 100050  # width in EMUs

s2 = chart.series[1]
s2.smooth = True

ws_write_timedel.add_chart(chart, "D1")enter code here

推荐阅读