Create Multiple Bar Charts in Python using Matplotlib and Pandas

In this Python visualization tutorial you’ll learn how to create and save as a file multiple bar charts in Python using Matplotlib and Pandas. We’ll easily read in a .csv file to a Pandas dataframe and then let Matplotlib perform the visualization. As a bonus you’ll also learn how to save the plot as a file.

The key to making two plots work is the creation of two axes that will hold the respective bar chart subplots.

# define the figure container and the two plot axes
fig = plt.figure(figsize=(20,5))

# add subplots to the figure (build a 1x2 grid and place chart in the first or second section)
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

Understanding the subplot nomenclature is essential. Adding axes to the figure as part of a subplot arrangement is simple with the fig.add_subplot() call. In this arrangement the first digit is the number of rows, the second represents the number of columns, and the third is the index of the subplot (where we want to place our visualization).

Of course you need to watch the video to see how all of the code comes together.

Also, keep this Matplotlib style sheet reference handy for changing up the style on your visual.

NBA Blocks Assists

As always, If you find this type of instruction valuable make sure to subscribe to my Youtube channel.

All views and opinions are solely my own and do NOT necessarily reflect those my employer.

See the following links for additional background:

https://matplotlib.org/3.1.0/gallery/subplots_axes_and_figures/subplots_demo.html

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.add_subplot

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s