Python Pandas DataFrame plot.bar() Function

A bar plot (or bar chart) is a type of graph that displays categorical data using rectangular bars with heights or lengths proportionate to the values they represent. The bars can be plotted horizontally or vertically.

The ability to fast and effectively generate a bar chart from data in Pandas DataFrames is essential for any Python data scientist.

Nothing beats a bar chart for quick data exploration and comparison of variable values between groups, or for developing a storyline around how data groupings are created. At EdgeTier, we frequently end up with an overabundance of bar charts in both exploratory data analysis work and dashboard visualisations.

The advantage of bar charts (also known as “bar plots” or “column charts”) over other chart styles is that the human eye has acquired a refined ability to compare object length rather than angle or area.

Python Pandas DataFrame plot.bar() Function:

A vertical bar plot is created using the DataFrame.plot.bar() function. A bar plot depicts comparisons between discrete categories. The plot’s one axis depicts the particular categories being compared, while the other axis indicates a measured value.

Syntax:

DataFrame.plot.bar(x=None, y=None, **kwargs)

Parameters

x: This is optional. It indicates the label or position. It allows for the plotting of one column versus another. If no index is provided, the DataFrame’s index is taken.

y: This is optional. It indicates the label or position. It allows for the plotting of one column versus another. If no index is provided, all numerical columns are utilized.

**kwargs: This is required. It represents the additional keyword arguments.

Return Value:

matplotlib.axes.Axes or a ndarray is returned containing one matplotlib.axes.Axes per column when subplots=True.

Pandas DataFrame plot.bar() Function in Python

Example1

Approach:

  • Import pandas module using the import keyword.
  • Import pyplot function of the matplotlib module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Plot the bar graph of the firstyear_marks of the students of the given dataframe using the dataframe.plot.bar() function
  • Display the plot using the show() function of the matplotlib module.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import pyplot function of the matplotlib module using the import keyword.
import matplotlib.pyplot as plt
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90],
  "thirdyear_marks": [90, 73, 68, 80],
  "fourthyear_marks": [75, 80, 55, 95]},
  
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Plot the bar graph of the firstyear_marks of the students of the
# given dataframe using the dataframe.plot.bar() function 
data_frme.plot.bar(y=['firstyear_marks'], rot=0)

# Display the plot using the show() function of the matplotlib module 
plt.show()

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks  thirdyear_marks  fourthyear_marks
virat                71                80               90                75
nick                 60                40               73                80
jessy                35                25               68                55
sindhu               85                90               80                95

Example2

Here, the bar plot is produced for the selected columns i.e, firstyear_marks,  secondyear_marks of the given dataframe

Approach:

  • Import pandas module using the import keyword.
  • Import pyplot function of the matplotlib module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  •  Plot the bar graph of the firstyear_marks, secondyear_marks of the students of the given dataframe using the dataframe.plot.bar() function by passing the argument as a list.
  • Display the plot using the show() function of the matplotlib module.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import pyplot function of the matplotlib module using the import keyword.
import matplotlib.pyplot as plt
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90],
  "thirdyear_marks": [90, 73, 68, 80],
  "fourthyear_marks": [75, 80, 55, 95]},
  
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Plot the bar graph of the firstyear_marks, secondyear_marks of the students 
# of the given dataframe using the dataframe.plot.bar() function by passing the
# argument as a list
data_frme.plot.bar(y=['firstyear_marks', 'secondyear_marks'], rot=0)

# Display the plot using the show() function of the matplotlib module 
plt.show()

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks  thirdyear_marks  fourthyear_marks
virat                71                80               90                75
nick                 60                40               73                80
jessy                35                25               68                55
sindhu               85                90               80                95

Example3

A stack bar plot can be constructed by specifying the stacked=True argument.

Approach:

  • Import pandas module using the import keyword.
  • Import pyplot function of the matplotlib module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Plot the bar graph of all the 4 years marks of the students of the given dataframe using the dataframe.plot.bar() function by passing stacked=True as argument.
  • Display the plot using the show() function of the matplotlib module.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import pyplot function of the matplotlib module using the import keyword.
import matplotlib.pyplot as plt
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90],
  "thirdyear_marks": [90, 73, 68, 80],
  "fourthyear_marks": [75, 80, 55, 95]},
  
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Plot the bar graph of all the 4 year marks of the students 
# of the given dataframe using the dataframe.plot.bar() function by passing
# stacked=True as argument.
data_frme.plot.bar(rot=0, stacked=True)

# Display the plot using the show() function of the matplotlib module 
plt.show()

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks  thirdyear_marks  fourthyear_marks
virat                71                80               90                75
nick                 60                40               73                80
jessy                35                25               68                55
sindhu               85                90               80                95

Example4

Here, the bar graph is split column-wise by using the subplots=True argument.

Approach:

  • Import pandas module using the import keyword.
  • Import pyplot function of the matplotlib module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe.
  • Store it in a variable.
  • Print the given dataframe.
  • Plot the bar graph of the firstyear_marks, secondyear_marks of the students of the given dataframe using the dataframe.plot.bar() function by passing the argument as a list, and subplots=True.
  • Here, the bar graph is split column-wise by using the subplots=True argument
  • Display the plot using the show() function of the matplotlib module.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import pyplot function of the matplotlib module using the import keyword.
import matplotlib.pyplot as plt
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "firstyear_marks": [71, 60, 35, 85],
  "secondyear_marks": [80, 40, 25, 90],
  "thirdyear_marks": [90, 73, 68, 80],
  "fourthyear_marks": [75, 80, 55, 95]},
  
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Plot the bar graph of the firstyear_marks, secondyear_marks of the students 
# of the given dataframe using the dataframe.plot.bar() function by passing the
# argument as a list, and subplots=True.
# Here, the bar graph is split column-wise by using the subplots=True argument
data_frme.plot.bar(y=['firstyear_marks', 'secondyear_marks'], rot=0, subplots=True)

# Display the plot using the show() function of the matplotlib module 
plt.show()

Output:

The given Dataframe:
        firstyear_marks  secondyear_marks  thirdyear_marks  fourthyear_marks
virat                71                80               90                75
nick                 60                40               73                80
jessy                35                25               68                55
sindhu               85                90               80                95

Leave a Comment