Skip to content

Fundamental Python Data Science Libraries: A Cheatsheet ~ Matplotlib

Originally published on Hackernoon

If you are a developer and want to integrate data manipulation or science into your product or starting your journey in data science, here are the Python libraries you need to know.

  1. NumPy
  2. Pandas
  3. Matplotlib
  4. Scikit-Learn

The goal of this series is to provide introductions, highlights, and demonstrations of how to use the must-have libraries so you can pick what to explore more in depth.

Matplotlib

This library is the go-to Python visualization package (except for Plotly which is paid)! It allows you to create rich images displaying your data with Python code.

Focus of the Library

This library is extensive, but this article will focus on two objects: the Figure and the Axes.

Installation

Open a command line and type in

Windows: in the past I have found installing NumPy & other scientific packages to be a headache, so I encourage all you Windows users to download Anaconda’s distribution of Python which already comes with all the mathematical and scientific libraries installed.

Details

Matplotlib is split into two main sections: the Pyplot API(visualization functions for fast production) and the Object Oriented API(more flexible and robust).

We will focus on the latter.

Let’s dive in!

Creation

In order to make a visualization, you need to create 2 objects one right after the other. First create a Figure object and then from that, create an Axes object. After that, all visualization details are created by calling methods.

Some things to note about the Figure object:

  • The figsize & dpi parameters are optional
  • figsize is the width and height of the figure in inches
  • dpi: is the dots-per-inch (pixel per inch)

Some things to note about the add_axes method:

  • The position of the axes can only be specified in fractions of the figure size
  • There are many other parameters that you can pass to this method

Plotting

Now we are going to create some simple data, plot it, label the graph, and save it to the same directory as where our code lives.

Here is the resulting image:

Legends

The best way to add a legend is to include the label keyword when you call the plot method on the Axes object (as we saw in the code above). Then you can make a legend and choose its location by calling another method.

Here is the resulting image:

Colors & Lines

You can control features of the lines by passing certain keyword arguments into the plot method. Some of the most commonly used keywords are:

  • color: either passing the name (“b”, “blue”, “r”, “red”, etc) or a hex code (“#1155dd”, “15cc55”)
  • alpha: transparency of the line
  • linewidth
  • linestyle: pattern of the line (‘-’, ‘-.’, ‘:’, ‘steps’)
  • marker: pattern for each data point on the line (‘+’, ‘o’, ‘*’, ‘s’, ‘,’, ‘.’)
  • markersize

Here is the resulting image:

Axes Range & Tick Marks

You can also control the range of the axes and override the tick lines of your graph.

Here is the resulting image:

Subplots

So far we have created a Figure object with only one graph on it. It is possible to create multiple graphs on one Figure all in one go. We can do this using the subplots function.

Here is the resulting image:

I’m providing here a link to download my Matplotlib walkthrough using a Jupyter Notebook!

Never used Jupyter notebooks before? Visit their website here.

Applications

In my last article on pandas, we acquired data on Bitcoin and created a signal for when to buy and trade based on the rolling 30 day average price. We can use our new knowledge in Matplotlib to visualize this data.

You’ll need a Quandlaccount and the python Quandl library.

Code from last time:

New code to visualize bitcoin data:

Here is the resulting image:

That’s Matplotlib! Fast, flexible, and easy visualizations with real data. But what if we wanted to analyze the data with something more sophisticated than a rolling 30 day average? The last library every Python data-oriented programmer needs to know is Scikit-Learn — learn about it in my next article!

Connect with me on Instagram @lauren__glassLinkedIn

Check out my essentials list on Amazon

View this post on Instagram

A post shared by Lauren Glass | לורן גלאס (@lauren__glass) on

Published inUncategorized

Be First to Comment

Leave a Reply

Your email address will not be published.