How to create visualizations in Python

One of the most important things you can do with Python (as far as data processing goes) is creating visualizations with your data. First off, make sure you have Python installed on your machine, if not please read this guide that I made, next up make sure you have “pip” installed on your machine, if not please read this guide that I also made. Awesome, we’re ready to start!

Data Setup

At this point let’s go ahead and install the packages that we need. Let’s go ahead and install this package with one of the following commands:

#FOR PYTHON 2 USE THIS
pip install matplotlib
#FOR PYTHON 3 USE THISpip3 install matplotlib#IF YOU'RE USING ANACONDA USE THISconda install matplotlib

Awesome, now that we have this package installed, be sure to have the pandas package installed as well, install this package by using the following command:

#FOR PYTHON 2 USE THIS
pip install pandas
#FOR PYTHON 3 USE THISpip3 install pandas#IF YOU'RE USING ANACONDA USE THISconda install pandas

We also need the numpy package for this case, unless you’re bringing in your own dataset, here is how you install this package:

#FOR PYTHON 2 USE THIS
pip install numpy
#FOR PYTHON 3 USE THISpip3 install numpy#IF YOU'RE USING ANACONDA USE THISconda install numpy

Awesome, let’s go ahead and create our dataset, like I said before we can use a different dataset by importing through pandas, if not let’s create our own with the following code:

import pandas as pd
import matplotlib
import numpy as np
data = pd.DataFrame(np.random.rand(100, 4), columns=['One','Two','Three','Four'])

Awesome, our dataset will look something like this:

Next up, let’s go ahead and plot these points! We start off by taking the name of our dataframe (which is data in this example) and add the matplotlib function “.plot.bar”, this is the code to do so:

data.plot.bar()

Awesome! This is our output:

We can change so many parameters within our bar graph, here is a website that shows a bunch of different combinations you can use with matplotlib.

Next up, let’s say we wanted to change the color of this bar graph, let’s start off by rewriting our code to use the color command:

data.plot.bar(color = "green")

our new output should look like this:

Now let’s say we don’t want a bar graph, instead we wanted a graph that covers the whole area, let’s do so by change the “bar” to “area”:

data.plot.area(color = “green”)

This is the output:

Congrats! You’ve now created some visualizations within Python! Matplotlib is an extremely functional tool that isn’t really hard to use, enjoy creating visualizations!

Data Scientist / Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store