By Ankit Srivastava – Lead Digital Marketing Analyst & IT Trainer, Slidescope Institute


🎯 Objective

In this chapter, we’ll learn how to create Line Plots and Scatter Plots using Plotly in Python. These are the foundation of data visualization β€” ideal for tracking trends, identifying correlations, and analyzing performance over time.


🧠 Before We Begin

We’ll use Plotly Express, the high-level interface of Plotly, because it allows us to create beautiful and interactive graphs with minimal code.

Make sure Plotly is installed:

pip install plotly

And import the library:

import plotly.express as px

πŸ“ˆ Line Plots – Tracking Trends Over Time

A line plot connects data points with a continuous line β€” it’s perfect for visualizing trends such as sales growth, website traffic, or ad impressions over time.


πŸ”Ή Example 1: Simple Line Plot

import plotly.express as px

data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Website Visitors': [1500, 1800, 2200, 2400, 2600, 3100]
}

fig = px.line(data, 
              x='Month', 
              y='Website Visitors', 
              title='Monthly Website Traffic Trend',
              markers=True)

fig.show()

βœ… Explanation:

  • x β†’ Months
  • y β†’ Visitors count
  • markers=True adds circle markers to each data point

πŸ” Hovering on each point shows exact values β€” helping you interpret growth or drops instantly.


πŸ”Ή Example 2: Multiple Line Comparison

When analyzing data from different sources (like Google Ads vs Meta Ads), multiple lines help you compare performance over time.

import pandas as pd
import plotly.express as px

data = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'] * 2,
    'Visitors': [1500, 1800, 2200, 2400, 2600, 3100, 
                 1000, 1200, 1800, 2100, 2300, 2500],
    'Source': ['Google Ads']*6 + ['Meta Ads']*6
})

fig = px.line(data, 
              x='Month', 
              y='Visitors', 
              color='Source', 
              title='Traffic Comparison: Google Ads vs Meta Ads',
              markers=True)

fig.show()

βœ… Insight:
You can quickly identify which campaign is performing better month by month β€” perfect for marketing reports and dashboard presentations.


πŸ’‘ Customizing Your Line Chart

Plotly allows customization of titles, axis labels, hover data, and themes easily:

fig.update_layout(
    title='Traffic Growth by Platform',
    xaxis_title='Month',
    yaxis_title='Visitors',
    template='plotly_dark'
)

This gives your chart a modern, dark theme, perfect for dashboard presentations.


πŸ”· Scatter Plots – Visualizing Relationships

A scatter plot displays data points on two axes (X and Y) β€” great for identifying relationships or correlations between variables, such as:

  • Advertising Spend vs Conversions
  • Study Hours vs Test Score
  • Product Price vs Sales Volume

πŸ”Ή Example 3: Simple Scatter Plot

import plotly.express as px

data = {
    'Ad Spend ($)': [1000, 2000, 3000, 4000, 5000, 6000],
    'Conversions': [20, 45, 70, 90, 110, 130]
}

fig = px.scatter(data, 
                 x='Ad Spend ($)', 
                 y='Conversions',
                 title='Ad Spend vs Conversions',
                 size='Conversions',
                 color='Ad Spend ($)')

fig.show()

βœ… Insight:
A positive trend shows that higher ad spend is leading to more conversions β€” a common pattern in digital campaign analysis.


πŸ”Ή Example 4: Scatter Plot with Trendline

Plotly can automatically fit a trendline to reveal correlations clearly:

import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Ad Spend ($)': [1000, 2000, 3000, 4000, 5000, 6000],
    'Conversions': [20, 45, 70, 90, 110, 130]
})

fig = px.scatter(df, 
                 x='Ad Spend ($)', 
                 y='Conversions',
                 title='Ad Spend vs Conversions (with Trendline)',
                 trendline='ols')  # Ordinary Least Squares regression

fig.show()

πŸ” Interpretation:
The line represents the best-fit trend. A strong upward slope means ad spend is effectively driving conversions.


πŸ’¬ Business Use Case

As a Digital Marketing Analyst, scatter plots help you:

  • Find the sweet spot of ROI (where spend = maximum return)
  • Identify diminishing returns at higher budgets
  • Communicate visually with management or clients using interactive reports

🎨 Adding More Interactivity

Plotly charts allow tooltips, hover data, and legends to be customized.

fig = px.scatter(
    df,
    x='Ad Spend ($)',
    y='Conversions',
    hover_data=['Ad Spend ($)', 'Conversions'],
    title='Interactive Ad Spend Analysis',
    template='plotly_white'
)
fig.show()

βœ… Hovering shows detailed values β€” great for presentation-ready dashboards.


🧩 Summary

ConceptLine PlotScatter Plot
PurposeShow trend over timeShow relationship between variables
Best ForTime series data (e.g., sales, traffic)Correlation or regression analysis
Plotly Functionpx.line()px.scatter()
InteractivityHover, zoom, exportHover, zoom, regression line
Use Case ExampleMonthly revenue trendAd spend vs conversion analysis

πŸŽ“ What’s Next?

In Chapter 3 – Bar and Pie Charts in Plotly, we’ll visualize categorical and proportional data β€” such as campaign comparisons, product category shares, or country-wise sales distribution β€” all with interactive visuals and real-world insights.

