Matplotlib plot bar chart

In this Python tutorial, we will discuss, How to plot a bar chart using matplotlib in Python, and we shall also cover the following topics:

  • Matplotlib plot bar chart
  • Matplotlib plot bar chart size
  • Matplotlib plot bar chart with different colors
  • Matplotlib plot bar chart from dict
  • Matplotlib plot bar chart from dataframe
  • Matplotlib plot bar chart with values
  • Matplotlib plot bar chart with dates
  • Matplotlib plot bar chart with error bars
  • Matplotlib plot multiple bar graphs
  • Matplotlib plot stacked bar chart
  • Matplotlib plot horizontal bar chart

Matplotlib plot bar chart

Matplotlib is the most commonly used data visualization tool-rich library in python. It supports a wide variety of data visualization tools to make 2D plots from the data provided by different sources or of different types like from lists, arrays, dictionaries, DataFrames, JSON files, CSV files, etc. This library is built on the Numpy arrays in python.

You can create bar plots or bar charts or bar graphs in python using the API pyplot provided by the matplotlib library as a submodule in matplotlib. Pyplot provides a variety of plots and functions related to them.

A bar chart is a type of figure that presents different categories in data as rectangular bars with the heights or lengths proportional to the values that these categories represent respectively. It means that the bar plots are used to visualize the categorical data (compare the categories in a data).

You can plot bars vertically or horizontally according to your needs. One of the axes shows the categories to be compared and the other one shows the values of those categories.

You can use the function bar() of the submodule pyplot of module (library) matplotlib to create a bar plot/chart/graph in python. The syntax of the bar() function is as follows:

matplotlib.pyplot.bar(categories, heights [, width, bottom, align, ...])

In the above syntax,

  • The categories specify the value or list/array of the categories to be compared.
  • The heights specify the value or list/array of the values against each respective categories.
  • You can specify the width of the bars in width. The default is 0.8.
  • You can specify the bases of the bars (y-cordinate of the bars bases) in bottom. The default is 0.
  • You can specify the alignment of the bars to the categories axis ticks. The default is ‘centre’:
    • Set it ‘centre’ to align the bars so as the ticks positions get to the centre of the base of the bars.
    • Set it ‘edge’ to align the bars so as the left edges of the bars get at the ticks positions.
    • Set it ‘edge’ with the negative value of the width to align the bars so as the right edges of the bars get at the ticks positions.
  • There are other parameters also that you can specify according to your needs like:
    • color to specify the color of the bars. You can set a color or list of colors.
    • edgecolor to specify the color of the edges of the bars. You can set a color or list of colors.
    • linewidth to specify the width of the edge of the bars, if 0 no edges will be drawn.
    • tick_label to specify the tick labels of the bars.
    • yerr/xerr to specify the error bars at the tip of the bars.
    • ecolor to specify the line color of the errorbars, the default is ‘black’.
    • capsize to specify the length of the errorbar caps in points, the default is 0.0.
    • log to set the scale of the height axis to the log if set True.

You can follow the following mentioned general steps to create a bar chart:

  • Import the libraries necessary to plot the graph (numpy or/and pandas for data inclusion or creation or manipulation, pyplot from the matplotlib for data visualization, etc.).
  • Define the data to be visualized (Define the x and y axis values).
  • Plot command to define the plot with the features (parameters to the command) you want to add on the plot (plot with diffrent color bars, labels, title, legend, etc)
  • Show the graph/plot/chart defined above.

Example :

# Importing the required libraries
from matplotlib import pyplot as plt

# Preparing the data to plot
blogs = ['python', 'sqlserver', 'postgresql', 'mongodb', 'sharepoint']
posts = [257, 213, 112, 101, 456]


# Creating a simple bar chart
plt.bar(blogs, posts)

plt.title('The posts in different blogs')
plt.xlabel('blogs', fontsize=15)
plt.ylabel('posts', fontsize=15)
plt.show()


# Creating a bar chart with the parameters
plt.bar(blogs, posts, width=0.7, bottom=50, align='edge')

plt.title('The posts in different blogs')
plt.xlabel('blogs', fontsize=15)
plt.ylabel('posts', fontsize=15)
plt.show()
Matplotlib plot bar chart
Matplotlib plot bar chart

Read: How to install matplotlib python

Matplotlib plot bar chart size

You can specify the size of the chart using matplotlib in python by using matplotlib.pyplot.figure() function with parameter figsize, which can accept a list of two values representing the width and height of the figure/chart.

