Pandas DataFrame.plot()
method is used to generate a line plot from the DataFrame. A line
plot is the default plot. It Provides the plotting of one column to another column. If not specified, by default plotting is done over the index of the DataFrame to another numeric column.
In this article, I will explain the concept of a line plot and using plot()
how to plot the line from the given Pandas DataFrame.
Key Points –
- Use the
.plot()
method withkind='line'
to generate a basic line plot for a DataFrame or Series. - By default, calling
.plot()
on a DataFrame generates a line plot with the index on the x-axis and column values on the y-axis. - You can specify individual columns or a subset of columns to plot by indexing the DataFrame or using the
y
parameter. - The DataFrame’s index is automatically used for the x-axis. To use a specific column for the x-axis, use the
x
parameter. - Use the
subplots=True
parameter to generate individual line plots for each column in separate subplots. - If the index or x-axis values are datetime objects, Pandas formats the x-axis accordingly, making it ideal for time series data.
Quick Examples of Line Plot
If you are in a hurry, below are some quick examples of how to generate line plot in a DataFrame.
# Quick examples of line plot
# Example 1: Create a line plot
seattle_temps['temp'].plot()
# Example 2: Default line plot
df.plot()
# Example 3: Get the single line plot
df['min'].plot()
# Example 4: Customize the line plot of DataFrame
df.plot(rot = 60)
plt.xlabel("Index", size = 20)
plt.ylabel("Temp", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
# Example 5: Create multiple line on separate plots
df.plot(subplots = True)
# Example 6: Create timeseries plot
df.plot(x="date", y="min")
plt.xlabel("Date", size = 20)
plt.ylabel("Minimum Temperature", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
Syntax of Pandas plot()
Following is the syntax of the plot() function which I will be using to create a time series plot.
# Syntax of plot()
DataFrame.plot(*args, **kwargs)
Parameters of plot() function
Following are the parameters of the plot() function.
data
: Series or DataFrame.x:
label or position, default None. Only used if data is a DataFrame.y:
label, position or list of label, positions, default None. It allows the plotting of multiple columns. Only used if data is a DataFrame.Kind:
It defines the type of plot to be created, default value isline
.
The kind of plot to produce:
line -
line plot (default)bar -
vertical bar plotbarh -
horizontal bar plothist -
histogrambox -
boxplotkde -
Kernel Density Estimation plotdensity -
same as ‘kde’area -
area plotpie -
pie plotscatter -
scatter plot (DataFrame only)hexbin -
hexbin plot (DataFrame only)
**kwargs:
Options to pass to matplotlib plotting method.
Return Value
It returns matplotlib.axes.Axes
or numpy.ndarray of them
Introduction of Plot.
Python Pandas library is mainly focused on data analysis and it is not only a data visualization library but also using this we can create basic plots. When we want to develop exploratory data analysis plots, pandas is highly useful and practical. It provides plot()
and several other wrapper functions for visualizing our data.
Let’s use this pandas plot()
function to create a time series plot. Here I have taken weather data of Seattle
city from vega_datasets
and using pandas I will plot the line plot of the given dataset.
To access these datasets from Python, you can use the Vega datasets python package. Let’s import weather data of Seattle city, Here columns are date
and temp
. The date column is in the form of yyyy-mm-dd
.
# Import weather dataset
import pandas as pd
import numpy as np
from vega_datasets import data
import matplotlib.pyplot as plt
# Load seattle temperature data
seattle_temps = data.seattle_temps()
print(seattle_temps.shape)
print(seattle_temps.head())
print(seattle_temps.tail())
Yields below output.
# Shape() output:
(8759, 2)
# Head() output:
date temp
0 2010-01-01 00:00:00 39.4
1 2010-01-01 01:00:00 39.2
2 2010-01-01 02:00:00 39.0
3 2010-01-01 03:00:00 38.9
4 2010-01-01 04:00:00 38.8
# Tail() output:
date temp
8754 2010-12-31 19:00:00 40.7
8755 2010-12-31 20:00:00 40.5
8756 2010-12-31 21:00:00 40.2
8757 2010-12-31 22:00:00 40.0
8758 2010-12-31 23:00:00 39.6
Pandas plot() Function to Create Sample line Plot
We can directly pass temp
column into plot()
function to create a line plot by using the above specific column of Seattle’s weather data.
# Create a line plot
seattle_temps['temp'].plot()
Yields below output.
As you can see from the above, we have got a line plot with all the data, here band showing the minimum and maximum temperature for every data. For every hour the temperature data changes over a day. Also, we can observe indices of DataFrame on the x-axis, not the date column.
Extract Date from Datetime
Let’s remove the time part from datetime column.
# Convert date column as simple date
seattle_temps['date'] = seattle_temps['date'].dt.date
print(seattle_temps.tail())
Yields below output.
# Output:
date temp
8754 2010-12-31 40.7
8755 2010-12-31 40.5
8756 2010-12-31 40.2
8757 2010-12-31 40.0
8758 2010-12-31 39.6
Let’s also get minimum and maximum temperatures for each day using Pandas groupby() function along with pandas agg() function.
# Get the min & max temparatures
df = seattle_temps.groupby('date').agg(['min','max'])
print(df)
Yields below output.
# Output:
temp
min max
date
2010-01-01 38.6 43.5
2010-01-02 38.8 43.8
2010-01-03 39.0 44.0
2010-01-04 39.2 44.2
2010-01-05 39.3 44.4
... ...
2010-12-27 37.9 42.8
2010-12-28 38.1 43.0
2010-12-29 38.1 43.0
2010-12-30 38.2 43.1
2010-12-31 38.4 43.3
[365 rows x 2 columns]
Using the pd.droplevel() function we can drop the multi-level column index, here I can drop the level 0
index of a given DataFrame to make a flattened Dataframe. Then, reset the index using reset_index() function.
# Drop the level 0 column & set the index
df.columns = df.columns.droplevel(0)
df.reset_index(level=0, inplace=True)
print(df.head())
Yields below output.
# Output:
date min max
0 2010-01-01 38.6 43.5
1 2010-01-02 38.8 43.8
2 2010-01-03 39.0 44.0
3 2010-01-04 39.2 44.2
4 2010-01-05 39.3 44.4
Default Line Plot using DataFrame
Here, I will create a line plot of the given DataFrame using plot() function, it will take default indices on the x-axis and min and max columns on the y-axis. Finally, it will return the double-line plot.
# Default line plot
df.plot()
Yields below output.
Make a Single Line plot
By using the above-created dataframe let’s plot the min
temperature across different days.
# Get the single line plot
df['min'].plot()
Yields below output.
Customize the Line Plots
We can customize the plots using any keyword arguments pass into plot()
function. rot
keyword allows rotating the markings on the x-axis for horizontal plotting and y-axis for vertical plotting, size
keyword allows to set the font size for the labels of axis points and title of the plots, and colormap
keyword argument allows to choose different color sets for the plots.
Using Matplotlib.pyplot
we can give the labels of the axes and the title of the plot. For example, Here, I use the rot
keyword into plot()
function, it will rotate the marking of the x-axis horizontally.
# Customize the Line plot of DataFrame
df.plot(rot = 60)
plt.xlabel("Index", size = 20)
plt.ylabel("Temp", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
Plot Multiple Lines on Separate Plots
We can create multiple lines on separate plots using plot()
function. For that, we will set and pass the keyword argument argument subplots=True
into this function, it will create multiple lines on separate plots.
# Create multiple line on separate plots
df.plot(subplots = True)
Create Timeseries plot in Pandas
Let’s create timeseries plot with minimum temperature
on y-axis
and date
on x-axis
using plot() function directly on the DataFrame.
# Create timeseries plot
df.plot(x="date", y="min")
plt.xlabel("Date", size = 20)
plt.ylabel("Minimum Temperature", size = 20)
plt.title("Minimum temperature of Seattle", size = 25)
Frequently Asked Questions on Generate Line Plot in a DataFrame
To generate a line plot from a DataFrame using a specific column as the y-axis, you can use the plot
function provided by the pandas library. For example, the plot
function is called on the ‘y_values’ column, and kind='line'
specifies that you want to create a line plot. The resulting line plot will have the index (default x-axis) against the values in the ‘y_values’ column.
You can customize a line plot in pandas by using additional parameters and functions provided by the matplotlib library. Here’s an example of how you can add labels and a title to your line plot.
You can plot multiple lines on the same graph from different columns in a DataFrame. For example, df.plot
is used with the x
parameter set to the column containing x-values and the y
parameter set to a list of columns for y-values. This creates a line plot with multiple lines, and plt.legend
is used to add a legend with labels for each line.
You can change the color or style of the line in a pandas line plot by using the color
and style
parameters in the plot
function.
You can create a line plot for multiple columns in the same DataFrame by specifying the columns you want to plot in the y
parameter of the plot
function. For example, the y
parameter is set to a list of column names ([‘y_values1’, ‘y_values2’]) to create a line plot for both columns on the same graph. The legend is added to differentiate between the lines.
You can save a line plot as an image file using the savefig
function from the matplotlib.pyplot
module. For example, plt.savefig('line_plot.png')
saves the plot as a PNG image file in the current working directory. You can change the file extension to save the plot in different formats (e.g., ‘line_plot.jpg’, ‘line_plot.pdf’).
Conclusion
In this article, I have explained the concept of line plot and by using the plot()
function how to plot the line plot or time series of DataFrame. Also explained how we can customize the line plot and timeseries using optional parameters.
Happy learning !!
Related Articles
- How to Change Pandas Plot Size?
- How to Plot the Pandas Series?
- How to add title to Pandas plot()?
- How to add title to plots in Pandas
- Pandas Correlation of Columns
- How to add legends to plots in Pandas?
- How to Plot a Histogram Using Pandas?
- How to Plot Columns of Pandas DataFrame
- How to Generate Time Series Plot in Pandas
- How to Plot a Scatter Plot Using Pandas?
- How to Plot the Boxplot from DataFrame?
- How to make a histogram in Pandas Series?
- How to distribute column values in Pandas plot?
- Create Pandas Plot Bar Explained with Examples