By Ankit Srivastava – Lead Digital Marketing Analyst & IT Trainer, Slidescope Institute


🎯 Objective

In this chapter, we’ll learn how to create Line Plots and Scatter Plots using Plotly in Python. These are the foundation of data visualization β€” ideal for tracking trends, identifying correlations, and analyzing performance over time.


🧠 Before We Begin

We’ll use Plotly Express, the high-level interface of Plotly, because it allows us to create beautiful and interactive graphs with minimal code.

Make sure Plotly is installed:

pip install plotly

And import the library:

import plotly.express as px

πŸ“ˆ Line Plots – Tracking Trends Over Time

A line plot connects data points with a continuous line β€” it’s perfect for visualizing trends such as sales growth, website traffic, or ad impressions over time.


πŸ”Ή Example 1: Simple Line Plot

import plotly.express as px

data = {
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Website Visitors': [1500, 1800, 2200, 2400, 2600, 3100]
}

fig = px.line(data, 
              x='Month', 
              y='Website Visitors', 
              title='Monthly Website Traffic Trend',
              markers=True)

fig.show()

βœ… Explanation:

  • x β†’ Months
  • y β†’ Visitors count
  • markers=True adds circle markers to each data point

πŸ” Hovering on each point shows exact values β€” helping you interpret growth or drops instantly.


πŸ”Ή Example 2: Multiple Line Comparison

When analyzing data from different sources (like Google Ads vs Meta Ads), multiple lines help you compare performance over time.

import pandas as pd
import plotly.express as px

data = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'] * 2,
    'Visitors': [1500, 1800, 2200, 2400, 2600, 3100, 
                 1000, 1200, 1800, 2100, 2300, 2500],
    'Source': ['Google Ads']*6 + ['Meta Ads']*6
})

fig = px.line(data, 
              x='Month', 
              y='Visitors', 
              color='Source', 
              title='Traffic Comparison: Google Ads vs Meta Ads',
              markers=True)

fig.show()

βœ… Insight:
You can quickly identify which campaign is performing better month by month β€” perfect for marketing reports and dashboard presentations.


πŸ’‘ Customizing Your Line Chart

Plotly allows customization of titles, axis labels, hover data, and themes easily:

fig.update_layout(
    title='Traffic Growth by Platform',
    xaxis_title='Month',
    yaxis_title='Visitors',
    template='plotly_dark'
)

This gives your chart a modern, dark theme, perfect for dashboard presentations.


πŸ”· Scatter Plots – Visualizing Relationships

A scatter plot displays data points on two axes (X and Y) β€” great for identifying relationships or correlations between variables, such as:

  • Advertising Spend vs Conversions
  • Study Hours vs Test Score
  • Product Price vs Sales Volume

πŸ”Ή Example 3: Simple Scatter Plot

import plotly.express as px

data = {
    'Ad Spend ($)': [1000, 2000, 3000, 4000, 5000, 6000],
    'Conversions': [20, 45, 70, 90, 110, 130]
}

fig = px.scatter(data, 
                 x='Ad Spend ($)', 
                 y='Conversions',
                 title='Ad Spend vs Conversions',
                 size='Conversions',
                 color='Ad Spend ($)')

fig.show()

βœ… Insight:
A positive trend shows that higher ad spend is leading to more conversions β€” a common pattern in digital campaign analysis.


πŸ”Ή Example 4: Scatter Plot with Trendline

Plotly can automatically fit a trendline to reveal correlations clearly:

import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Ad Spend ($)': [1000, 2000, 3000, 4000, 5000, 6000],
    'Conversions': [20, 45, 70, 90, 110, 130]
})

fig = px.scatter(df, 
                 x='Ad Spend ($)', 
                 y='Conversions',
                 title='Ad Spend vs Conversions (with Trendline)',
                 trendline='ols')  # Ordinary Least Squares regression

fig.show()

πŸ” Interpretation:
The line represents the best-fit trend. A strong upward slope means ad spend is effectively driving conversions.


πŸ’¬ Business Use Case

As a Digital Marketing Analyst, scatter plots help you:

  • Find the sweet spot of ROI (where spend = maximum return)
  • Identify diminishing returns at higher budgets
  • Communicate visually with management or clients using interactive reports

🎨 Adding More Interactivity

Plotly charts allow tooltips, hover data, and legends to be customized.

fig = px.scatter(
    df,
    x='Ad Spend ($)',
    y='Conversions',
    hover_data=['Ad Spend ($)', 'Conversions'],
    title='Interactive Ad Spend Analysis',
    template='plotly_white'
)
fig.show()

βœ… Hovering shows detailed values β€” great for presentation-ready dashboards.


🧩 Summary

ConceptLine PlotScatter Plot
PurposeShow trend over timeShow relationship between variables
Best ForTime series data (e.g., sales, traffic)Correlation or regression analysis
Plotly Functionpx.line()px.scatter()
InteractivityHover, zoom, exportHover, zoom, regression line
Use Case ExampleMonthly revenue trendAd spend vs conversion analysis

πŸŽ“ What’s Next?

In Chapter 3 – Bar and Pie Charts in Plotly, we’ll visualize categorical and proportional data β€” such as campaign comparisons, product category shares, or country-wise sales distribution β€” all with interactive visuals and real-world insights.