【问题标题】:Saving graphs to .html using mpld3使用 mpld3 将图形保存到 .html
【发布时间】:2021-03-01 18:19:07
【问题描述】:

我正在尝试使用 seaborn 和 mpld3 但在 .html 文件中显示图表。当我使用命令mpld3.show() 时,会弹出一个新窗口,并且该图显示在创建的本地网络服务器中(如文档所述)。但是,当尝试将其保存在 .html 文件中时,图表为空,但我可以看到一些图标,如移动和缩放可用....

mpld3.show()

sns.set(style="whitegrid")
sns.barplot(x=df.index.tolist(), y=df['Salário'])
mpld3.show()

图表应该是怎样的:

尝试使用 save_html() 保存到 .html 文件:

sns.set(style="whitegrid")
sns.barplot(x=df.index.tolist(), y=df['Salário'])
fig = plt.figure()
mpld3.save_html(fig, "./templates/graph_exp.html")

带有工作图标的错误图表

有人可以帮我让这个图表看起来正常保存到 .html 文件吗? :)

【问题讨论】:

  • 我建议编码为base 64

标签: python flask seaborn mpld3


【解决方案1】:

我不确定save_html() 是如何工作的,但这是我在我的烧瓶网站上使用的。

如果您不需要保存图像,此解决方案有效。

import base64
import io


pngImage = io.BytesIO()
FigureCanvas(fig).print_png(pngImage) #fig is my matPlotlib instance of Figure()

# Encode PNG image to base64 string
image = "data:image/png;base64,"
image += base64.b64encode(pngImage.getvalue()).decode('utf8')

然后在html中使用:

<img src="{{image}}">

【讨论】:

  • 你能把你的进口商品发给我吗?我在使用“.print_png”时遇到问题
  • ``` from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas``` from matplotlib.figure import Figure import base64 import io
  • 现在我的图像显示一个损坏的图标。有什么想法吗?哈哈
  • 我会添加这一行:print(base64.b64encode(pngImage.getvalue()).decode('utf8')) 然后从控制台粘贴base64(很长很正常)到这个网站:base64-to-image.com
  • 最后我使用 Plotly 来渲染图形:D。无论如何,谢谢你的帮助:)))
猜你喜欢
  • 2022-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 2014-09-24
相关资源
最近更新 更多