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 of my employer.

Advertisement

One Comment

  1. Hi. I have a df without column name. I cant rename the columns because the column headers are the indeces. How can I plot a bar plot for such a DF?

    Like

    Reply

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 )

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