Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly chart not showing in Jupyter notebook

I have been trying to solve this issue for hours. I followed the steps on the Plotly website and the chart still doesn't show in the notebook.

This is my code for the plot:

colorway = ['#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844']  data = [     go.Scatter(         x = immigration.columns,         y = immigration.loc[state],                    name=state) for state in immigration.index]  layout = go.Layout(     title='Immigration',     yaxis=dict(title='Immigration %'),     xaxis=dict(title='Years'),     colorway=colorway,     font=dict(family='Courier New, monospace', size=18, color='#7f7f7f') )  fig = go.Figure(data=data, layout=layout) iplot(fig) 

And this is everything I have imported into my notebook:

import pandas as pd import numpy as np import matplotlib.pyplot as plt import plotly.plotly as py import plotly.graph_objs as go from plotly.offline import init_notebook_mode, iplot  init_notebook_mode(connected=True)   
like image 375
Liz Avatar asked Oct 12 '18 02:10

Liz


People also ask

How do you show plotly in Jupyter?

You can publish Jupyter Notebooks on Plotly. Simply visit plot.ly and select the + Create button in the upper right hand corner. Select Notebook and upload your Jupyter notebook (. ipynb) file!

Why plotly is not working?

If you are encountering problems using plotly with Dash please first ensure that you have upgraded dash to the latest version, which will automatically upgrade dash-core-components to the latest version, ensuring that Dash is using an up-to-date version of the Plotly. js rendering engine for plotly .

How do you show graphs in Jupyter Notebook?

IPython kernel of Jupyter notebook is able to display plots of code in input cells. It works seamlessly with matplotlib library. The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called.

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.


1 Answers

You need to change init_notebook_mode call, if you want to work in offline mode.

Such that:

# Import the necessaries libraries import plotly.offline as pyo import plotly.graph_objs as go # Set notebook mode to work in offline pyo.init_notebook_mode() # Create traces trace0 = go.Scatter(     x=[1, 2, 3, 4],     y=[10, 15, 13, 17] ) trace1 = go.Scatter(     x=[1, 2, 3, 4],     y=[16, 5, 11, 9] ) # Fill out data with our traces data = [trace0, trace1] # Plot it and save as basic-line.html pyo.iplot(data, filename = 'basic-line') 

Output should be shown in your jupyter notebook:

My example

like image 127
Dmitriy Kisil Avatar answered Sep 30 '22 12:09

Dmitriy Kisil