#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Interactive Line Charts

Learn to create interactive line charts with Plotly

Interactive Line Charts

Basic Line Chart

code.pyPython
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Sales': [100, 120, 115, 140, 160, 180]
})

fig = px.line(df, x='Month', y='Sales', title='Monthly Sales')
fig.show()

Add Markers

code.pyPython
fig = px.line(df, x='Month', y='Sales', markers=True)
fig.show()

Multiple Lines

code.pyPython
df = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
    'Product_A': [100, 120, 115, 140, 160, 180],
    'Product_B': [80, 95, 100, 110, 130, 150]
})

# Reshape for Plotly
df_long = df.melt(id_vars='Month', var_name='Product', value_name='Sales')

fig = px.line(df_long, x='Month', y='Sales', color='Product', markers=True)
fig.show()

Line with Color Groups

code.pyPython
df = pd.DataFrame({
    'Month': ['Jan', 'Feb', 'Mar']*2,
    'Sales': [100, 120, 115, 80, 95, 100],
    'Region': ['East', 'East', 'East', 'West', 'West', 'West']
})

fig = px.line(df, x='Month', y='Sales', color='Region', markers=True)
fig.show()

Area Chart

Fill under the line:

code.pyPython
fig = px.area(df, x='Month', y='Sales', title='Sales Area Chart')
fig.show()

Stacked Area

code.pyPython
fig = px.area(df_long, x='Month', y='Sales', color='Product')
fig.show()

Custom Line Style

code.pyPython
fig = px.line(df, x='Month', y='Sales')

# Make line thicker and dashed
fig.update_traces(line=dict(width=3, dash='dash'))
fig.show()

Dash options: 'solid', 'dot', 'dash', 'longdash', 'dashdot'

Add Range Slider

code.pyPython
fig = px.line(df, x='Month', y='Sales')
fig.update_xaxes(rangeslider_visible=True)
fig.show()

Adds a slider to zoom in on time periods!

Fill Between Lines

code.pyPython
import plotly.graph_objects as go

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
high = [120, 130, 125, 145, 160]
low = [80, 90, 85, 105, 120]

fig = go.Figure()
fig.add_trace(go.Scatter(x=months, y=high, name='High', line=dict(color='green')))
fig.add_trace(go.Scatter(x=months, y=low, name='Low', fill='tonexty', line=dict(color='red')))
fig.show()

Complete Example

code.pyPython
import plotly.express as px
import pandas as pd

# Stock-like data
df = pd.DataFrame({
    'Date': pd.date_range('2024-01-01', periods=30),
    'Stock_A': [100 + i + (i % 5) for i in range(30)],
    'Stock_B': [80 + i*0.8 + (i % 3) for i in range(30)]
})

df_long = df.melt(id_vars='Date', var_name='Stock', value_name='Price')

fig = px.line(df_long, x='Date', y='Price', color='Stock',
              title='Stock Price Comparison',
              markers=True)

fig.update_xaxes(rangeslider_visible=True)
fig.show()

Key Points

  • px.line() for line charts
  • markers=True shows data points
  • color creates multiple lines
  • px.area() fills under line
  • rangeslider_visible adds zoom slider
  • Use melt() to reshape data for multiple lines

What's Next?

Learn to create interactive bar charts.