Matplotlib & Pandas – Plot CSV Data Example

5Shares
Reading Time: 3 minutes

Create Visuals for your CSV data using Matplotlib and Pandas

This tutorial will teach you how to create a chart with a csv file’s data using matplotlib and pandas. For this example, we’re going to use Think Tree IT’s September visitor analytics data. We will create a chart using the country visited from as the x axis and new users for the y axis. Here is an example of our output.

matplotlib chart

 

What is Matplotlib?

Matplotlib is a plotting library in Python, that is capable of creating many kinds of charts. It can create line graphs, scatter plots, bar charts, pie charts, stack plots, 3D graphs, and more.

Feel free to check you Matplotlib’s website for more information and examples. Install the matplotlib package using the code below.

python -m pip install matplotlib

What is Pandas?

Pandas is a Python package that provides fast, flexible, and expressive data structures designed to make working with relational or labeled data easy and intuitive. The two main data structures pandas works with are Series – 1 Dimensional and DataFrame – 2 dimensional. For our example we will be working with 2 dimensional data.

Check out pandas homepage for more information and documentation. Make sure to have the pandas package installed.

python -m pip install pandas

 



The Code

Download this Matplotlib Example script on Github

 

How It Works

First thing we do is import the matplotlib.pyplot and pandas library. We give them an alias to make our code easier to read.

Then we create the headers variable to store the csv column names we’re going to use for the x and y axis.

Next we create a variable named df, which is short for DataFrame. In this variable we use pandas read_csv function. This function reads CSV file into a DataFrame. We set the delimiter as a comma since it’s a csv and then set the names as our header variable for a list of our column names to use.

The x variable will be the Country, which is also the first column in the csv and the y variable will be the number of visitors.

Now we create a bar chart using plt.bar(x, y) and use our x and y variables. We give our new chart label names and a title to make it easier to read. We end our code off with plt.show() to display the chart. If you want to use a line charts, use plt.plot(x, y) instead.

Take a look a matplotlibs example page to see what else you can create.

Thanks for reading. Feel free to leave a comment below with any questions or a function you were able to create.

Turn into a Python Pro!

Come on over and take a look at “Automate the Boring Stuff with Python: Practical Programming for Total Beginners“. If you’ve ever spent hours renaming files or updating hundreds of spreadsheet cells, you know how tedious tasks like these can be. But what if you could have your computer do them for you?

In Automate the Boring Stuff with Python, you’ll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required. Once you’ve mastered the basics of programming, you’ll create Python programs that effortlessly perform useful and impressive feats of automation to:
–Search for text in a file or across multiple files
–Create, update, move, and rename files and folders
–Search the Web and download online content
–Update and format data in Excel spreadsheets of any size
–Split, merge, watermark, and encrypt PDFs
–Send reminder emails and text notifications
–Fill out online forms

This post “Matplotlib & Pandas – Plot CSV Data Example” is located under the Programming Category

Learn how to Create a Website status monitor in Python!

5Shares

admin

Leave a Reply

Your email address will not be published.