Example :

# Importing the required libraries
from matplotlib import pyplot as plt

# Preparing the data to plot
players = ['Cristiano Ronaldo', 'Pele', 'Romario', 'Lionel Messi', 
           'Ferenc Puskas', 'Josef Bican', 'Jimmy Jones', 
           'Gerd Muller', 'Eusebio', 'Joe Bambrick'] 
goals = [783, 765, 753, 748, 729, 720, 647, 634, 622, 616]


# Creating a simple bar chart
plt.bar(players, goals)

plt.title('Top 10 football goal scorers of all time')
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()


# Increase the size of the figure (chart)
plt.figure(figsize=[15, 7])

# Creating a bar chart with the parameters
plt.bar(players, goals, width=0.7)

plt.title('Top 10 football goal scorers of all time')
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()
Matplotlib plot bar chart size
Matplotlib plot bar chart size

Read: Matplotlib plot a line

Matplotlib plot bar chart with different colors

You can specify different colors to different bars in a bar chart. You can do it by specifying the value for the parameter color in the matplotlib.pyplot.bar() function, which can accept the list of color names or color codes or color hash codes.

You can either manually enter the list of color names or can use the color palettes available in matplotlib using a colormap. You can use the function matplotlib.pyplot.get_cmap() with a positional argument specifying the name of the colormap available in matplotlib.

Example :

# Importing the required libraries
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd

# Preparing the data to plot
players = ['Cristiano Ronaldo', 'Pele', 'Romario', 'Lionel Messi', 
           'Ferenc Puskas', 'Josef Bican', 'Jimmy Jones', 
           'Gerd Muller', 'Eusebio', 'Joe Bambrick'] 
goals = [783, 765, 753, 748, 729, 720, 647, 634, 622, 616]


plt.figure(figsize=[15, 7])

# Creating a bar chart with bars of different color
plt.bar(players, goals, width=0.7, edgecolor='blueviolet',
        color=['r', 'y', 'g', 'b', 'c', 'k', 
               'olive', 'gray', 'pink', 'maroon'], linewidth=2)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()


# Set colors to the bars using colormaps available in matplotlib

plt.figure(figsize=[15, 7])
col_map = plt.get_cmap('Paired')

# Creating a bar chart with bars of different color using colormap
plt.bar(players, goals, width=0.7, color=col_map.colors, edgecolor='k', 
        linewidth=2)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()
Matplotlib plot bar chart with different colors
Matplotlib plot bar chart with different colors

Read: Python plot multiple lines

Matplotlib plot bar chart from dict

You can plot a bar chart from the python dictionary by specifying the categories and height values from the dictionary.

Example :

# Preparing the data to plot
data_dict = {'Cristiano Ronaldo': 783, 'Pele': 765, 'Romario': 753,
             'Lionel Messi': 748, 'Ferenc Puskas': 729, 
             'Josef Bican': 720, 'Jimmy Jones': 647, 
             'Gerd Muller': 634, 'Eusebio': 622, 'Joe Bambrick': 616}

print('type: '+str(type(data_dict)), 'size: '+str(len(data_dict)), data_dict, sep='\n')
Python create dictionary
Python create dictionary
# Importing the required libraries
from matplotlib import pyplot as plt

# Visualizing the data

plt.figure(figsize=[15, 7])
col_map = plt.get_cmap('tab20c')

# Creating a bar chart from a dictionary
plt.bar(data_dict.keys(), data_dict.values(), width=0.7, 
        color=col_map.colors, edgecolor='maroon', linewidth=2)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()
Matplotlib plot bar chart from dict
Matplotlib plot bar chart from dict

Matplotlib plot bar chart from dataframe

You can plot a bar chart from the pandas DataFrame by specifying the categories and height values from the columns of the DataFrame.

Example :

In the following example, two dataframes are created, the first one is a normal dataframe with the categories and values for the bar plot as the columns of the dataframe, and the second one is the dataframe with the index as the categories for the bar plot.

# Importing the required libraries
import numpy as np
import pandas as pd

# Preparing the data to plot
players = ['Cristiano Ronaldo', 'Pele', 'Romario', 'Lionel Messi', 
           'Ferenc Puskas', 'Josef Bican', 'Jimmy Jones', 
           'Gerd Muller', 'Eusebio', 'Joe Bambrick'] 
goals = [783, 765, 753, 748, 729, 720, 647, 634, 622, 616]

