如何使用python中的pandas发送图表?

2024-04-23 12:22:40 发布

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

我想发一封带图表的报告电子邮件,不要用绘图。如何通过电子邮件发送用熊猫创建的图表?以下是我目前为止的代码:

df = pd.DataFrame({'lab':eventID, 'val':counter})
ax = df.plot.bar(x='lab', y='val', rot=0)

email_body=ax

msg = MIMEMultipart('alternative')
msg['From'] = me
msg['To'] = recipient
msg['Subject'] = subject

msg.attach(MIMEText(email_body, 'html'))
server = smtplib.SMTP('smtp3.mycompany.com')
server.ehlo()
server.sendmail(me, recipient, msg.as_string())
server.close()

我需要添加什么才能使它工作?在


Tags: 代码绘图dfserver电子邮件email报告图表
1条回答
网友
1楼 · 发布于 2024-04-23 12:22:40

Pandas plot是基于matplotlib的,所以它只需要调用savefig('foo.png'),然后将foo.png附加到您的电子邮件中。在

步骤1(来自here):

fig = ax[0].get_figure()
fig.savefig("~/Desktop/foo.png")

第2步-直接从manual引用Pythonemail

^{pr2}$

应该能帮你完成任务。在

相关问题 更多 >