Plotly:如何在 Spyder 中显示图表? [英] Plotly: How to display charts in Spyder?

查看:146
本文介绍了Plotly:如何在 Spyder 中显示图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自 2015 年 11 月以来,plotly 是开源的,可用于 Python.https://plot.ly/javascript/open-source-announcement/>

当尝试离线做一些绘图时,这些在 iPython Notebook(版本 4.0.4)中工作但是如果我尝试在 Spyder(版本 2.3.8)中运行它们,我只会得到以下输出:

<IPython.core.display.HTML 对象>

我的代码有问题还是 Spyder 的 iPython 终端仍然不支持?

这是示例代码(取自 https://www.reddit.com/r/IPython/comments/3tibc8/tip_on_how_to_run_plotly_examples_in_offline_mode/)

from plotly.offline import download_plotlyjs, init_notebook_mode, iplot从 plotly.graph_objs 导入 *init_notebook_mode()trace0 = 散点(x=[1, 2, 3, 4],y=[10, 11, 12, 13],模式='标记',标记=字典(大小=[40, 60, 80, 100],))数据 = [trace0]布局 = 布局(showlegend=假,高度=600,宽度=600,)fig = dict(数据=数据,布局=布局)iplot(图)

解决方案

importing plot 而不是 iplot(并将最后一行从 iplot(fig) 更改为 plot(fig) 解决了问题,至少在 python 3 中:

from plotly.offline import download_plotlyjs, init_notebook_mode, plot从 plotly.graph_objs 导入 *init_notebook_mode()trace0 = 散点(x=[1, 2, 3, 4],y=[10, 11, 12, 13],模式='标记',标记=字典(大小=[40, 60, 80, 100],))数据 = [trace0]布局 = 布局(showlegend=假,高度=600,宽度=600,)fig = dict(数据=数据,布局=布局)情节(图)

但是,您可以执行以下操作,这稍微容易一些:

plotly 导入导入 plotly.graph_objsplotly.offline.plot({数据": [plotly.graph_objs.Scatter( x=[1, 2, 3, 4],y=[10, 11, 12, 13], mode='markers',标记=字典(大小=[40, 60, 80, 100]))],布局":plotly.graph_objs.Layout(showlegend=False,高度=600,宽度=600,)})

Since November 2015, plotly is Open-Source and available for python. https://plot.ly/javascript/open-source-announcement/

When trying to do some plots offline, these work in iPython Notebook (version 4.0.4) But if I try to run them in Spyder (version 2.3.8), i just get the following output:

<IPython.core.display.HTML object>
<IPython.core.display.HTML object>

There's something wrong in my code or the iPython Terminal of Spyder still doesn't support this?

Here goes the example code (taken from https://www.reddit.com/r/IPython/comments/3tibc8/tip_on_how_to_run_plotly_examples_in_offline_mode/)

from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
from plotly.graph_objs import *
init_notebook_mode()

trace0 = Scatter(
    x=[1, 2, 3, 4],
    y=[10, 11, 12, 13],
    mode='markers',
    marker=dict(
        size=[40, 60, 80, 100],
    )
)
data = [trace0]
layout = Layout(
    showlegend=False,
    height=600,
    width=600,
)

fig = dict( data=data, layout=layout )

iplot(fig)  

解决方案

importing plot instead iplot (and changing the last line from iplot(fig) to plot(fig) resolved the problem, at least in python 3:

from plotly.offline import download_plotlyjs, init_notebook_mode,  plot
from plotly.graph_objs import *
init_notebook_mode()

trace0 = Scatter(
    x=[1, 2, 3, 4],
    y=[10, 11, 12, 13],
    mode='markers',
    marker=dict(
        size=[40, 60, 80, 100],
    )
)
data = [trace0]
layout = Layout(
    showlegend=False,
    height=600,
    width=600,
)

fig = dict( data=data, layout=layout )

plot(fig)  

But instead you could do the following, which is slightly easier:

import plotly
import plotly.graph_objs
plotly.offline.plot({
"data": [
    plotly.graph_objs.Scatter(    x=[1, 2, 3, 4],
    y=[10, 11, 12, 13], mode='markers',
    marker=dict(
        size=[40, 60, 80, 100]))],
"layout": plotly.graph_objs.Layout(showlegend=False,
    height=600,
    width=600,
)
})

这篇关于Plotly:如何在 Spyder 中显示图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