Line Chart using Plotly in Python

Plotly is an interactive, open-source toolkit that enables users to build a wide range of aesthetically pleasing and intelligent charts for data visualization in Python. The line chart, one of the most popular chart forms, is the subject of this article. We'll explore how to create line charts using Plotly with practical examples.

Plotly is a flexible library that supports a wide range of chart styles, offering countless opportunities to tell engaging stories with data.

A Brief Overview of Plotly

Plotly is a powerful Python library that supports interactive and browser-based graph generation. You can create complex graphs that users can modify with ease, enabling a more thorough data exploration experience.

Line charts (also known as line plots) are used to display data points connected by straight lines, typically showing trends over time or continuous data.

Plotly Installation

Use the pip command below to install Plotly if it isn't already installed ?

pip install plotly

Building a Basic Line Chart

You can create a basic line chart in Plotly by importing the Plotly Express module and using the line() function, which accepts data in several forms like Pandas DataFrames or lists of arrays.

Example 1: Basic Line Chart

Let's begin by creating a straightforward line chart to display stock price changes over five days ?

import plotly.express as px

# Data
days = list(range(1, 6))
stock_price = [100, 105, 98, 105, 110]

# Create a line chart
fig = px.line(x=days, y=stock_price, labels={'x':'Days', 'y':'Stock Price'}, 
              title='Stock Price Over Time')

# Show the plot
fig.show()

In this example, we create a simple line chart with stock prices on the y-axis and day numbers on the x-axis.

Example 2: Multiple Line Chart

Let's compare the stock prices of two different companies using multiple lines ?

import plotly.graph_objects as go

# Data
days = list(range(1, 6))
company_A_stock = [100, 105, 98, 105, 110]
company_B_stock = [95, 102, 95, 102, 108]

# Create figure
fig = go.Figure()

# Add traces
fig.add_trace(go.Scatter(x=days, y=company_A_stock, mode='lines+markers', 
                         name='Company A'))
fig.add_trace(go.Scatter(x=days, y=company_B_stock, mode='lines+markers', 
                         name='Company B'))

# Update layout
fig.update_layout(title='Stock Price Comparison',
                  xaxis_title='Days',
                  yaxis_title='Stock Price')

fig.show()

This approach uses plotly.graph_objects which provides more control over chart customization.

Example 3: Customizing the Line Chart

Line charts can be customized in various ways with Plotly. Let's modify the appearance and styling ?

import plotly.graph_objects as go

# Data
days = list(range(1, 6))
company_A_stock = [100, 105, 98, 105, 110]
company_B_stock = [95, 102, 95, 102, 108]

# Create figure
fig = go.Figure()

# Add traces with custom styling
fig.add_trace(go.Scatter(x=days, y=company_A_stock, mode='lines+markers',
                         name='Company A', line=dict(color='blue', width=3)))
fig.add_trace(go.Scatter(x=days, y=company_B_stock, mode='lines+markers',
                         name='Company B', line=dict(color='red', width=3)))

# Customize layout
fig.update_layout(
    title='Stock Price Comparison',
    xaxis_title='Days',
    yaxis_title='Stock Price',
    width=600,
    height=400,
    plot_bgcolor='lightgray',
    paper_bgcolor='white'
)

fig.show()

This example customizes line colors, width, plot size, and background colors using the update_layout() method.

Example 4: Interactive Line Chart with Hover Data

Plotly's interactivity is one of its key strengths. You can customize hover information to enhance user experience ?

import plotly.graph_objects as go

# Data
days = list(range(1, 6))
company_A_stock = [100, 105, 98, 105, 110]
company_B_stock = [95, 102, 95, 102, 108]

# Create figure
fig = go.Figure()

# Add traces with custom hover templates
fig.add_trace(go.Scatter(
    x=days, y=company_A_stock, mode='lines+markers',
    name='Company A',
    hovertemplate='<b>Company A</b><br>Day: %{x}<br>Price: $%{y}<extra></extra>'
))

fig.add_trace(go.Scatter(
    x=days, y=company_B_stock, mode='lines+markers',
    name='Company B',
    hovertemplate='<b>Company B</b><br>Day: %{x}<br>Price: $%{y}<extra></extra>'
))

# Update layout
fig.update_layout(
    title='Interactive Stock Price Comparison',
    xaxis_title='Days',
    yaxis_title='Stock Price ($)',
    hovermode='x unified'
)

fig.show()

The hovertemplate customizes the hover information display. The placeholders %{x} and %{y} represent x and y values, while <extra></extra> removes the default trace box.

Key Features Comparison

Approach Best For Complexity Customization
plotly.express Quick simple charts Low Limited
plotly.graph_objects Advanced customization Medium High
Combined approach Complex interactive charts High Maximum

Conclusion

Plotly provides excellent tools for creating interactive line charts with extensive customization options. Use plotly.express for quick visualizations and plotly.graph_objects for advanced customization and interactivity.

Updated on: 2026-03-27T08:18:50+05:30

756 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements