【问题标题】:How to send a chart using pandas in python?如何在 python 中使用 pandas 发送图表?
【发布时间】:2019-02-10 10:18:56
【问题描述】:

我想发送带有图表的报告电子邮件,而不使用 plotly。如何通过电子邮件发送使用 Pandas 创建的图表?到目前为止,这是我的代码:

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()

我需要添加什么才能使其正常工作?

【问题讨论】:

  • Pandas plot 是基于 matplotlib 的,所以它肯定会涉及到调用 savefig('foo.png') 然后将 foo.png 附加到您的电子邮件中
  • @JoshFriedlander 你建议使用不同的库吗?

标签: python pandas email charts


【解决方案1】:

Pandas 绘图基于 matplotlib,因此只需调用 savefig('foo.png'),然后将 foo.png 附加到您的电子邮件中。

第 1 步(来自here):

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

第 2 步 - 直接从 manual 引用 Python email

for file in pngfiles:
    # Open the files in binary mode.  Let the MIMEImage class automatically
    # guess the specific image type.
    fp = open(file, 'rb')
    img = MIMEImage(fp.read())
    fp.close()
    msg.attach(img)

应该能让你到达那里。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2020-02-22
    • 2018-10-30
    • 2012-05-28
    相关资源
    最近更新 更多