Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)  
like image 340
Gabriel Avatar asked Feb 10 '16 12:02

Gabriel


People also ask

How do you view plotly graphs on Spyder?

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 .

How do you display plotly plots?

In general, there are five different approaches you can take in order to display plotly figures: Using the renderers framework in the context of a script or notebook (the main topic of this page) Using Dash in a web app context. Using a FigureWidget rather than a Figure in an ipywidgets context.

Does plotly work in Spyder?

Once updated, you can run the fig. show() function and Plotly will display the figure as a static image in the Plots Tab in Spyder.

How can plotly visualizations be displayed?

To install Plotly package, open up Terminal and type pip install plotly . Plotly is a platform that runs on JSON, a format in which parameters are passed to the Plotly API in dictionary formats. The plot and information are saved to your Plotly account when viewing visualizations.


1 Answers

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.

And if you'd like display your figure in the browser as a fully 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 producing your figure in 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:

enter image description here

System info:

Python 3.7.6
Spyder 3.3.1
Plotly 3.2.0
like image 199
vestland Avatar answered Sep 17 '22 22:09

vestland