Matplotlib Visualisation Fundamentals in Python
Matplotlib is a popular data visualization tool for Python, By far visualization is one of the most important tools that every Data Scientist will need. it is one of the easiest and most efficient ways to share an idea/convey findings to people who are both from a technical and non-technical background, and for this very reason, it is so important, the ability to convey to a wide and varied audience without any form of boundaries.
There are many different types of visualization that you will use for various situations and contexts, the good news is that Matplotlib has almost any and all the visualization you will need.
In this blog, I will be showing some of the fundamentals of Matplotlb as well as the code implementation alongside it.
firstly like any library in Python, you must import it
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib inline is known as a magic command and this will ensure that matplotlib plots into the output of the cells.
Anatomy of a figure in matplotlib:
This will be helpful when you are dealing with multipled graphs and need to navigate and call specific parts, such as changing colors or adding things to one axe and not the others.
Plotting a figure with a single Axes
fig, ax = plt.subplots()
you can also change the size of your plot using figsize=()
fig, ax = plt.subplots(figsize=(8, 4))
This makes our plot 8 inches in width and 4 inches in length, by default matplotlib will use a 6x4 size plot.
and also just to show clearly the different parts of this subplot lets change the colors of each segment
fig.set_facecolor(‘skyblue’)
ax.set_facecolor(‘sandybrown’)
here you can see what constitutes the figure part and where the figure and the Axe part are
Calling certain methods(get_ and set_ methods)
get_ method which will look at our axis and get that information we requested
set_ method will set something to a feature of our axis
you are able to interact with your axis in many different ways and change things to fit your need. a few of these properties which you will interact with are:
title
ax.set_title(‘My Matplotlib Graph’)
ax.get_title()
output:
‘My First Matplotlib Graph’
you can see the interactions that the get_ and set_ methods have now, they are very similar with all of the different properties you can interact with on matplotlib, one will set/assign the other one will get/collect that information
xlabel/ylabel
by default, there are no xlabel/ylabel assigned to your axis.
ax.set_xlabel(‘X-Axis’)
ax.set_ylabel(‘Y-Axis’)
xlim/ylim
by default, the xlim and ylim are set from 0–1
ax.set_xlim(-10, 50)
ax.set_ylim(-1, 20)
Here we change the x and y lim from 0–1 to custom ones which are reflected on the Axis now.
setting the X and Y axis limiters are especially important when dealing with outliers, in those cases assigning the limits of both axes may allow your visualization to look cleaner.
xticks/yticks
usually, ticks for both axes are set to 1, meaning that a new tick is placed every 1 unit away from the next. Matplotlib will choose reasonable values for the data that is being plotted.
ax.set_xticks([5, 17, 22])
xticklabels/yticklabels
this will allow you to assign any tick a new variable name, by default they are assigned to the numeric location.
ax.set_xticklabels([‘dog’, ‘cat’, ‘snake’])
this will be assigned on the 3 ticks I have from above, in this case:
5 → dog
17 → cat
22 → snake
Again a very handy tool if you want to emphasize findings and when showing results.
I hope this was helpful in showing the basic layout of matplotlib graphs and giving you an under the hood view of it.
Matplotlib: Python plotting — Matplotlib 3.2.1 documentation
Python Plotting With Matplotlib (Guide) — Real Python
Learn Python, Data Science & Machine Learning with expert instruction
Matplotlib Tutorial: Learn basics of Python’s powerful Plotting library
Learn to visualize your data using Python’s Matplotlib library.towardsdatascience.com