Line Chart – How to create a Line Chart with Plotly Python

Spread the love

In this post, you will learn –

1 . How to create a line chart with plotly express

2 . How to create multiple line charts with plotly express

3 . How to create a line chart with plotly graph objects

4 . How to create multiple line charts with plotly graph objects

1 . How to create a line chart with plotly express

Plotly express is the easy-to-use, high-level interface to Plotly. It makes it possible to draw complicated figure with fewer lines of code.

Let’s read a dataset to work with and see how to use plotly express to plot a line chart.

import pandas as pd
import numpy as np

df = pd.read_csv("https://raw.githubusercontent.com/bprasad26/lwd/master/data/ICICIBANK.NS.csv")
df.head()

We will work with a banks stock price dataset.

Let’s say you want to plot the close price on the y axis and the date on the x axis.

import plotly.express as px
fig = px.line(df, x='Date', y='Close', title='ICICI BANK stock prices')
fig.show()

2 . How to create multiple line charts with plotly express

If you want to create multiple line chats on the same plot using plotly express, then you need to pass the name of the columns in list to the y axis.

import plotly.express as px
fig = px.line(df, x='Date', y=['Open','Close'], title='ICICI BANK stock prices')
fig.show()

For more information – Plotly express line docs

3 . How to create a line chart with Plotly graph objects

To create a line chart with Plotly graph objects, you need to use go.Scatter. go.Scatter can be used both for plotting points (makers) or lines, depending on the value of mode.

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(x=df["Date"], y=df["Close"]))
fig.update_layout(
    title="ICICI BANK stock prices", xaxis_title="Date", yaxis_title="Close"
)
fig.show()

4 . How to create multiple line charts with plotly graph objects

To create multiple line charts on the same plot with plotly graph objects, all you have to do is add another trace to the plot.

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(x=df["Date"], y=df["Close"], name="Close", mode="lines"))
fig.add_trace(go.Scatter(x=df["Date"], y=df["Open"], name="Open", mode="lines"))
fig.update_layout(
    title="ICICI BANK stock prices", xaxis_title="Date", yaxis_title="Close"
)
fig.show()

If you look closely, you can see that I have also added the name parameter to add the labels for the legend and also explicitly added the mode=’lines’ to tell plotly that i want a line chart.

For more information – plotly line chart graph objects docs

1 . How to install plotly python with pip

2 . How to create Scatter plot in Plotly Python

3 . How to create a Bar Chart in Plotly Python

4 . How to create Horizontal Bar Chart in Plotly Python

5 . How to create a Histogram in plotly python

6 . How to Create a Box Plot in Plotly Python

7 . How to create a Pie Chart in Plotly Python

8 . How to create a Dot Plot in Plotly Python

9 . How to Create Heatmap with Plotly Python

10 . How to Create a Violin Plot in Plotly Python

11 . How to Create Subplots in Plotly Python

Rating: 1 out of 5.

Leave a Reply