Line Chart in Plotly
Last Updated: 08 Nov 2025
Line charts connect data points with smooth or straight lines — ideal for time series, trends, stock prices, or sensor data.
Hinglish Tip: Line chart = "time ke saath change dikhao" — jaise heartbeat monitor. Interactive → zoom karo, hover karo!
Basic Line Chart
import plotly.express as px
# Sample data
x = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
y = [10, 15, 13, 18, 22]
fig = px.line(x=x, y=y, title="Basic Line Chart")
fig.show()
Key Parameters in px.line()
| Parameter | Values / Examples | Purpose |
|---|---|---|
| data_frame | df | Use Pandas DataFrame |
| x, y | Column names or lists | X-axis (time), Y-axis (value) |
| color | Column name | Different lines per category |
| line_dash | Column or 'dash', 'dot' | Dashed, dotted lines |
| markers | True, False | Show dots on data points |
| hover_data | ['extra_col'] | Extra info on hover |
| title | String | Chart title |
| labels | 'y' : 'Sales (₹)' | Rename axes |
| render_mode | 'svg' or 'webgl' | webgl = faster for 10k+ points |
Full Example
import plotly.express as px
import pandas as pd
# Sample sales data
df = pd.DataFrame({
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
'Product A': [100, 120, 110, 140, 160, 180],
'Product B': [80, 90, 95, 85, 100, 110],
'Region': ['North', 'North', 'North', 'South', 'South', 'South']
})
fig = px.line(
df,
x='Month',
y=['Product A', 'Product B'],
color='Region',
markers=True,
line_dash='Region',
title='Monthly Sales: Product A vs B (North vs South)',
labels={
'value': 'Sales (₹ in Thousands)',
'variable': 'Product'
},
hover_data={'Region': True}
)
fig.update_layout(
legend_title="Region",
hovermode='x unified', # Shows all values at same x
font=dict(size=12)
)
fig.show()
Hinglish Tip: hovermode='x unified' → ek hi x pe saare values dikhega — comparison easy!
Add Fill Area (Area Chart)
fig = px.area(
df, x='Month', y='Product A',
title='Product A Sales (Filled Area)',
labels={'Product A': 'Revenue'}
)
fig.show()
Use
px.area()for filled line charts.
Multiple Lines with graph_objects
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=df['Month'], y=df['Product A'],
mode='lines+markers',
name='Product A',
line=dict(color='royalblue', width=3),
marker=dict(size=10)
))
fig.add_trace(go.Scatter(
x=df['Month'], y=df['Product B'],
mode='lines',
name='Product B',
line=dict(color='firebrick', dash='dash')
))
fig.update_layout(
title='Custom Line Chart with Graph Objects',
xaxis_title='Month',
yaxis_title='Sales',
hovermode='x'
)
fig.show()
Hinglish Tip:
mode='lines+markers'→ line + dots = professionaly to read!
Save Interactive Plot
# Interactive HTML
fig.write_html("sales_line_chart.html")
# Static Image (PNG, PDF)
fig.write_image("sales_line_chart.png", engine="kaleido", width=1000, height=600)
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| No markers → hard to see points | Use markers=True |
| Too many lines → messy | Use line_dash + color wisely |
| Hover shows junk | Use hovermode='x unified' |
| Plot lags with big data | Use render_mode='webgl' |