# Creating a DataFrame from a dictionary
df = pd.DataFrame({'Players': players, 'Goals': goals})
print('type: '+str(type(df)), 'size: '+str(np.shape(df)), df, sep='\n', end='\n\n')

# Another dataframe where index is changed
df2 = df.set_index('Players')
print('type: '+str(type(df2)), 'size: '+str(np.shape(df2)), df2, sep='\n')
Python create DataFrame
Python create DataFrame
# Importing the required libraries
from matplotlib import pyplot as plt

# Visualizing the data in DataFrame df

plt.figure(figsize=[14, 7])
col_map = plt.get_cmap('tab20')

# Creating a bar chart from the DataFrame df
plt.bar(df.Players, df.Goals, width=0.5, color=col_map.colors, 
        edgecolor='maroon', linewidth=2)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()


# Visualizing the data in DataFrame df2 where category data is set as index

plt.figure(figsize=[14, 7])
col_map = plt.get_cmap('Paired')

# Creating a bar chart from the DataFrame df2
plt.bar(df2.index, df2.Goals, width=0.5, color=col_map.colors, 
        edgecolor='olive', linewidth=3)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()
Matplotlib plot bar chart from dataframe
Matplotlib plot bar chart from dataframe

Read: What is matplotlib inline

Matplotlib plot bar chart with values

You can add values to the bar chat by annotating the text (value) to the chart/graph by using the matplotlib.pyplot.annotate() function with two compulsory arguments, text and the x-y positions of the text on the graph.

Example :

# Importing the required libraries

import numpy as np
import pandas as pd

# Preparing the data to plot
players = ['Cristiano Ronaldo', 'Pele', 'Romario', 'Lionel Messi', 
           'Ferenc Puskas', 'Josef Bican', 'Jimmy Jones', 
           'Gerd Muller', 'Eusebio', 'Joe Bambrick'] 
goals = [783, 765, 753, 748, 729, 720, 647, 634, 622, 616]

# Creating a DataFrame from a dictionary
df = pd.DataFrame({'Players': players, 'Goals': goals})
print('type: '+str(type(df)), 'size: '+str(np.shape(df)), df, sep='\n')
Python create dataframe from a dictionary
Python create dataframe from a dictionary
# Importing the required libraries
from matplotlib import pyplot as plt

# Visualizing the data in DataFrame df with values on the bars

plt.figure(figsize=[14, 7])
col_map = plt.get_cmap('tab20')

# Creating a bar chart from the DataFrame df
pl = plt.bar(df.Players, df.Goals, width=0.5, color=col_map.colors, 
        edgecolor='maroon', linewidth=3)

for bar in pl:
    plt.annotate(bar.get_height(), 
                 xy=(bar.get_x()+0.07, bar.get_height()+10), 
                     fontsize=15)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show()
Matplotlib plot bar chart with values
Matplotlib plot bar chart with values

Read: Matplotlib log log plot

Matplotlib plot bar chart with dates

You can plot a bar chart from time-series data using matplotlib in python.

First, you have to prepare time-series data by converting the Date column into the datestamp type. Then set that date column as the index of the DataFrame. Now, select the data to be visualized and then plot the bar graph for that data with time.

Example :

# Importing the required libraries
import pandas as pd

# Importing the dataset using the pandas into Dataframe
sales_df = pd.read_csv('./Data/Sales_records.csv')
print(sales_df.head(), end='\n\n')

# Converting the Date column into the datestamp type
sales_df['Date'] = pd.to_datetime(sales_df['Date'])

# Sorting the data in ascending order by the date
sales_df = sales_df.sort_values(by='Date')

# Now, setting the Date column as the index of the dataframe
sales_df.set_index('Date', inplace=True)

# Print the new dataframe and its summary
print(sales_df.head(), sales_df.describe(), sep='\n\n')

# Subset dataframe by year 2017
sales2017 = sales_df['2017']
print(sales2017)
Python prepare time series dataframe
Python prepare time series dataframe
# Importing the required libraries
from matplotlib import pyplot as plt

# Visualizing the Total Revenue in Time series DataFrame sales2017
plt.figure(figsize=[15, 9])
col_map = plt.get_cmap('Paired')

# Plotting total revenue throughout 2017 sales in bar chart
pl = plt.bar(sales2017.index, sales2017['Total Revenue'], width=5, 
             color=col_map.colors, edgecolor='maroon', linewidth=2)

