简体   繁体   中英

Plotly: How to display charts in Spyder?

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)  

If you'd like to develop your plotly figures in Spyder, perhaps because of Spyders superb variable explorer, you can easily display a non-interactive image by just running fig.show() . Note that this is for newer versions of plotly where you don't have to worry about iplot and plotly.offline . If you'd like to send your figure to the browser for an interactive version, just run:

import plotly.io as pio

pio.renderers.default='browser'

Now your figure will be displayed in your default browser

To switch back to Spyder, just run:

import plotly.io as pio

pio.renderers.default='svg'

You can check other options too using pio.renderers? :

Renderers configuration
-----------------------
Default renderer: 'svg'
Available rendere <...> wser', 'firefox', 'chrome', 'chromium', 'iframe',
'iframe_connected', 'sphinx_gallery']

You'll find even more details here under Setting the default renderer

Here's a detailed example

Code:

import plotly.graph_objects as go

import plotly.io as pio
#pio.renderers.default = 'svg'
pio.renderers.default = 'browser'

x = ['Product A', 'Product B', 'Product C']
y = [20, 14, 23]

fig = go.Figure(data=[go.Bar(
            x=x, y=y,
            text=y,
            textposition='auto',
        )])
fig.show()

Plot:

在此处输入图片说明

System info:

Python 3.7.6
Spyder 3.3.1
Plotly 3.2.0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM