Lesson 40 of 60 · python
Interactive Visualizations with Plotly
Duration: 20 minutes
Interactive Plots with Plotly
Plotly enables hover tooltips, zoom, and export to HTML.
Installing Plotly
pip install plotly
Simple interactive line chart
import plotly.express as px
import pandas as pd
time = pd.date_range('2024-01-01', periods=12, freq='M')
values = np.random.randn(12).cumsum()
fig = px.line(x=time, y=values, title='Interactive Monthly Trend')
fig.show()
Scatter plot with hover data
fig = px.scatter(df, x='Age', y='Salary', color='Department', hover_data=['Name', 'Experience'])
fig.update_traces(marker=dict(size=12, opacity=0.7))
fig.show()
Bar chart with animation (e.g., yearly sales)
sales = pd.read_csv('sales.csv')
fig = px.bar(sales, x='Month', y='Revenue', color='Region', animation_frame='Year')
fig.show()
Exporting to a standalone HTML file
fig.write_html('interactive_plot.html')
When to choose Plotly
- When you need interactive dashboards.
- For sharing visualizations with non‑technical stakeholders.