# Annotating the heights of the bars at the top of the bars
for bar in pl:
    plt.annotate(bar.get_height(), 
                 xy=(bar.get_x()-4, bar.get_height()+100000), 
                     fontsize=15)

plt.title('Total Revenue generated in 2017', fontsize=20)
plt.xlabel('Date', fontsize=20)
plt.ylabel('Total Revenue', fontsize=20)
plt.xticks(fontsize=14)
plt.yticks(fontsize=18)

plt.show()
Matplotlib plot bar chart with dates
Matplotlib plot bar chart with dates

Read: modulenotfounderror: no module named ‘matplotlib’

Matplotlib plot bar chart with error bars

You can plot a bar chart with error bars using the matplotlib in python by specifying the value for the parameter yerr/xerr in the matplotlib.pyplot.bar() function. You can also specify the color of the error bars by giving the color name as the value to the parameter ecolor, and the capsize of the error bars.

Example :

# Importing the required libraries


import pandas as pd

# Importing the dataset using the pandas into Dataframe
sales_df = pd.read_csv('./Data/Sales_records.csv')
print(sales_df.head(), end='\n\n')

# Converting the Date column into the datestamp type
sales_df['Date'] = pd.to_datetime(sales_df['Date'])

# Sorting the data in ascending order by the date
sales_df = sales_df.sort_values(by='Date')

# Now, setting the Date column as the index of the dataframe
sales_df.set_index('Date', inplace=True)

# Subset dataframe by year 2017
sales2017 = sales_df['2017']
print(sales2017, end='\n\n')

x = sales2017.columns.values
heights = sales2017.mean().values
errors = sales2017.std().values

print('categories: '+str(x), 'heights: '+str(heights), 'errors: '+str(errors), sep='\n')
Python create subset of time series dataframe
Python prepare data from a time series dataframe
# Importing the required libraries
from matplotlib import pyplot as plt

# Visualizing the Total Revenue in DataFrame sales2017 with error bars
plt.figure(figsize=[15, 9])
plt.grid(axis='y', color = 'olive', linestyle = '--')
col_map = plt.get_cmap('tab20b')

# Plotting total revenue throughout 2017 sales in bar chart with error bars
plt.bar(x, heights, width=0.5, color=col_map.colors, edgecolor='y', 
        linewidth=3, yerr=errors, ecolor='k', capsize=10)

plt.title('Total sales description of 2017', fontsize=20)
plt.ylabel('Amount', fontsize=20)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)

plt.show()
Matplotlib plot bar chart with error bars
Matplotlib plot bar chart with error bars

Matplotlib plot multiple bar graphs

When you have data with some subcategories for each category then you can visualize this data by plotting multiple bars graphs in the same chart/figure, where you can plot the bars (representing different subcategories) of the same category side by side for all the categories.

Example :

# Importing the required libraries

import numpy as np
import pandas as pd

# Preparing the data to plot
authors = ['Millisent Danielut', 'Deborah Tinn', 'Brendin Bracer',
           'Aurel Newvell']
python = [15, 21, 9, 25]
postgreSQL = [7, 5, 24, 11]
mongodb = [23, 17, 21, 15]

# Creating a DataFrame from a dictionary
blogs = pd.DataFrame({'Authors': authors, 'Python': python, 
                      'PostgreSQL': postgreSQL, 'Mongodb': mongodb})
print('type: '+str(type(blogs)), 'size: '+str(np.shape(blogs)), blogs, sep='\n')
Python create dataframe from lists
Python create dataframe from lists
# Importing the required libraries
from matplotlib import pyplot as plt
import numpy as np

# Visualizing the data with multiple bar chart
plt.figure(figsize=[15, 9])

# Set the width of the bars
wd = 0.3
x_pos = np.arange(1, 2*len(blogs), 2)

# Plotting the multiple bar graphs on the same figure
plt.bar(x_pos, blogs.Python, color='r', width=wd, edgecolor='k',
        label='Python')
plt.bar(x_pos+wd, blogs.PostgreSQL, color='y', width=wd, edgecolor='k', 
        label='PostgeSQL')
plt.bar(x_pos+(wd*2), blogs.Mongodb, color='c', width=wd, 
        edgecolor='k', label='Mongodb')

# Add xticks
plt.xticks(x_pos+wd, blogs.Authors.values, fontsize=15)
plt.yticks(fontsize=15)
plt.title('The blogs posted by Authors', fontsize=20)
plt.xlabel('Authors', fontsize=17)
plt.ylabel('Blogs', fontsize=17)

