使用绘图离线将图形生成为图像

问题描述 投票:11回答:4

我正在使用plotly offline并且能够使用生成html文件

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

它很棒。图表生成正确,html文件保存到我当前的目录。

我想要的是,使用绘图离线,可以保存图像(.png,.jpg等)文件。我是在正确的轨道上吗?从这里我需要做什么?

python html image python-2.7 plotly
4个回答
7
投票

试试这个

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')

并在Chrome中打开它


3
投票

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

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

所以最小的例子是:

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')

0
投票

使用ipython notebook的一种可能性是显示图形,然后手动选择“Download plot as png”选项。


-2
投票

Here说要使用

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')
© www.soinside.com 2019 - 2024. All rights reserved.