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
β Monthsy
β Visitors countmarkers=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
Concept | Line Plot | Scatter Plot |
---|---|---|
Purpose | Show trend over time | Show relationship between variables |
Best For | Time series data (e.g., sales, traffic) | Correlation or regression analysis |
Plotly Function | px.line() | px.scatter() |
Interactivity | Hover, zoom, export | Hover, zoom, regression line |
Use Case Example | Monthly revenue trend | Ad 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.