如何将包含两个系列的图例添加到图表中?

2024-10-03 19:20:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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

顺便说一句,我已经尝试了一些方法来实现这一点,但没有成功(其中一个方法被标记为注释)

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

Tags: datasizewstitlelinechartwidthpp
1条回答
网友
1楼 · 发布于 2024-10-03 19:20:03

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

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

相关问题 更多 >