
In this post, you will learn how to create a line chart in pandas.
Line Chart –
Read a dataset to work with.
import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/ICICIBANK.NS.csv"
df = pd.read_csv(url, parse_dates=['Date'])
df.head()

To plot a line chart in pandas, we use DataFrame.plot.line() method. Let’s say that you want to plot the close price on the y axis and the date on the x axis.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,8))
df.plot.line(x='Date', y='Close',color='crimson', ax=ax)
plt.ylabel("Closing Price")
plt.show()

Another way to plot a line chart in pandas is using the DataFrame.plot() method. By default it creates a line chart but if you want to plot other kinds of graph then you have to use the kind parameter.
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,8))
df.plot(x='Date', y='Close',color='crimson', ax=ax)
plt.ylabel("Closing Price")
plt.show()

With Kind parameter set
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(10,8))
df.plot(x='Date', y='Close', kind='line', color='crimson', ax=ax)
plt.ylabel("Closing Price")
plt.show()
Create Multiple Line Charts –
Let’s say along with closing price, you also want to plot the opening price.
fig, ax = plt.subplots(figsize=(10,8))
df.plot(x='Date', y='Close',color='crimson', ax=ax)
df.plot(x='Date', y='Open',color='seagreen', ax=ax)
plt.ylabel("Closing Price")
plt.show()

Multiple Line charts on Separate Plot (Subplots) –
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(10,8), sharex=True)
df.plot(x='Date', y='Open',color='seagreen', ax=ax1)
df.plot(x='Date', y='Close',color='crimson', ax=ax2)
ax1.set_ylabel("Opening Price")
ax2.set_ylabel("Closing Price")
plt.show()

Related Posts –
1 . How to create subplots in Matplotlib