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

Create a Bar Chart in Python using Matplotlib and Pandas

In this Python visualization tutorial you’ll learn how to create and save as a file a stylish bar chart 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.

I am using the Anaconda Distribution which is a great one stop shop for all your python needs as it is a free and open source distribution of python. I love this option because it gives you the ability to quickly download multiple python packages for analyses and visualizations. As a bonus it includes Jupyter notebooks (which is a web based environment for writing code).

Packages Used

In case you aren’t familiar with Python packages:

  • Pandas is the killer app so to speak for reading, writing and wrangling data.
  • Matplotlib is the visualization plotting library we will use.
  • Pyplot is collection of functions that enables changes to a figure. Think of a figure as a container that can contain multiple plots know as axes. We can plot our bar chart in the figure plotting area and then enhance the plot with labels, etc.
  • Finally OS is a module that enables the use of operating system functionality. We’ll use this package to point our working directory to where our .csv file is located.

Once you follow the instructions in the video, you’ll produce an image like the following which can be saved to a directory location of your choice.

Keep this Matplotlib style sheet reference handy for changing up your visual.

NBA Blocks

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.