Creating Interactive Dashboards with Plotly
๐งฉ What is Plotly?
Plotly is a powerful Python library for creating interactive graphs and dashboards. It integrates well with Dash, a framework built on top of Plotly, for building full dashboards in Python (no JavaScript required).
✅ What You Need to Know First
Required Libraries:
plotly – for creating interactive visualizations
dash – for building web-based dashboards
pandas – for data manipulation (often used with dashboards)
Install them via pip:
pip install plotly dash pandas
๐ Simple Example: Create an Interactive Dashboard
Goal:
We'll build a dashboard with a dropdown to select a country and display a line chart of its data.
Sample Code:
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import pandas as pd
import plotly.express as px
# Sample dataset: Gapminder from Plotly
df = px.data.gapminder()
# Initialize the Dash app
app = dash.Dash(__name__)
# Layout of the dashboard
app.layout = html.Div([
html.H1("Country GDP Dashboard", style={'textAlign': 'center'}),
dcc.Dropdown(
id='country-dropdown',
options=[{'label': c, 'value': c} for c in df['country'].unique()],
value='India',
clearable=False
),
dcc.Graph(id='gdp-graph')
])
# Callback to update the graph
@app.callback(
Output('gdp-graph', 'figure'),
Input('country-dropdown', 'value')
)
def update_graph(selected_country):
filtered_df = df[df['country'] == selected_country]
fig = px.line(filtered_df, x='year', y='gdpPercap', title=f'GDP per Capita of {selected_country}')
return fig
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)
๐ผ️ Output
This dashboard will:
Show a dropdown list of countries.
When you select a country, it updates a line chart showing GDP per capita over time.
The plot is fully interactive — zoom, pan, hover, etc.
๐ง What You Can Add
Feature How
Multiple dropdowns Add more dcc.Dropdown components
Filter by continent Add radio buttons or checkboxes
Multiple graphs Just add more dcc.Graph()s and callbacks
Advanced layout Use dash_bootstrap_components for styling
Upload your own data Use dcc.Upload to load CSV or Excel files
๐ Resources to Learn More
Plotly Express Docs
Dash Docs
Awesome Dash Examples
Learn Data Science Course in Hyderabad
Read More
A Comparison of Python vs. R for Data Science
The Best Python Libraries for Machine Learning
Building Your First Data Science Project in Jupyter Notebook
An Introduction to R's ggplot2 for Beautiful Visualizations
Visit Our Quality Thought Training Institute in Hyderabad
Comments
Post a Comment