Matplotlib is the defacto plotting library in Python. Even though it is one of many plotting libraries, analysts most likely cut their teeth on Matplotlib. This is because it is one of the oldest and and a lot of the concepts of plotting were based on what happened in Matplotlib.
Once you get the hang of it, plotting on Matplotlib becomes second nature. It is intuitive and plays well with the data. It is flexible enough to accommodate how you want to analyse your data. This could be either as low level Python data objects (lists and dictionaries), Numpy arrays or Pandas Series or Data Frames.
Getting into Matplot lib is easy. Creating a graph can be a simple one line command if you already have the data.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) plt.plot(x, np.sin(x));
But, as the complexity of your analysis grows and you need to customize your graphs more, the learning curve starts to climb.
If you are in the habit of Just In Time (JIT) learning, you soon start to realize that there seem to be multiple ways to solve a single problem. Because of this, internalizing solutions to common problems becomes harder than it should be.
Something that should be understood early and that will save you a lot of time is learn about the two main categories of how to create graphs on Matplotlib.
Matplotlib has two interfaces when creating graphs. Functional and Object oriented. When searching for solutions online, it really helps to know which interface you are using. Internalizing solutions on this basis makes recalling them a lot easier.
MATLAB Style Interface
Matplotlib was originally conceived as a way of writing MATLAB style graphs in Python. The author tried to stay faithful to the same way graphs were originally built. This was closer to a functional programming style.
It is a faster way to get up and running with a graph. And would be a better way go creating simple graphs and can work OK creating multiple graphs as well.
plt.figure() plt.subplot(2,1,1) plt.plot(x, np.sin(x)) plt.subplot(2,1,2) plt.plot(x, np.cos(x));
Each time subplot is called, Matplotlib specifically works on the subplot and any commands will apply to that plot only.
It is easy when you don’t need to go back to previous plots if you want to change styles or add features to them.
It also works well if you are doing analysis on a Jupyter notebook and want to quickly have a view of your data.
The Object Oriented Interface
The OO interface creates objects and you write methods directly on those objects. It is a little longer winded but you know exactly what objects you are dealing with.
fig, ax = plt.subplots(nrows=2) ax.plot(x, np.sin(x)) ax.plot(x, np.cos(x));
The same task in the OO interface gives you a Figure object (which is the container both subplots will live in) and a list of two Axes objects (or subplots). You draw the graphs directly with the Axes objects.
Here you have control over each of the components that make up the graph.
You start to see benefits in this approach as your graphs become more and more complicated. The objects can be passed between other objects in a more organised manner.
This approach also plays well with Pandas. For instance if you want graph what is in a data frame into an Axis in a subplot, Pandas has helpfully built the ability to do this.
When you plot in Pandas, you have the ability to pass the Axis object into which you want the plot to display.
It is worth reading this if you are learning Python plotting. When looking for solutions online, it may be confusing trying to reconcile the different ways of doing the same thing and which one is the better way.
Overall, knowing Matplotlib well is one of the best skills you can have for data analysis. It is one of the best data visualization tools out there.