<jupyter_start><jupyter_text>Interactive Visualizations with Plotlyhttps://plot.ly/python/getting-started/__Plotly currently cannot display plots inside notebooks opened in Jupyter Lab out-of-the-box. Therefore please switch to Jupyter Notebook for this tutorial.__Jupyter Notebook (if running locally): http://localhost:8888/tree<jupyter_code>!conda install plotly --yes
import plotly.offline as plt # plot.ly offline plotting
import plotly.graph_objs as go
plt.init_notebook_mode(connected=True) # required for plt.iplot
# connected = False requires no internet connection for plotting, 
# but significantly increases notebook size.<jupyter_output><empty_output><jupyter_text>MotivationFor most plotting purposes, [Matplotlib](plotting.ipynb) (supplemented with Seaborn) is a great choice.The use case for Plot.ly are interactive plots where the user can change parameters, scroll, click on points to get their values, etc. In addition, Plot.ly plots can be used in the Dash web framework.Plot.ly offers both web-based plotting (which requires a plot.ly account) and offline plotting (based on the plot.ly open source library). For most use-cases, offline plotting is preferred, therefore only this option is discussed here.  Basic Plottinghttps://plot.ly/python/user-guide/python-api-user-guide<jupyter_code>import numpy as np
trace1 = go.Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1], marker={'color': 'red'},
                                                            mode='markers+lines')
trace1
x = np.arange(1,4,0.1)
trace2 = go.Scatter(x=x, y=4*np.sin(x), marker={'color': 'blue'}, mode='lines')
trace2
layout = go.Layout(title="hello world", xaxis={'title': 'x'},
                                yaxis={'title': 'y'})
layout
fig = go.Figure(data=[trace1, trace2], layout=layout)
fig
plt.plot(fig)<jupyter_output><empty_output><jupyter_text>The *plot* function creates the plot as html file for opening in Browser.<jupyter_code>p = plt.iplot(fig)<jupyter_output><empty_output><jupyter_text>Classical method to create a plot inside a notebook.<jupyter_code>f = go.FigureWidget(data=[trace1, trace2], layout=layout)
f.show()<jupyter_output><empty_output><jupyter_text>New method (Plotly >= 3.0) to create a plot inside a notebook.  Interactive Plotting of Pandas DataFrames<jupyter_code>import pandas as pd
from ipywidgets import widgets
df = pd.read_csv('../example_files/test_data.csv', parse_dates=[1, 2])
df.head()
vendor = widgets.Dropdown(
    options=df.VendorID.unique().tolist(),
    value=1,
    description='Vendor ID:'
    )
passengers = widgets.IntSlider(
    value=1,
    min=df.passenger_count.min(),
    max=df.passenger_count.max(),
    step=1,
    description='Passengers:',
    continuous_update=False,
    )
vendor
passengers
plot_df = df[(df.VendorID == vendor.value)
             & (df.passenger_count == passengers.value)].head(100)
g = go.FigureWidget(data=[go.Scatter(x=plot_df.trip_distance, y=plot_df.total_amount, 
                                     name='Distance vs. Price', mode='markers')],
                layout=go.Layout(title={'text': 'NYC Taxi Data'},
                                xaxis={'title': 'trip distance'},
                                yaxis={'title': 'total amount'}))
def response(change):
    plot_df = df[(df.VendorID == vendor.value)
                 & (df.passenger_count == passengers.value)].head(100)
    with g.batch_update():
        g.data[0].x = plot_df.trip_distance
        g.data[0].y = plot_df.total_amount
vendor.observe(response, names='value')
passengers.observe(response, names='value')
widgets.VBox([vendor, passengers, g])<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup