Creating Interactive Visualizations with Plotly

by | Python

Table of Contents

Introduction to Plotly and Installation

Overview

Plotly is a powerful graphing library used to create interactive and dynamic visualizations. It is widely used for data science and analytics in Python due to its capability to produce detailed and interactive plots effortlessly.

This section aims to guide you through the installation process and provide a brief overview of Plotly.

Installation

To install Plotly, you need to follow these steps:


  1. Ensure Python is Installed: Make sure you have Python 3.x installed on your machine. You can check your Python version by running:


    python --version


  2. Using pip to Install Plotly:
    Open your command line interface (CLI) or terminal and run the following command:


    pip install plotly

    This command installs the Plotly library along with its necessary dependencies.



  3. Verify Installation:
    To verify that Plotly is installed correctly, open a Python interactive shell or script and execute:


    import plotly
    print(plotly.__version__)

    This should print the Plotly version number installed, confirming the installation was successful.


Basic Example

Once Plotly is installed, you can create a simple plot to ensure everything is set up correctly.


  1. Creating a Simple Line Plot:


    In your Python script or interactive shell, execute the following code:


    import plotly.graph_objects as go

    # Create the figure
    fig = go.Figure()

    # Add a trace (line plot)
    fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[10, 11, 12, 13], mode='lines', name='line plot'))

    # Add titles
    fig.update_layout(title='Simple Line Plot', xaxis_title='X Axis', yaxis_title='Y Axis')

    # Show the plot
    fig.show()


  2. Running the Script:


    Save the script in a file, for example, simple_plot.py, and run it using:


    python simple_plot.py


  3. Visualization Output:


    This script will generate and display a simple line plot in your default web browser.


Summary

You’ve successfully set up Plotly in Python and created a basic interactive plot. This is the foundation for building more complex and dynamic visualizations using Plotly throughout this project.

Basic Plot Types with Plotly in Python

1. Line Plot

import plotly.graph_objects as go

x_data = [1, 2, 3, 4, 5]
y_data = [10, 11, 12, 13, 14]

fig = go.Figure(data=go.Scatter(x=x_data, y=y_data, mode='lines'))
fig.update_layout(title='Basic Line Plot', xaxis_title='X Axis', yaxis_title='Y Axis')
fig.show()

2. Scatter Plot

import plotly.graph_objects as go

x_data = [1, 2, 3, 4, 5]
y_data = [10, 11, 12, 13, 14]

fig = go.Figure(data=go.Scatter(x=x_data, y=y_data, mode='markers'))
fig.update_layout(title='Basic Scatter Plot', xaxis_title='X Axis', yaxis_title='Y Axis')
fig.show()

3. Bar Plot

import plotly.graph_objects as go

x_data = ['A', 'B', 'C', 'D']
y_data = [10, 20, 30, 40]

fig = go.Figure(data=go.Bar(x=x_data, y=y_data))
fig.update_layout(title='Basic Bar Plot', xaxis_title='Category', yaxis_title='Values')
fig.show()

4. Histogram

import plotly.graph_objects as go

data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

fig = go.Figure(data=go.Histogram(x=data))
fig.update_layout(title='Basic Histogram', xaxis_title='Value', yaxis_title='Frequency')
fig.show()

5. Box Plot

import plotly.graph_objects as go

data = [10, 20, 30, 40, 50, 60, 70]

fig = go.Figure(data=go.Box(y=data))
fig.update_layout(title='Basic Box Plot', yaxis_title='Values')
fig.show()

Customization of Plots: Layouts and Annotations in Plotly

1. Customizing Layouts

To customize the layout of a plot in Plotly, you will typically adjust aspects such as titles, axis labels, axis ranges, and general layout configurations. Below is a practical implementation to demonstrate this.

Example: Customizing Plot Layout

import plotly.graph_objects as go

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 14, 18, 22, 26]

# Creating the figure
fig = go.Figure()

# Adding a line plot
fig.add_trace(go.Scatter(x=x, y=y, mode='lines+markers', name='Sample Data'))

# Customizing layout
fig.update_layout(
    title='Customized Plot Layout',
    xaxis_title='X Axis Title',
    yaxis_title='Y Axis Title',
    xaxis=dict(
        range=[0, 6],
        tickmode='linear',
        tickangle=45,
        tickfont=dict(size=12, color='blue')
    ),
    yaxis=dict(
        range=[5, 30],
        tickmode='linear',
        ticks='outside',
        tickcolor='black',
        ticklen=8
    ),
    plot_bgcolor='rgba(0,0,0,0)',
    paper_bgcolor='rgba(255,255,255,0.9)',
)

# Display the figure
fig.show()

2. Adding Annotations

Annotations are used to add text labels to specific parts of a plot. They can greatly enhance the readability and informativeness of a plot.

Example: Adding Annotations to a Plot

import plotly.graph_objects as go

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 14, 18, 22, 26]

# Creating the figure
fig = go.Figure()

# Adding a line plot
fig.add_trace(go.Scatter(x=x, y=y, mode='lines+markers', name='Sample Data'))

# Customizing layout
fig.update_layout(
    title='Plot with Annotations',
    xaxis_title='X Axis Title',
    yaxis_title='Y Axis Title'
)

# Adding annotations
annotations = [
    go.layout.Annotation(
        x=2,
        y=14,
        xref='x',
        yref='y',
        text='Annotation 1',
        showarrow=True,
        arrowhead=2,
        ax=20,
        ay=-30
    ),
    go.layout.Annotation(
        x=4,
        y=22,
        xref='x',
        yref='y',
        text='Annotation 2',
        showarrow=True,
        arrowhead=2,
        ax=-30,
        ay=20
    )
]

fig.update_layout(annotations=annotations)

# Display the figure
fig.show()

Combining Layout Customizations and Annotations

For comprehensive customization, we can integrate both layout customizations and annotations in a single plot.

Example: Comprehensive Customization

import plotly.graph_objects as go

# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 14, 18, 22, 26]

# Creating the figure
fig = go.Figure()

# Adding a line plot
fig.add_trace(go.Scatter(x=x, y=y, mode='lines+markers', name='Sample Data'))

# Customizing layout
fig.update_layout(
    title='Comprehensively Customized Plot',
    xaxis_title='X Axis Title',
    yaxis_title='Y Axis Title',
    xaxis=dict(
        range=[0, 6],
        tickmode='linear',
        tickangle=45,
        tickfont=dict(size=12, color='darkblue')
    ),
    yaxis=dict(
        range=[5, 30],
        tickmode='linear',
        ticks='outside',
        tickcolor='darkred',
        ticklen=8
    ),
    plot_bgcolor='rgba(0,0,0,0)',
    paper_bgcolor='rgba(255,255,255,0.9)',
)

# Adding annotations
annotations = [
    go.layout.Annotation(
        x=2,
        y=14,
        xref='x',
        yref='y',
        text='Annotation 1',
        showarrow=True,
        arrowhead=2,
        ax=20,
        ay=-30
    ),
    go.layout.Annotation(
        x=4,
        y=22,
        xref='x',
        yref='y',
        text='Annotation 2',
        showarrow=True,
        arrowhead=2,
        ax=-30,
        ay=20
    )
]

fig.update_layout(annotations=annotations)

# Display the figure
fig.show()

This provides a robust template for customizing the layout and annotations in Plotly, allowing you to enhance your plots’ visual appeal and informativeness effectively.

Advanced Graphs and Plots with Plotly

This section covers creating advanced interactive and dynamic visualizations using Plotly in Python, focusing on 3D plots, subplots, and animations.

3D Plots

3D Scatter Plot

import plotly.graph_objs as go
import plotly.express as px

# Sample data
df = px.data.iris()

# Create 3D Scatter Plot
fig = go.Figure(data=[go.Scatter3d(
    x=df['sepal_length'], 
    y=df['sepal_width'], 
    z=df['petal_length'],
    mode='markers',
    marker=dict(
        size=5,
        color=df['species_id'],  # color by the 'species_id' column
        colorscale='Viridis',    # choose a colorscale
        opacity=0.8
    )
)])

# Customize layout
fig.update_layout(
    scene = dict(
        xaxis_title='Sepal Length',
        yaxis_title='Sepal Width',
        zaxis_title='Petal Length'
    )
)

# Show plot
fig.show()

3D Surface Plot

import numpy as np
import plotly.graph_objects as go

# Generate data
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))

# Create surface plot
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])

# Customize layout
fig.update_layout(
    title='3D Surface Plot',
    scene=dict(
        xaxis_title='X AXIS',
        yaxis_title='Y AXIS',
        zaxis_title='Z AXIS'
    )
)

# Show plot
fig.show()

Subplots

Creating Subplots

from plotly.subplots import make_subplots
import plotly.graph_objs as go

# Initialize subplots
fig = make_subplots(rows=2, cols=2, 
                    subplot_titles=('Plot 1', 'Plot 2', 'Plot 3', 'Plot 4'))

# Add traces to subplots
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Bar(x=[1, 2, 3], y=[6, 5, 4]), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[7, 8, 9]), row=2, col=1)
fig.add_trace(go.Bar(x=[1, 2, 3], y=[9, 8, 7]), row=2, col=2)

# Customize layout
fig.update_layout(title='Customized Subplots Example')

# Show plot
fig.show()

Animations

Creating Animated Scatter Plot

import plotly.express as px

# Sample data
df = px.data.gapminder()

# Create animated scatter plot
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, 100000], range_y=[25, 90])

# Customize layout
fig.update_layout(title='GDP vs Life Expectancy (Animated)')

# Show plot
fig.show()

These examples demonstrate creating various advanced plots using Plotly in Python. Each section provides a functional implementation which you can use directly in your project for generating interactive and dynamic visualizations.

Unit 5: Interactivity with Plotly Express

In this section, we’ll focus on making graphs interactive, including hover effects, dropdowns, sliders, and buttons to provide a dynamic experience for end users.

1. Hover Data

To add hover data for enhanced information display:

import plotly.express as px

# Sample DataFrame
df = px.data.iris()

# Scatter plot with hover data
fig = px.scatter(df, x="sepal_width", y="sepal_length", hover_data=['species', 'petal_width'])
fig.show()

2. Dropdown Menus

To add dropdown menus for dynamically updating the graph:

import plotly.express as px
import plotly.graph_objects as go

# Sample DataFrame
df = px.data.gapminder().query("year == 2007")

# Base figure
fig = px.scatter(df, x="gdpPercap", y="lifeExp")

# Update drop down
fig.update_layout(
    updatemenus=[
        dict(
            buttons=list([
                dict(label="GDP per Capita",
                     method="update",
                     args=[{"x": [df["gdpPercap"]], "y": [df["lifeExp"]]}]),
                dict(label="Population",
                     method="update",
                     args=[{"x": [df["pop"]], "y": [df["lifeExp"]]}])
            ]),
            direction="down"
        )
    ]
)

fig.show()

3. Slider for Dynamic Updates

To add a slider to control the display dynamically:

import plotly.express as px

# Sample DataFrame
df = px.data.gapminder()

# Initial plot
fig = px.scatter(df[df['year'] == 2007], 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,100000], range_y=[25,90])

fig.show()

4. Adding Buttons for Mode Change

Adding buttons to switch between different plot types (scatter, line):

import plotly.express as px

# Sample DataFrame
df = px.data.iris()

# Initial figure
fig = px.scatter(df, x="sepal_width", y="sepal_length")

# Add buttons
fig.update_layout(
    updatemenus=[
        dict(
            type="buttons",
            direction="left",
            buttons=list([
                dict(label="Scatter",
                     method="update",
                     args=[{"type": "scatter"}]),
                dict(label="Line",
                     method="update",
                     args=[{"type": "line"}])
            ]),
        )
    ]
)

fig.show()

5. Combined Interactive Elements

Combine multiple interactive elements (dropdown, sliders, and buttons):

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go

# Sample DataFrame
df = px.data.gapminder()

# Create figure with subplots
fig = make_subplots()

# Define traces
scatter = go.Scatter(x=df['gdpPercap'], y=df['lifeExp'], mode='markers')
line = go.Scatter(x=df['year'], y=df.groupby('year')['lifeExp'].mean(), mode='lines')

# Add traces to figure
fig.add_trace(scatter)
fig.add_trace(line)

# Update layout for interactivity
fig.update_layout(
    updatemenus=[
        dict(
            buttons=list([
                dict(label="Scatter",
                     method="update",
                     args=[{"visible": [True, False]}]),
                dict(label="Line",
                     method="update",
                     args=[{"visible": [False, True]}])
            ]),
        ),
        dict(
            buttons=list([
                dict(label="2007",
                     method="relayout",
                     args=[{"xaxis.range": [2007, 2007]}]),
                dict(label="All Years",
                     method="relayout",
                     args=[{"xaxis.range": [df['year'].min(), df['year'].max()]}])
            ]),
            direction="down"
        )
    ]
)

fig.show()

This guide explores the core elements required to build interactive, dynamic visualizations using Plotly Express in Python. Each example can be directly run and adapted to specific datasets and requirements.

Interactivity with Plotly

Interactivity is a crucial feature of Plotly which allows users to explore data in a more granular and user-friendly manner. This section will cover how to add interactive elements such as hover information, clickable interactions, and interactive widgets to your Plotly plots in Python.

Adding Hover Information

import plotly.graph_objs as go

# Sample data
data = [
    go.Scatter(
        x=[1, 2, 3, 4, 5],
        y=[10, 14, 12, 16, 18],
        mode='markers',
        marker=dict(size=14, color='rgb(51,204,153)'),
        text=['A', 'B', 'C', 'D', 'E'],  # Hover text
        hoverinfo='text+x+y'  # Customize hover info
    )
]

layout = go.Layout(
    title='Scatter plot with custom hover info',
)

fig = go.Figure(data=data, layout=layout)
fig.show()

Clickable Interactions

Using dash, a web-based framework for Plotly, you can create more complex interactions like updating a plot based on dropdown selection.

Install Dash first:

pip install dash

Create a simple Dash app:

import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import plotly.graph_objs as go

# Initialize the Dash app
app = dash.Dash(__name__)

# Layout of the app
app.layout = html.Div([
    dcc.Dropdown(
        id='dropdown',
        options=[
            {'label': 'Dataset 1', 'value': 'ds1'},
            {'label': 'Dataset 2', 'value': 'ds2'}
        ],
        value='ds1'
    ),
    dcc.Graph(id='graph')
])

# Define the callback to update graph
@app.callback(
    Output('graph', 'figure'),
    [Input('dropdown', 'value')]
)
def update_graph(selected_value):
    data_dict = {
        'ds1': {'x': [1, 2, 3], 'y': [4, 1, 2]},
        'ds2': {'x': [1, 2, 3], 'y': [2, 4, 5]}
    }

    data = [
        go.Bar(
            x=data_dict[selected_value]['x'],
            y=data_dict[selected_value]['y']
        )
    ]
    layout = go.Layout(title=f'Dataset: {selected_value}')

    return go.Figure(data=data, layout=layout)

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)

Interactive Widgets using Plotly

You can use Jupyter Widgets to create interactive plots directly in Jupyter Notebooks. To achieve this, you will use Plotly’s support for Jupyter Widgets:

First, install ipywidgets if not installed:

pip install ipywidgets

Then use the following code in your Jupyter Notebook:

import plotly.graph_objs as go
from ipywidgets import interactive
from plotly.subplots import make_subplots
import numpy as np

# Create sample data
x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

# Function to update graph
def update_plot(freq=1.0):
    y = np.sin(freq * x)
    trace = go.Scatter(x=x, y=y, mode='lines', name=f'Sine wave with frequency {freq} Hz')
    
    fig = make_subplots(rows=1, cols=1)
    fig.add_trace(trace)
    fig.update_layout(title='Interactive Sine Wave')
    fig.show()

# Create an interactive widget
interactive_plot = interactive(update_plot, freq=(0.1, 5.0, 0.1))
output = interactive_plot.children[-1]
output.layout.height = '350px'
interactive_plot

Summary

This section has provided practical implementations for adding interactivity in Plotly, including hover information, clickable interactions using Dash, and interactive widgets in Jupyter notebooks. You can directly use these snippets in your projects to enhance user interactivity in your Plotly plots.

Integrating Plotly with Dash for Web Applications

In this section, we’ll focus on integrating Plotly with Dash to create interactive web applications. Below is the complete implementation for a simple web application leveraging Dash and Plotly.

Implementation

1. Install dependencies

pip install dash
pip install plotly

2. Create a Dash Application

# imports
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go

# Initialize the Dash app
app = dash.Dash(__name__)

# Create sample data for the graph
data = {
    'x': [1, 2, 3, 4, 5],
    'y': [10, 11, 12, 13, 14]
}

# Layout of the Dash app
app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''Dash: A web application framework for Python.'''),

    dcc.Graph(
        id='example-graph',
        figure={
            'data': [
                go.Scatter(
                    x=data['x'],
                    y=data['y'],
                    mode='lines+markers',
                    marker=dict(size=10, color='red', symbol='circle')
                )
            ],
            'layout': go.Layout(
                title='Sample Line Plot',
                xaxis={'title': 'X Axis'},
                yaxis={'title': 'Y Axis'}
            )
        }
    )
])

# Run the app
if __name__ == '__main__':
    app.run_server(debug=True)

3. Running the Application

Make sure to run your application script by executing the following command in your terminal or command prompt:

python your_application_script.py

Navigate to http://127.0.0.1:8050 in your web browser to view the interactive Plotly plot integrated into the Dash web application.

Plotly in Jupyter Notebooks

Part #8: Plotly in Jupyter Notebooks

In this section, you will learn how to use Plotly to create interactive and dynamic visualizations directly within Jupyter Notebooks. You will understand how to integrate Plotly with Jupyter Notebooks to harness the full power of interactive plots.

Setting Up the Environment

Ensure that you have a Jupyter Notebook environment ready. You can use Jupyter Lab or Jupyter Notebook for this purpose.

Importing Necessary Libraries

import plotly.graph_objects as go
import pandas as pd
import numpy as np

Creating a Simple Plot

We will start by creating a simple line plot.

# Sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)

# Create the plot
fig = go.Figure()

# Add a line plot
fig.add_trace(go.Scatter(x=x, y=y, mode='lines', name='Sine Wave'))

# Show the plot
fig.show()

Creating a Complex Plot with Multiple Traces

In this section, we’ll create a more complex plot with multiple traces to demonstrate how you can build intricate visualizations in Jupyter Notebooks.

# Sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Create the plot
fig = go.Figure()

# Add a sine wave
fig.add_trace(go.Scatter(x=x, y=y1, mode='lines', name='Sine Wave'))

# Add a cosine wave
fig.add_trace(go.Scatter(x=x, y=y2, mode='lines', name='Cosine Wave'))

# Customize layout
fig.update_layout(
    title="Sine and Cosine Waves",
    xaxis_title="X Axis",
    yaxis_title="Y Axis",
    template="plotly_dark"
)

# Show the plot
fig.show()

Creating Interactive Widgets

Enhance interactivity by using IPyWidgets to create widgets like sliders and dropdowns that interact with your Plotly plots.

Install IPyWidgets If Needed

You can install IPyWidgets by running the following command in your Jupyter cell:

!pip install ipywidgets

Adding an Interactive Slider

from ipywidgets import interact

# Update function for the slider
def update_plot(freq):
    y = np.sin(freq * x)
    with fig.batch_update():
        fig.data[0].y = y
        fig.layout.title = f"Sine Wave with Frequency {freq}"

# Initial Plot
y = np.sin(x)
fig = go.FigureWidget([go.Scatter(x=x, y=y, mode='lines', name='Sine Wave')])
fig.show()

# Interactive slider
interact(update_plot, freq=(1, 10, 0.1))

Conclusion

This section demonstrated how to use Plotly to create both simple and complex interactive visualizations within Jupyter Notebooks. You also learned to add interactivity using IPyWidgets, enhancing the user experience by allowing dynamic updates to the plots. This powerful combination can be used effectively in data analysis, reports, and educational materials, providing a richer and more engaging way to present data.

Data Preprocessing for Plotly Visualizations

Data preprocessing is a critical step when working with visualizations in Plotly. Proper preprocessing ensures that the data is clean, formatted correctly, and made ready for visualization.

Step-by-Step Preprocessing Procedure

1. Handling Missing Values

Identify and handle missing values in your dataset. Decide whether to fill, interpolate, or drop missing values based on data context.

function handleMissingValues(data):
    for each column in data:
        if column type is numeric:
            fill missing values with median of the column
        else if column type is categorical:
            fill missing values with the most frequent value
    return data

2. Normalizing/Standardizing Data

Normalize or standardize data to ensure that features contribute equally to the analysis.

function normalizeData(data):
    for each column in data:
        if column type is numeric:
            mean = calculateMean(column)
            std = calculateStd(column)
            for each value in column:
                value = (value - mean) / std
    return data

3. Encoding Categorical Variables

Convert categorical variables into a format that can be provided to ML algorithms to do a better job in prediction.

function encodeCategoricalData(data):
    for each column in data:
        if column type is categorical:
            unique_values = getUniqueValues(column)
            for each unique_value in unique_values:
                create dummy column for unique_value
                for each value in original column:
                    if value == unique_value:
                        set dummy column value to 1
                    else:
                        set dummy column value to 0
            drop original column
    return data

4. Feature Engineering

Carry out feature engineering to create new features or modify existing ones to make them more meaningful for analysis.

function featureEngineering(data):
    for each column pair in data:
        create new feature by combining the pair (if it makes sense)
        ex: new_column = column1 * column2
    return data

Example Workflow

Initial Data

Assume data is a pandas DataFrame including the columns ‘age’, ‘income’, ‘gender’, and ‘purchase_frequency’.

Execution Steps

data_processed = handleMissingValues(data)
data_normalized = normalizeData(data_processed)
data_encoded = encodeCategoricalData(data_normalized)
data_featured = featureEngineering(data_encoded)

Apply to Plotly Visualization

With the processed data, you can now create Plotly visualizations. For example:

Plotly Visualization Code

import plotly.express as px

# Assuming data_featured is the final preprocessed pandas DataFrame
fig = px.scatter(data_featured, x='age', y='income', color='gender_male')
fig.show()

By following these preprocessing steps, the data is prepared and cleaned, ensuring your Plotly visualizations are based on consistent and reliable datasets.

Real-world Data Visualization Projects

In this section, we will cover the practical implementation of various real-world data visualization projects using Plotly in Python. The projects will include steps to integrate data sources, preprocessing data, creating interactive visualizations, and deploying them for user interaction. Let’s dive into three concrete projects.

Project 1: COVID-19 Dashboard

Data Source

  1. Fetch COVID-19 data from a reliable source such as Johns Hopkins University.

Preprocessing

  1. Load and preprocess the data using Pandas.
    import pandas as pd
    url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
    df = pd.read_csv(url)
    df = df.drop(columns=['Lat', 'Long'])
    df = df.melt(id_vars=['Province/State', 'Country/Region'], var_name='Date', value_name='Cases')
    df['Date'] = pd.to_datetime(df['Date'])

Visualization

  1. Create the visualization using Plotly.
    import plotly.express as px
    fig = px.line(df, x='Date', y='Cases', color='Country/Region', title='COVID-19 Cases Over Time')
    fig.show()

Deployment

  1. Deploy using Dash.
    from dash import Dash, dcc, html
    app = Dash(__name__)
    app.layout = html.Div([
    dcc.Graph(figure=fig)
    ])
    if __name__ == '__main__':
    app.run_server(debug=True)

Project 2: Financial Data Visualization

Data Source

  1. Fetch financial stock data from Yahoo Finance.

Preprocessing

  1. Load and preprocess the stock data using yfinance.
    import yfinance as yf
    ticker = 'AAPL'
    stock_data = yf.download(ticker, start='2020-01-01', end='2023-01-01')
    stock_data.reset_index(inplace=True)

Visualization

  1. Create an interactive candlestick chart.
    import plotly.graph_objects as go
    fig = go.Figure(data=[go.Candlestick(x=stock_data['Date'],
    open=stock_data['Open'],
    high=stock_data['High'],
    low=stock_data['Low'],
    close=stock_data['Close'])])
    fig.update_layout(title=f'{ticker} Stock Prices',
    yaxis_title='Price (USD)')
    fig.show()

Deployment

  1. Deploy using Dash.
    from dash import Dash, dcc, html
    app = Dash(__name__)
    app.layout = html.Div([
    dcc.Graph(figure=fig)
    ])
    if __name__ == '__main__':
    app.run_server(debug=True)

Project 3: Customer Segmentation Analysis

Data Source

  1. Simulate customer data.
    import numpy as np
    np.random.seed(0)
    customer_data = {
    'CustomerID': np.arange(1, 101),
    'Annual Income (k$)': np.random.randint(15, 137, 100),
    'Spending Score': np.random.randint(1, 101, 100)
    }
    customer_df = pd.DataFrame(customer_data)

Visualization

  1. Create a 3D scatter plot to visualize customer segments.
    fig = px.scatter_3d(customer_df, x='Annual Income (k$)', y='Spending Score', z='CustomerID',
    title='Customer Segmentation', color='Spending Score')
    fig.show()

Deployment

  1. Deploy using Dash.
    from dash import Dash, dcc, html
    app = Dash(__name__)
    app.layout = html.Div([
    dcc.Graph(figure=fig)
    ])
    if __name__ == '__main__':
    app.run_server(debug=True)

These projects provide comprehensive examples of how to fetch, preprocess, visualize, and deploy data using Plotly and Dash to create real-world, interactive data visualizations.

Related Posts