Python 使用 plotly offline 将图形生成为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34957790/
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 15:46:05  来源:igfitidea点击:

Use plotly offline to generate graphs as images

pythonhtmlimagepython-2.7plotly

提问by Matt Cremeens

I am working with plotly offline and am able to generate an html file using

我正在离线使用 plotly 并且能够使用生成一个 html 文件

plotly.offline.plot({"data": data, "layout": layout})

It works great. The graph is generated correctly and the html file gets saved to my current directory.

它工作得很好。图形正确生成,html 文件保存到我的当前目录。

What I want, though is, using plotly offline, is to have an image (.png, .jpg, etc.) file saved instead. Am I on the right track? What do I need to do from here?

不过,我想要的是,离线使用 plotly,而是保存一个图像(.png、.jpg 等)文件。我在正确的轨道上吗?我需要从这里做什么?

回答by Benares

one possibility using ipython notebook is to display the graph and then choose the option "Download plot as png" manually.

使用 ipython notebook 的一种可能性是显示图形,然后手动选择“将绘图下载为 png”选项。

回答by Benares

I found the solution in the documentation here:

我在这里的文档中找到了解决方案:

https://plot.ly/python/static-image-export/

https://plot.ly/python/static-image-export/

So a minimal example would be:

所以一个最小的例子是:

import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

N = 1000
random_x = np.random.randn(N)
random_y = np.random.randn(N)

trace = go.Scatter(
    x = random_x,
    y = random_y,
    mode = 'markers'
)

data = [trace]

py.image.save_as({'data':data}, 'scatter_plot', format='png')

回答by RFQED

Heresays to use

这里说使用

import plotly.plotly as py
# Generate the figure

trace = Bar(x=[1,2,3],y=[4,5,6])
data = [trace]
layout = Layout(title='My Plot')
fig = Figure(data=data,layout=layout)

# Save the figure as a png image:
py.image.save_as(fig, 'my_plot.png')

回答by LordK

Try this

尝试这个

import plotly.offline
import plotly.graph_objs as go

plotly.offline.plot({"data": [go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
                     "layout": go.Layout(title="hello world")},
                     image='jpeg', image_filename='test')

and open it in Chrome

并在 Chrome 中打开它

回答by Shantanu Deshmukh

Simple way of using python plotly graphs offline:

离线使用python plotly图的简单方法:

1) Write import statements

1) 编写导入语句

import plotly.graph_objs as go
import plotly as plotly
import plotly.express as px

2) write your plotly graph code e.g.

2)编写您的绘图代码,例如

data = px.data.gapminder()

data_canada = data[data.country == 'Canada']
fig = px.bar(data_canada, x='year', y='pop',
             hover_data=['lifeExp', 'gdpPercap'], color='lifeExp',
             labels={'pop':'population of Canada'}, height=400)

3) name your figure (provide reader-friendly name :) )

3) 为您的人物命名(提供易于阅读的名称 :) )

plotly.offline.plot(fig, filename= output_filename + ".html")

4) Well-done! Please add comments, if you like my answer!

4)干得好!如果您喜欢我的回答,请添加评论!