import plotly
print(plotly.__version__) # Should be 5.x or above
6.0.0
Benedict Thekkel
To get started with Plotly in Jupyter Labs, you need to install the following:
To render Plotly figures within JupyterLab, you need to install the following extensions:
Ensure you have Plotly version 5.x or above for the latest features.
import plotly.express as px
import numpy as np
import pandas as pd
# Sample Data
x = np.linspace(0, 10, 100)
y = np.sin(x)
df = pd.DataFrame({'x': x, 'y': y})
# Line Plot
fig = px.line(df, x='x', y='y', title='Sine Wave')
fig.show()
### **4.2. Scatter Plot**
```python
fig = px.scatter(df, x='x', y='y', title='Scatter Plot of Sine Wave')
fig.show()
import plotly.express as px
import numpy as np
import pandas as pd
import plotly.offline as pyo
from IPython.display import HTML, display
# Sample Data
x = np.linspace(0, 10, 100)
y = np.sin(x)
df = pd.DataFrame({'x': x, 'y': y})
# Turn off the menu bar
config = {
'displayModeBar': False # Disable the mode bar (menu bar)
}
# Line Plot
fig = px.line(df, x='x', y='y', title='Sine Wave')
# fig.show(config=config)
fig_html = fig.to_html(full_html=False, config=config)
display(HTML(fig_html))
fig = px.scatter(df, x='x', y='y', title='Scatter Plot of Sine Wave')
# fig.show(config=config)
fig_html = fig.to_html(full_html=False, config=config)
display(HTML(fig_html))
data = {'Fruits': ['Apple', 'Banana', 'Cherry', 'Date'],
'Quantity': [10, 20, 15, 5]}
df = pd.DataFrame(data)
fig = px.bar(df, x='Fruits', y='Quantity', title='Fruit Quantity')
fig_html = fig.to_html(full_html=False, config=config)
display(HTML(fig_html))
fig = px.histogram(df, x='Quantity', title='Histogram of Quantity')
fig_html = fig.to_html(full_html=False, config=config)
display(HTML(fig_html))
fig = px.pie(df, names='Fruits', values='Quantity', title='Fruit Distribution')
fig_html = fig.to_html(full_html=False, config=config)
display(HTML(fig_html))
fig = px.choropleth(df, locations="iso_alpha",
color="lifeExp",
hover_name="country",
animation_frame="year",
title="World Life Expectancy Over Time")
fig.show()
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="sex",
facet_col="time", facet_row="smoker",
title="Facet Plot of Tips")
fig.show()
df = px.data.gapminder()
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year",
animation_group="country", size="pop", color="continent",
hover_name="country", log_x=True, size_max=55,
range_x=[100,10000], range_y=[25,90],
title="Gapminder Animation")
fig.show()
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=2)
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='Line Plot'), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=np.cos(x), mode='markers', name='Scatter Plot'), row=1, col=2)
fig.add_trace(go.Bar(x=['A', 'B', 'C'], y=[5, 10, 15], name='Bar Chart'), row=2, col=1)
fig.add_trace(go.Pie(labels=['Apple', 'Banana', 'Cherry'], values=[10, 20, 30], name='Pie Chart'), row=2, col=2)
fig.update_layout(title='Multiple Plots')
fig.show()
fig.update_layout(
annotations=[dict(x=2, y=0, xref="x", yref="y", text="Annotation",
showarrow=True, arrowhead=2)],
shapes=[dict(type="line", x0=2, y0=-1, x1=2, y1=1,
line=dict(color="RoyalBlue", width=2))]
)
fig.show()
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, mode='lines+markers', name='Sine Wave'))
fig.update_layout(title='Sine Wave', xaxis_title='X', yaxis_title='Y')
fig.show()
from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=2)
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='Line Plot'), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=np.cos(x), mode='markers', name='Scatter Plot'), row=1, col=2)
fig.add_trace(go.Bar(x=['A', 'B', 'C'], y=[5, 10, 15], name='Bar Chart'), row=2, col=1)
# fig.add_trace(go.Pie(labels=['Apple', 'Banana', 'Cherry'], values=[10, 20, 30], name='Pie Chart'), row=2, col=2)
fig.update_layout(title='Multiple Plots')
fig.show()
fig.update_layout(
annotations=[dict(x=2, y=0, xref="x", yref="y", text="Annotation",
showarrow=True, arrowhead=2)],
shapes=[dict(type="line", x0=2, y0=-1, x1=2, y1=1,
line=dict(color="RoyalBlue", width=2))]
)
fig.show()
Dependencies:
import ipywidgets as widgets
def update_plot(change):
new_data = np.sin(x + change['new'])
fig = px.line(x=x, y=new_data, title='Interactive Sine Wave')
fig.show()
slider = widgets.FloatSlider(value=0, min=0, max=10, step=0.1, description='Phase')
slider.observe(update_plot, names='value')
display(slider)
scattergl
for large datasets (WebGL acceleration).fig.show(renderer='notebook')
for inline rendering in some cases.%matplotlib inline
to avoid conflicts with Matplotlib.Install kaleido
for image exporting:
scattergl
for WebGL acceleration.