Pandas plot()
function is used to plot the multiple columns from the given DataFrame. If we plot the bar graph and set the kind
parameter to the bar
of a plot()
function, it will plot the bar graph of multiple columns. We can plot data directly from your DataFrame using this function.
The plot()
function works on both Series
and DataFrame
. In this article, I will explain the syntax of the plot()
function and how we can plot the multiple columns of Pandas DataFrame.
Key Points –
- Use the
DataFrame.plot()
function to create various types of plots, including bar, line, scatter, etc., for visualizing columns of data. - Use the
kind
parameter to define the plot type (e.g.,bar
,line
,hist
,scatter
,area
, etc.). - By default,
plot()
will plot all numerical columns from the DataFrame. You can also select specific columns by indexing the DataFrame. - The DataFrame’s index is used as the x-axis by default. You can modify the index or use a specific column as the x-axis.
- You can add titles, labels, and other visual enhancements using
matplotlib
functions likeplt.title()
,plt.xlabel()
, andplt.ylabel()
. - Use the
stacked=True
option for stacked bar plots, and set thewidth
parameter to control bar width in bar plots. - Use the
ax
parameter to plot on an existingmatplotlib
axis, allowing for complex multi-plot layouts.
Quick Examples of Plot Columns
Below are quick examples of how to plot the columns of pandas DataFrame.
# Quick examples of Plot columns
# Example 1: Create unstacked multiple columns bar
df.plot(kind="bar", figsize = (2, 4))
# Example 2: create multiple columns of stacked bar plot
df.plot(kind="bar", stacked = True)
# Example 3: Create customized multiple columns bar plot
df.plot(kind="bar", stacked = True)
plot.title("Death rate if corona virus")
plot.xlabel("Country")
plot.ylabel("Death Rate")
Pandas plot() Introduction
Following is the syntax of the plot()
.
# Syntax of plot()
DataFrame.plot(x=None, y=None, **kwds)
Parameters
Following are the parameters of the Pandas plot() function.
x :
Label or position depending on the kind of plot.y :
This parameter specifies the data to be plotted along the y-axis. It follows the same rules as thex
parameter.**kwds :
Additional keyword arguments that are passed to the plotting methods. This can include parameters such askind
,ax
,subplots
,figsize
,title
,grid
,legend
, etc., which vary depending on the type of plot you’re creating.
Return Value
- The
plot()
function in Pandas returns a Matplotlib AxesSubplot object or an array of such objects ifsubplots=True
is specified.
Use Plot Bar Chart
The Pandas DataFrame class in Python offers a versatile member function called plot()
, which serves the purpose of generating various types of visualizations, including the Bar Chart. Pandas facilitates diverse representations for presenting data through graphical means. Among these representations, the Bar Plot stands out as a crucial tool, extensively employed across various applications and presentations.
Pandas provides a convenient and efficient way to create bar charts directly from DataFrame data. Bar charts are incredibly useful for rapid data exploration and comparison of variable values across different groups or categories. Their primary function is to visually represent data in a way that makes it easy for users to compare the lengths of different objects, which aids in quickly identifying patterns, trends, and differences within the dataset.
Plot the Multiple Columns of Pandas DataFrame
We can plot the columns of Pandas Dataframe using the plot()
function here I want to represent the multiple columns of DataFrame in the form of bar graph visualization. For that, we need to set the kind
parameter to bar
pass into plot()
function, it will return the bar graph of DataFrame.
Here’s an example of creating a DataFrame with multiple columns and then plotting both stacked and unstacked bar charts using Pandas
import pandas as pd
import matplotlib.pyplot as plot
# Create DataFrame
df = pd.DataFrame({"1st wave death rate":[316.3, 321.3, 117.2, 38.25, 302.2 ],
"2nd wave death rate":[200.1, 127.2, 60.1, 37.6, 230.1],
"3rd wave death rate":[20.1, 34.1, 12.1, 4.2, 24.3]},
index = ["USA", "Brazil", "Germany", "India", "Uk"])
print(df)
Yields below output.
Unstacked Multiple Columns of Bar Plots
When you select more than one column in a DataFrame for plotting, Pandas by default creates an unstacked bar chart where each column becomes a separate bar, and the DataFrame index serves as the x-axis.
Unstacked bar plots serve as a powerful tool for comparing specific categories across diverse samples. In the following example, we employ unstacked bar plots to visualize the death rate of coronavirus over three distinct waves. Here I have passed the figsize
parameter into this function along with the kind
param, it will return the customized size of the plot bar.
# Create unstacked multiple columns bar
df.plot(kind="bar", figsize = (2, 4))
Yields below output.
Stacked Multiple Columns of Bar Plots
Stacked bar charts provide a comprehensive view of the total quantity for each group. By utilizing stacked bar plots, we can effectively compare individual components within each group. This is achieved by setting the stacked
keyword parameter to True
. Stacked bar plots visually stack each component on top of one another, offering a visual representation of the total quantity while also showcasing the relative contribution of each component within the group.
# Create multiple columns of stacked bar plot
df.plot(kind="bar", stacked = True)
Yields below output.
Customize the Multiple Columns of Bar Graph
We can customize the bar graph by providing any keyword argument to this function. Here, I have customized it by providing the title of the plot bar and axis labels with the help of matplotlib. Let’s customize the bar graph.
# Create customized multiple columns bar plot
df.plot(kind="bar", stacked = True)
plot.title("Death rate if corona virus")
plot.xlabel("Country")
plot.ylabel("Death Rate")
Yields below output.
FAQ on Plot Columns of Pandas DataFrame
To plot a single column of a Pandas DataFrame, you can use the plot()
function on that specific column. By default, it will generate a line plot.
To plot multiple columns of a Pandas DataFrame on the same graph, you can call the plot()
function on the entire DataFrame or select specific columns. Pandas will automatically plot each selected column as a separate line on the same graph.
You can customize the type of plot in Pandas by using the kind
parameter in the plot()
function. Pandas supports various types of plots, such as line, bar, histogram, scatter, etc.
To add labels and a title to your plot, you can use the xlabel()
, ylabel()
, and title()
functions from Matplotlib.
To plot a DataFrame with a specific color or line style, you can use the color
and linestyle
(or style
) parameters in the plot()
function. You can customize the color of the plot lines and set various line styles such as dashed, dotted, or solid lines.
Conclusion
In this article, you have learned how the Pandas DataFrame.plot()
function serves to create both stacked and unstacked plots, particularly when dealing with multiple columns within a DataFrame. This method facilitates the visualization of data where comparisons are made across different variables or categories, offering insights into their respective contributions or distributions.
Happy learning !!
Related Articles
- Change Pandas Plot Size
- How to add title to Pandas plots?
- How to generate line plot in Pandas?
- How to add legends to plots in Pandas
- How to Create Scatter Plot in Pandas
- Pretty Print Pandas DataFrame or Series
- How to Generate Time Series Plot in Pandas
- How to Plot the Boxplot from DataFrame
- Pandas Convert String Column To DateTime
- How to distribute column values in Pandas plot?
- Split the column of DataFrame into two columns
- Create Pandas DataFrame With Working Examples
- Select Pandas DataFrame Rows Between Two Dates
- Pandas Check Column Contains a Value in DataFrame
- Pandas Extract Column Value Based on Another Column