How to save Plotly Offline graph in format png?(如何以 png 格式保存 Plotly 离线图?)
问题描述
I am using Plotly offline to generate graph in python.
As per the documentation below,
https://plot.ly/python/offline/
Here is my code, which perfectly generates C:/tmp/test_plot.html file.
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?
offline.plot
method has image='png
and image_filename='image_file_name'
attributes to save the file as a 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.py
or online at 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.
Alternately,
You may want to look at plotly to matplotlib conversion using plot_mpl
.
Following example is from 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')
这篇关于如何以 png 格式保存 Plotly 离线图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何以 png 格式保存 Plotly 离线图?
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01