plt.legend(loc='upper center', fontsize=15)
plt.show()
Matplotlib plot multiple bar graphs
Matplotlib plot multiple bar graphs

Read: Matplotlib best fit line

Matplotlib plot stacked bar chart

When you have data with some subcategories for each category then you can also visualize this data by plotting multiple bars graphs in the same chart/figure, where you can plot the bars (representing different subcategories) of the same category one upon another for all the categories. This is known as the stacked bar chart, as it is like stacking the subcategories for each category.

# Importing the required libraries

import numpy as np
import pandas as pd

# Preparing the data to plot
authors = ['Millisent Danielut', 'Deborah Tinn', 'Brendin Bracer',
           'Aurel Newvell']
python = [15, 21, 9, 25]
postgreSQL = [7, 5, 24, 11]
mongodb = [23, 17, 21, 15]

# Creating a DataFrame from a dictionary
blogs = pd.DataFrame({'Authors': authors, 'Python': python, 
                      'PostgreSQL': postgreSQL, 'Mongodb': mongodb})
print('type: '+str(type(blogs)), 'size: '+str(np.shape(blogs)), blogs, sep='\n')
Python prepare dataframe for the stacked bar chart
Python prepare dataframe for the stacked bar chart
# Importing the required libraries
from matplotlib import pyplot as plt
import numpy as np

# Visualizing the data with stacked bar chart
plt.figure(figsize=[15, 9])

# Set the width of the bars
wd = 0.4
x_pos = np.arange(len(blogs))

# Plotting the multiple bar graphs on top on other
plt.bar(x_pos, blogs.Python, color='r', width=wd, label='Python')
plt.bar(x_pos, blogs.PostgreSQL, color='y', width=wd, label='PostgeSQL', 
       bottom=blogs.Python)
plt.bar(x_pos, blogs.Mongodb, color='c', width=wd, label='Mongodb', 
       bottom=blogs.Python+blogs.PostgreSQL)

# Add xticks
plt.xticks(x_pos, blogs.Authors.values, fontsize=15)
plt.yticks(fontsize=15)
plt.title('The blogs posted by Authors', fontsize=20)
plt.xlabel('Authors', fontsize=17)
plt.ylabel('Blogs', fontsize=17)

plt.legend(loc='upper left', fontsize=15)
plt.show()
Matplotlib plot stacked bar chart
Matplotlib plot stacked bar chart

Read: Matplotlib subplot tutorial

Matplotlib plot horizontal bar chart

You can also plot a horizontal bar chart by using matplotlib.pyplot.barh() function in matplotlib python. All the parameters in it are as same as in matplotlib.pyplot.bar() function, except the height and the width. Here, the positional argument width represents the values of the categories (bars), and the height represents the thickness of the horizontal bars in the bar chart.

You can use horizontal bar charts where the number of categories (bars) is large, and a vertical bar chart is unable to present it conveniently.

Example :

# Importing the required libraries
from matplotlib import pyplot as plt

# Preparing the data to plot
players = ['Cristiano Ronaldo', 'Pele', 'Romario', 'Lionel Messi', 
           'Ferenc Puskas', 'Josef Bican', 'Jimmy Jones', 'Gerd Muller',
           'Eusebio', 'Joe Bambrick'] 
goals = [783, 765, 753, 748, 729, 720, 647, 634, 622, 616]


plt.figure(figsize=[7, 7])
col_map = plt.get_cmap('Paired')

# Plotting a simple horizontal bar chart
plt.barh(players, goals, height=0.6, color=col_map.colors, 
         edgecolor='k', linewidth=2)

plt.title('Top 10 football goal scorers of all time', fontsize=15)
plt.xlabel('Players', fontsize=15)
plt.ylabel('Goals', fontsize=15)
plt.show() simple
Matplotlib plot horizontal bar chart
Matplotlib plot horizontal bar chart

In this Python tutorial, we have discussed, How to plot a bar chart using matplotlib in Python, and we have also covered the following topics:

  • Matplotlib plot bar chart
  • Matplotlib plot bar chart size
  • Matplotlib plot bar chart with different colors
  • Matplotlib plot bar chart from dict
  • Matplotlib plot bar chart from dataframe
  • Matplotlib plot bar chart with values
  • Matplotlib plot bar chart with dates
  • Matplotlib plot bar chart with error bars
  • Matplotlib plot multiple bar graphs
  • Matplotlib plot stacked bar chart
  • Matplotlib plot horizontal bar chart