jonmmease / plotly_ipywidget_notebooks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to run?

ankushnag14 opened this issue · comments

I copied the code as below and to understand what this project is doing. It returned with 0 errors when I run it. But how do I view the plot.

import pandas as pd
import numpy as np
import os
from ipywidgets import Image, Layout, HBox, VBox
import plotly.graph_objs as go
from ipywidgets import HTML
import plotly.plotly as py

cars_df = pd.read_csv('data/cars/cars.csv',
usecols=['City mpg',
'Fuel Type',
'Horsepower',
'Model Year',
'Torque', 'Hybrid', 'ID'])
cars_df.sample(5)

image_data = {}
for img_filename in os.listdir('data/cars/images'):
model_year = img_filename.split('.')[0]
with open("data/cars/images/2009_Audi_A3.jpg", "rb") as f:
b = f.read()
image_data[model_year] = b

Image(value=image_data['2012_Chevrolet_Camaro_Coupe'])

fig = go.FigureWidget(
data=[
dict(
type='scattergl',
x=cars_df['Torque'],
y=cars_df['City mpg'],
mode='markers',
)
],
)

fig.layout.title = 'Torque and Fuel Efficience'
fig.layout.titlefont.size = 22
fig.layout.titlefont.family = 'Rockwell'
fig.layout.xaxis.title = 'Torque (foot-pounds)'
fig.layout.yaxis.title = 'City MPG'
scatter = fig.data[0]
N = len(cars_df)
scatter.x = scatter.x + np.random.rand(N) * 10
scatter.y = scatter.y + np.random.rand(N) * 1
scatter.opacity = 0.2
fig.layout.hovermode = 'closest' # Set to 'closest'

contour = fig.add_histogram2dcontour(
x=scatter.x, y=scatter.y)
contour.colorscale = 'Hot'
contour.reversescale = True

contour.hoverinfo = 'skip'

scatter.text = cars_df['ID']
scatter.hoverinfo = 'text'

details = HTML()

def hover_fn(trace, points, state):
ind = points.point_inds[0]
details.value = cars_df.iloc[ind].to_frame().to_html()

scatter.on_hover(hover_fn)

image_widget = Image(
value=image_data['2012_Chevrolet_Camaro_Coupe'],
layout=Layout(height='252px', width='400px')
)

def hover_fn(trace, points, state):
ind = points.point_inds[0]

# Update details HTML widget
details.value = cars_df.iloc[ind].to_frame().to_html()

# Update image widget
model_year = cars_df['Model Year'][ind].replace(' ', '_')
image_widget.value = image_data[model_year]

scatter.on_hover(hover_fn)

VBox([fig, HBox([image_widget, details])])

Hi @ankushnag14, thanks for letting me know.

Did you run the code in the classic Jupyter Notebook or in JupyterLab?

Did you follow the full set of installation instructions at https://github.com/plotly/plotly.py? There are a bunch of versions that have to line up, especially if you're working in JupyterLab.

Thanks!

I was using JupyterLab. I probably didn't have things configured correctly.