• Post author:
  • Post category:Pandas
  • Post last modified:May 14, 2024
  • Reading time:11 mins read
You are currently viewing How to Plot Columns of Pandas DataFrame

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. 

Advertisements

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.

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

  • 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 the x parameter.
  • **kwds : Additional keyword arguments that are passed to the plotting methods. This can include parameters such as kind, 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 if subplots=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.

Pandas plot columns

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.

Pandas plot columns
Unstacked Pandas plot bar

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.

Pandas plot columns
Stacked Pandas plot bar

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.

Pandas plot columns
Pandas plot bar

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 !!

References