【问题标题】:How to save ggplot generated graph as image in streamlit python如何在streamlit python中将ggplot生成的图形保存为图像
【发布时间】:2021-10-08 17:41:05
【问题描述】:

我已经绘制了一个 ggplot 图。现在我想在我的 streamlit 应用程序中提供下载图作为图像选项。任何想法如何做到这一点。

这是我的 ggplot 代码。

 A = A + geom_line(data = pred1,mapping = aes(x = 'time',y = 'U_CI'),color = "red",linetype = "dotted",size = 1.3)\
                            +geom_line(data = pred2,mapping = aes(x = 'time',y = 'U_CI'),color = "blue",linetype = "dotted",size = 1.3)\
                            +geom_line(data = pred3,mapping = aes(x = 'time',y = 'U_CI'),color = "green",linetype = "dotted",size = 1.3)\
                            +geom_line(data = pred1,mapping = aes(x = 'time',y = 'L_CI'),color = "red",linetype = "dotted",size = 1.3)\
                            +geom_line(data = pred2,mapping = aes(x = 'time',y = 'L_CI'),color = "blue",linetype = "dotted",size = 1.3)\
                            +geom_line(data = pred3,mapping = aes(x = 'time',y = 'L_CI'),color = "green",linetype = "dotted",size = 1.3)

【问题讨论】:

  • 你能澄清一下你在说什么编程语言吗?意思是,如果你试图从 Python 调用 R 代码(即“真正的”ggplot2),这将不是一个简单的例子。如果您的示例使用的 Python 包恰好与 R 包具有相同的语法,那么问题就容易多了。
  • 我在 python 中工作

标签: python streamlit


【解决方案1】:

较新的版本带有下载功能:

pip install --upgrade streamlit

with open("flower.png", "rb") as file:
     btn = st.download_button(
             label="Download image",
             data=file,
             file_name="flower.png",
             mime="image/png"
           )

但我建议使用 streamlit 也支持的 plotly,它在每个图表上也有一个“下载为 ...”按钮。 --> https://plotly.com/python/static-image-export/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2018-11-04
    • 1970-01-01
    相关资源
    最近更新 更多