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

查看:1059
本文介绍了情节:如何在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 object>
<IPython.core.display.HTML object>

我的代码出了点问题,或者Spyder的iPython Terminal仍然不支持此功能?

以下是示例代码(摘自 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)  

解决方案

导入图而不是iplot(并将最后一行从iplot(fig)更改为plot(fig)可以解决此问题,至少在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)  

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

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,
)
})

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,
)
})

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

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