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

时间:2019-11-03 17:10:05

标签: python openpyxl

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

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

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

1 个答案:

答案 0 :(得分:0)

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

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