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 in the field of data visualisation in Python. The line chart, one of the most popular chart forms, is the subject of this article. In order to help you comprehend, we'll go over how to make line charts using Plotly while using practical examples.

Even though we'll focus mostly on line charts, keep in mind that Plotly is a flexible library that supports a wide range of additional chart styles, offering countless opportunities to tell engaging tales with data.

A Brief Overview of Plotly

Python interactive and browser-based graph generation is supported by the powerful module Plotly. You may create complex graphs that the user can modify with ease, enabling a more thorough data exploration experience.

Making Line Charts in Python Using Plotly

Data can be displayed along a number line using line charts, often known as line plots. Let's get started making line charts in Python with Plotly.

Plotly Installation

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

pip install plotly

Building a Basic Line Chart

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

Practical Examples of Line Charts Using Plotly

Let's move on to some practical examples of line chart creation with Plotly.

Example 1: Basic Line Chart

We'll begin by creating a straightforward line graph to display the increase in a stock price over a period of five days −

import plotly.express as px

# Data
days = 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'})

# Show the plot
fig.show()

In this demonstration, we'll make 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 expand on our example and compare the growth in stock prices of two different businesses −

import plotly.express as px

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

# Create a line chart
fig = px.line(x=days, y=company_A_stock, labels={'x':'Days', 'y':'Stock Price'})
fig.add_trace(px.line(x=days, y=company_B_stock, labels={'x':'Days', 'y':'Stock Price'}).data[0])

# Show the plot
fig.show()

Add_trace() is used in this code to add the second line to the chart.

Example 3: Customizing the Line Chart

Line charts can be customised in a variety of ways with Plotly. Let's modify the line graph from the preceding illustration:

import plotly.express as px

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

# Create a line chart
fig = px.line(x=days, y=company_A_stock, labels={'x':''Days', 'y':'Stock Price'}, title='Stock Price Comparison')
fig.add_trace(px.line(x=days, y=company_B_stock, labels={'x':'Days', 'y':'Stock Price'}).data[0])

# Customize chart
fig.update_layout(
   autosize=False,
   width=500,
   height=500,
   margin=dict(
      l=50,
      r=50,
      b=100,
      t=100,
      pad=4
   ),
   paper_bgcolor="LightSteelBlue",
)

# Add custom names to traces
fig.data[0].name = "Company A"
fig.data[1].name = "Company B"

# Show the plot
fig.show()

The plot size, margins, and background colour are modified in the aforementioned example using the update_layout() function. The name element of data[ is used to customise the names of the traces (lines).

Example 4: Interactive Line Chart with Hover Data

Plotly's interactivity is what gives it its power. To improve the interactivity, users can personalise the hover information using the library:

import plotly.express as px

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

# Create a line chart
fig = px.line(title='Stock Price Comparison')
fig.add_trace(px.line(x=days, y=company_A_stock, labels={'x':'Days', 'y':'Stock Price'}).data[0])
fig.add_trace(px.line(x=days, y=company_B_stock, labels={'x':'Days', 'y':'Stock Price'}).data[0])

# Add custom hover data
fig.data[0].name = "Company A"
fig.data[0].hovertemplate = "Day: %{x}<br>Stock Price: %{y}<extra></extra>"
fig.data[1].name = "Company B"
fig.data[1].hovertemplate = "Day: %{x}<br>Stock Price: %{y}<extra></extra>"

# Show the plot
fig.show()

The hover data in this case is customised using the hovertemplate. The values for x and y are indicated by the placeholders %x and %y. The trace name is taken out of the hover information by the extra>/extra> element.

Conclusion

Line charts are essential tools for data visualisation, and Plotly offers a great platform for quickly and easily creating and customising these charts. Although we've just begun to scratch the surface of what Plotly and line charts are capable of, we hope that these examples give you a strong basis to begin developing your own visualisations.

Always keep in mind that mastering data visualisation requires more than simply understanding a library's syntax or instructions; it also requires knowing which type of plot is best appropriate for the given set of data. So continue playing with the various chart kinds available in Plotly and keep honing your data visualisation techniques.

Updated on: 18-Jul-2023

216 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements