Last modified: Jun 02, 2026
Plotly Graph Objects Line Chart Guide
Line charts show trends over time. They connect data points with straight lines. This makes patterns easy to see. Plotly graph objects let you build these charts with full control.
Unlike Plotly Express, graph objects give you low-level access. You set every detail yourself. This is great for complex visualizations. Beginners can start here too.
In this guide, you will learn to make line charts. We will cover setup, basic charts, and advanced styling. Each step includes code and output.
What is a Plotly Graph Object?
A graph object is a building block. It represents a chart element. For line charts, we use go.Scatter with mode='lines'. This tells Plotly to draw lines between points.
You combine graph objects into a figure. The figure holds the layout and data. You control everything from colors to axes.
If you want a broader view, read our Python Plotly Graph Objects Visualization Guide. It covers all chart types.
Setting Up Your Environment
First, install Plotly. Use pip in your terminal.
pip install plotly
Now import the library. We need plotly.graph_objects and plotly.io.
import plotly.graph_objects as go
import plotly.io as pio
That is all you need. Let us create your first line chart.
Creating a Basic Line Chart
Start with simple data. We will plot x and y values.
# Data for the line chart
x_data = [1, 2, 3, 4, 5]
y_data = [10, 15, 13, 17, 20]
# Create a scatter trace with lines
trace = go.Scatter(
x=x_data,
y=y_data,
mode='lines', # Draw lines between points
name='Line 1'
)
# Build the figure
fig = go.Figure(data=[trace])
# Show the chart
fig.show()
This creates a simple line chart. The mode='lines' parameter is key. Without it, you get only markers.
Here is the output you will see.
# A line chart appears in your browser.
# X-axis: 1, 2, 3, 4, 5
# Y-axis: 10, 15, 13, 17, 20
# Line connects all points smoothly.
Adding Markers to the Line
Markers highlight data points. Use mode='lines+markers'.
# Add markers to the line
trace_with_markers = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[10, 15, 13, 17, 20],
mode='lines+markers', # Lines and dots
marker=dict(size=10, color='red'), # Custom marker style
name='With Markers'
)
fig = go.Figure(data=[trace_with_markers])
fig.show()
Markers make each point visible. Use them when data is sparse. They help the reader see exact values.
Customizing Line Style
Change line color, width, and dash. Use the line dictionary.
# Custom line style
trace_custom = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[10, 15, 13, 17, 20],
mode='lines',
line=dict(
color='green', # Line color
width=4, # Line thickness
dash='dash' # Dashed line
),
name='Custom Line'
)
fig = go.Figure(data=[trace_custom])
fig.show()
You can use 'solid', 'dash', 'dot', or 'dashdot'. Experiment to match your style.
Multiple Lines on One Chart
Compare datasets by adding multiple traces. Each trace is a separate line.
# Two lines on the same chart
trace1 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[10, 15, 13, 17, 20],
mode='lines+markers',
name='Series A'
)
trace2 = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[5, 10, 8, 12, 15],
mode='lines+markers',
name='Series B'
)
fig = go.Figure(data=[trace1, trace2])
fig.show()
Multiple lines reveal relationships. Use different colors for clarity. The name attribute creates a legend.
Customizing the Layout
Make your chart readable. Add titles, axis labels, and grid lines.
# Full layout customization
trace = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[10, 15, 13, 17, 20],
mode='lines+markers',
name='Data'
)
layout = go.Layout(
title='Monthly Sales Trend',
xaxis=dict(title='Month', showgrid=True),
yaxis=dict(title='Sales ($)', showgrid=True),
showlegend=True
)
fig = go.Figure(data=[trace], layout=layout)
fig.show()
Layout options are vast. Set background color, font size, and more. This improves readability.
For more layout tips, check the Python Plotly Graph Objects Visualization Guide.
Using Real Data
Line charts often show time series. Here is an example with dates.
# Time series data
import datetime
dates = [
datetime.datetime(2023, 1, 1),
datetime.datetime(2023, 2, 1),
datetime.datetime(2023, 3, 1),
datetime.datetime(2023, 4, 1)
]
values = [100, 120, 115, 130]
trace = go.Scatter(
x=dates,
y=values,
mode='lines+markers',
name='Stock Price'
)
layout = go.Layout(
title='Stock Price Over Time',
xaxis=dict(title='Date'),
yaxis=dict(title='Price ($)')
)
fig = go.Figure(data=[trace], layout=layout)
fig.show()
Plotly handles dates automatically. It formats the axis for you.
Adding Annotations
Highlight important points. Use add_annotation.
# Annotate a peak point
trace = go.Scatter(
x=[1, 2, 3, 4, 5],
y=[10, 15, 13, 17, 20],
mode='lines+markers',
name='Data'
)
fig = go.Figure(data=[trace])
fig.add_annotation(
x=4, y=17,
text='Peak Sales',
showarrow=True,
arrowhead=1
)
fig.show()
Annotations guide the reader. Use them sparingly to avoid clutter.
Exporting the Chart
Save your chart as an image or HTML file.
# Save as HTML (interactive)
fig.write_html('line_chart.html')
# Save as PNG image (static)
fig.write_image('line_chart.png')
For PNG, you need kaleido or orca. Install with pip if needed.
pip install -U kaleido
Common Pitfalls
Beginners often forget the mode parameter. Without it, you get no lines. Always set mode='lines' or 'lines+markers'.
Another issue is data ordering. Line charts connect points in order. Sort your x values if needed.
If your chart looks empty, check the show() method. It must be called on the figure object.
Conclusion
Plotly graph objects give you full control over line charts. You learned to create basic charts, add markers, style lines, and customize layouts. You also saw how to handle time series and add annotations.
Practice with your own data. Try different mode options. Experiment with colors and dashes. For deeper learning, our Python Plotly Graph Objects Visualization Guide has more examples.
Line charts are powerful for showing trends. With Plotly, they become interactive and beautiful. Start building today.