• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 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. 

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.

1. Quick Examples of Plot of Pandas DataFrame

If you are in a hurry below are some quick Python examples of how to plot the columns of pandas DataFrame in a bar chart by using DataFrame.plot().


# Following are the quick examples

# 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")

2. Syntax of Pandas plot()

Following is the syntax of the plot().


# Syntax of plot()
DataFrame.plot(x=None, y=None, **kwds)

2.1 Parameters of the plot()

  • x : (label or position, optional) Is used to allow the plotting of categorical data versus measured values. If not specified, the index of the DataFrame is used.
  • y : (label or position, optional)Is used to allow the plotting of categorical data versus measured values. If not specified, all numerical columns are used.
  • **kwds : Additional keyword arguments.

2.2 Return Value

  • matplotlib.axes.Axes or np.ndarray.

3. Use a Plot Bar Chart in Pandas.

The pandas DataFrame class in Python has a member plot() that is used to draw various diagrams for visualization including the Bar Chart. Pandas provide different representations for showing the data in the form of graphs. One of the important diagrams is a Bar Plot which is rapidly used in many applications and presentations.

We can make bar charts quickly and easily with the data from Pandas DataFrames. The bar graph is one of the best for fast data exploration and comparison of variable values between different groups. The main purpose of bar charts or bar plots is to attract user’s eyes by providing a clear look for the ability to compare the length of the objects.

4. 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.

Using stacked and unstacked bars we can also compare columns of a given DataFrame and present them in a bar graph. For that, we need to create multiple columns in a DataFrame. Let’s create DataFrame with multiple columns.


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

5. Unstacked Multiple Columns of Bar Plots

Python Pandas un-stacked bar chart. If you select more than one column, pandas by default create an unstacked bar chart with each column forming a bar and the DataFrame index as the x-axis.

Use Unstacked bar plots to compare a particular category with different samples. As we can see from the below, it shows the death rate of coronavirus over the three 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

6. Stacked Multiple Columns of Bar Plots

Stacked bar charts show the total quantity of each group. Using stacked bar plots we can compare each individual. For, that we need to set the stacked keyword with the value True. Stacked bar plots have each plot stacked one over them.


# Create multiple columns of stacked bar plot
df.plot(kind="bar", stacked = True)

Yields below output.

Pandas plot columns
Stacked Pandas plot bar

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

8. Conclusion

In this article, I have explained pandas DataFrame.plot() is used to create stacked and unstacked plots when you have multiple columns on DataFrame.

Happy learning !!

References

Vijetha

Vijetha is an experienced technical writer with a strong command of various programming languages. She has had the opportunity to work extensively with a diverse range of technologies, including Python, Pandas, NumPy, and R. Throughout her career, Vijetha has consistently exhibited a remarkable ability to comprehend intricate technical details and adeptly translate them into accessible and understandable materials. Follow me at Linkedin.