Creating Interactive Plotly Dashboards in Python
Plotly is a powerful library for creating interactive visualizations in Python. It allows you to create a wide range of charts, graphs, and plots with ease. In this tutorial, we’ll explore different methods for combining multiple Plotly plots and tables into a single interactive dashboard using Python.
You can follow along with the code examples in this article or access the complete Jupyter Notebook
Method 1: Combining Plots and Tables in a Single HTML File
In this method, we’ll create multiple Plotly plots and a table, and then combine their HTML representations into a single HTML file. Here’s how to do it:
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
# Load the Iris dataset
iris = px.data.iris()
# Create different plots
fig1 = px.scatter(iris, x="sepal_width", y="sepal_length", color="species", title="Sepal Width vs Sepal Length")
fig2 = px.scatter(iris, x="petal_width", y="petal_length", color="species", title="Petal Width vs Petal Length")
fig3 = px.histogram(iris, x="sepal_length", color="species", title="Sepal Length Distribution")
fig4 = px.histogram(iris, x="sepal_width", color="species", title="Sepal Width Distribution")
fig5 = px.box(iris, x="species", y="petal_length", title="Petal Length by Species")
fig6 = px.box(iris, x="species", y="petal_width", title="Petal Width by Species")
# Create a Plotly table
fig_table = go.Figure(data=[go.Table(
header=dict(values=list(iris.columns), fill_color='paleturquoise', align='left'),
cells=dict(values=[iris[col] for col in iris.columns], fill_color='lavender', align='left'))])
# Concatenate HTML representations of each figure
html_string = '''
<html>
<head>
<title>Iris Dataset Dashboard</title>
</head>
<body>
<h1>Iris Dataset Visualization</h1>
'''
# Add each figure to the HTML string
for fig in [fig1, fig2, fig3, fig4, fig5, fig6, fig_table]:
html_string += fig.to_html(full_html=False, include_plotlyjs='cdn')
html_string += '''
</body>
</html>
'''
# Save the HTML string to a file
html_file_path = "/iris_dashboard_complete.html"
with open(html_file_path, 'w') as file:
file.write(html_string)
Method 2: Creating a Dashboard with Subplots
Another approach is to use Plotly’s subplots feature to create a dashboard with multiple plots arranged in a grid. Here’s an example:
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
# Load the Iris dataset
iris = px.data.iris()
# Create different plots
fig1 = px.scatter(iris, x="sepal_width", y="sepal_length", color="species", title="Sepal Width vs Sepal Length")
fig2 = px.scatter(iris, x="petal_width", y="petal_length", color="species", title="Petal Width vs Petal Length")
fig3 = px.histogram(iris, x="sepal_length", color="species", title="Sepal Length Distribution")
fig4 = px.histogram(iris, x="sepal_width", color="species", title="Sepal Width Distribution")
fig5 = px.box(iris, x="species", y="petal_length", title="Petal Length by Species")
fig6 = px.box(iris, x="species", y="petal_width", title="Petal Width by Species")
# Combine into a dashboard
dashboard = make_subplots(
rows=3, cols=2,
subplot_titles=("Sepal Width vs Sepal Length", "Petal Width vs Petal Length",
"Sepal Length Distribution", "Sepal Width Distribution",
"Petal Length by Species", "Petal Width by Species")
)
# Add traces
dashboard.add_trace(fig1['data'][0], row=1, col=1)
dashboard.add_trace(fig2['data'][0], row=1, col=2)
dashboard.add_trace(fig3['data'][0], row=2, col=1)
dashboard.add_trace(fig4['data'][0], row=2, col=2)
dashboard.add_trace(fig5['data'][0], row=3, col=1)
dashboard.add_trace(fig6['data'][0], row=3, col=2)
# Update layout
dashboard.update_layout(height=1200, showlegend=False)
# Export to HTML
html_file = "/content/iris_dashboard.html"
dashboard.write_html(html_file)
Method 3: Combining Plots and Tables Side by Side
If you want to display plots and their corresponding data tables side by side, you can use Plotly’s subplots with different column widths. Here’s how:
import pandas as pd
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import plotly.express as px
# Load the Iris dataset
iris = px.data.iris()
# Create a list of figures
figures = [
px.scatter(iris, x="sepal_width", y="sepal_length", color="species", title="Sepal Width vs Sepal Length"),
px.scatter(iris, x="petal_width", y="petal_length", color="species", title="Petal Width vs Petal Length"),
# Add more plots as needed
]
# Create the subplot layout
fig = make_subplots(rows=len(figures), cols=2, column_widths=[0.7, 0.3],
specs=[[{"type": "xy"}, {"type": "table"}]] * len(figures))
# Add each plot and its corresponding table to the layout
for i, figure in enumerate(figures, start=1):
fig.add_trace(figure['data'][0], row=i, col=1)
# Create a table for the plot data
table = go.Table(
header=dict(values=list(iris.columns), fill_color='paleturquoise', align='left'),
cells=dict(values=[iris[col] for col in iris.columns], fill_color='lavender', align='left')
)
fig.add_trace(table, row=i, col=2)
# Update layout if needed
fig.update_layout(height=1000 * len(figures), showlegend=False)
# Save to an HTML file
html_file_path = "/content/iris_plots_with_tables.html"
fig.write_html(html_file_path)
Conclusion
Creating interactive dashboards with Plotly and Python is a powerful way to visualize and explore data. By combining multiple plots and tables into a single HTML file, you can create comprehensive dashboards that provide insights and allow for interactive exploration.
Plotly offers various options for customizing and arranging plots, making it a versatile tool for data visualization. Whether you prefer to combine plots and tables in a single HTML file, create a dashboard with subplots, or display plots and tables side by side, Plotly has you covered.
Experiment with different layouts, plot types, and customizations to create engaging and informative dashboards tailored to your specific needs.