Python 如何以 png 格式保存 Plotly Offline 图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40243446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:20:21  来源:igfitidea点击:

How to save Plotly Offline graph in format png?

pythonplotly

提问by Dave D.

I am using Plotly offline to generate graph in python.

我正在离线使用 Plotly 在 python 中生成图形。

As per the documentation below,

根据以下文档,

https://plot.ly/python/offline/

https://plot.ly/python/offline/

Here is my code, which perfectly generates C:/tmp/test_plot.html file.

这是我的代码,它完美地生成了 C:/tmp/test_plot.html 文件。

import plotly.offline as offline

offline.init_notebook_mode()

offline.plot({'data': [{'y': [4, 2, 3, 4]}], 
               'layout': {'title': 'Test Plot', 
                          'font': dict(family='Comic Sans MS', size=16)}},
             auto_open=False, filename='C:/tmp/test_plot')

How can I save this graph as png instead of html?

如何将此图形保存为 png 而不是 html?

回答by Anil_M

offline.plotmethod has image='pngand image_filename='image_file_name'attributes to save the file as a png.

offline.plot方法image='pngimage_filename='image_file_name'属性,将文件保存为png

offline.plot({'data': [{'y': [4, 2, 3, 4]}], 
              'layout': {'title': 'Test Plot', 
                         'font': dict(family='Comic Sans MS', size=16)}},
             auto_open=True, image = 'png', image_filename='plot_image',
             output_type='file', image_width=800, image_height=600, 
             filename='temp-plot.html', validate=False)

See more details inside offline.pyor online at plotly.

在内部offline.py或在线查看更多详细信息plotly

However, one caveat is that , since the output image is tied to HTML, it will open in browser and ask for permissions to save the image file. You can turn that off in your browser settings.

但是,一个警告是,由于输出图像与 HTML 相关联,它将在浏览器中打开并请求保存图像文件的权限。您可以在浏览器设置中关闭它。

enter image description here

在此处输入图片说明

Alternately, You may want to look at plotly to matplotlib conversion using plot_mpl.
Following example is from offline.py

或者,您可能希望使用plot_mpl.
下面的例子来自offline.py

from plotly.offline import init_notebook_mode, plot_mpl
    import matplotlib.pyplot as plt

    init_notebook_mode()

    fig = plt.figure()
    x = [10, 15, 20, 25, 30]
    y = [100, 250, 200, 150, 300]
    plt.plot(x, y, "o")

    plot_mpl(fig)
    # If you want to to download an image of the figure as well
    plot_mpl(fig, image='png')

回答by Jon Mease

Quick update: As of plotly.py 3.2.0, it's now possible to programmatically export figures as static images while fully offline.

快速更新:从 plotly.py 3.2.0 开始,现在可以在完全离线的情况下以编程方式将图形导出为静态图像。

This was accomplished by integrating the orcaproject into plotly.py. Check out the announcement postfor some more details.

这是通过将orca项目集成到 plotly.py 来实现的。查看公告帖子了解更多详情。

回答by Jon Mease

You can automate PhantomJS to save a screenshot with exactly the same width and height as the original image would be when it is downloaded by opening the browser.

您可以自动化 PhantomJS 以保存与原始图像完全相同的宽度和高度的屏幕截图,当它通过打开浏览器下载时。

Here is the code:

这是代码:

import plotly.offline as offline
from selenium import webdriver

offline.plot({'data': [{'y': [4, 2, 3, 4]}],
               'layout': {'title': 'Test Plot',
                          'font': dict(size=12)}},
             image='svg', auto_open=False, image_width=1000, image_height=500)

driver = webdriver.PhantomJS(executable_path="phantomjs.exe")
driver.set_window_size(1000, 500)
driver.get('temp-plot.html')
driver.save_screenshot('my_plot.png')

#Use this, if you want a to embed this .png in a HTML file
#bs_img = driver.get_screenshot_as_base64()