2

I'm trying to run a plotly chart as suggest here but nothing appears on my notebook. How can I visualize it?

Code:

%config InlineBackend.figure_format = 'retina'
%pylab inline
import plotly.graph_objs as go
import plotly.offline as py

import pandas as pd
import numpy as np
from ipywidgets import interactive, HBox, VBox

f = go.FigureWidget()
py.init_notebook_mode()

df = pd.read_csv('https://raw.githubusercontent.com/jonmmease/plotly_ipywidget_notebooks/master/notebooks/data/cars/cars.csv')

f = go.FigureWidget([go.Scatter(y = df['City mpg'], x = df['City mpg'], mode = 'markers')])
scatter = f.data[0]
N = len(df)
scatter.x = scatter.x + np.random.rand(N)/10 *(df['City mpg'].max() - df['City mpg'].min())
scatter.y = scatter.y + np.random.rand(N)/10 *(df['City mpg'].max() - df['City mpg'].min())
scatter.marker.opacity = 0.5

def update_axes(xaxis, yaxis):
    scatter = f.data[0]
    scatter.x = df[xaxis]
    scatter.y = df[yaxis]
    with f.batch_update():
        f.layout.xaxis.title = xaxis
        f.layout.yaxis.title = yaxis
        scatter.x = scatter.x + np.random.rand(N)/10 *(df[xaxis].max() - df[xaxis].min())
        scatter.y = scatter.y + np.random.rand(N)/10 *(df[yaxis].max() - df[yaxis].min())

axis_dropdowns = interactive(update_axes, yaxis = df.select_dtypes('int64').columns, xaxis = df.select_dtypes('int64').columns)

# Create a table FigureWidget that updates on selection from points in the scatter plot of f
t = go.FigureWidget([go.Table(
    header=dict(values=['ID','Classification','Driveline','Hybrid'],
                fill = dict(color='#C2D4FF'),
                align = ['left'] * 5),
    cells=dict(values=[df[col] for col in ['ID','Classification','Driveline','Hybrid']],
               fill = dict(color='#F5F8FF'),
               align = ['left'] * 5))])

def selection_fn(trace,points,selector):
    t.data[0].cells.values = [df.loc[points.point_inds][col] for col in ['ID','Classification','Driveline','Hybrid']]

scatter.on_selection(selection_fn)

# Put everything together
VBox((HBox(axis_dropdowns.children),f,t))

Screenshot:

enter image description here

10
  • my problem is that nothing appears. Can I save it as html or visualize it on the notebook? Commented May 21, 2019 at 10:46
  • If I run on the notebook, the plot does not show up. Commented May 21, 2019 at 10:48
  • my code is there and my question is simple. How can I visualize the chart showed here plot.ly/python/figurewidget ? Commented May 21, 2019 at 10:53
  • @emax Your code snippet, exactly as it is, runs just fine on my end in a Jupyter Notebook and produces a scatter plot with two widgets. Both work perfectly. So how are your running your code? Directly in a Jupyter Notebook. Or through an IDE like Spyder or PyCharm? Jupyterlab perhaps? A description of your system with version information would be very helpful. Commented May 21, 2019 at 11:59
  • @vestland I am running it on my Jupyter Notebook but it does not produce the plot. Commented May 21, 2019 at 12:05

1 Answer 1

3

Use:

py.init_notebook_mode(connected=True)

as mentioned here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.