Introduction to Matplotlib in Python
A quick guide to getting up and running with data visualization techniques in the matplotlib library
6 July, 2022
5
5
0
Contributors
What is data visualization?
Is the practice of visualizing data in graphs, icons, presentations, and more. It is most commonly used to translate complex data into digestible insights for a non-technical audience.
This is a great book to use as a starting point if you are new to data visualization — Storytelling with Data
What is Python?
Python is a programming language that lets you work more quickly and integrate your systems more effectively.
Python is one of the most popular programming languages and has a wide variety of useful applications. For this article, I will assume you have basic working knowledge in the Python language.
If you do not here are some great resources to get started,
2.
What is Matplotlib?
There are thousands of libraries in Python, and Matplotlib is one of the most powerful tools for data visualization in Python.
Matplotlib tries to make easy things easy and hard things possible. You can generate plots, histograms, power spectra, bar charts, errorcharts, scatterplots, etc., with just a few lines of code.
Let’s get started!
Importing the library
To get matplotlib up and running in our environment, we need to import it.
It is common practice to import matplotlib under the alias plt — that way, we have to type less code to reference it further down the line.
Whenever you plot with matplotlib, the two main code lines should be,
1.
Type of graph — this is where you define a bar chart, line chart, etc.
2.
Show the graph — this is to display the graph
Line Graphs
Bar graphs
When using a bar graph, the change in code will be from plt.plot() to plot.bar() changes it into a bar chart. If you look inside the body of the code, I also added an argument color - this helps us quickly customize the color of the graph.
We can also flip the bar graph horizontally with the following,
Notice the change in the color argument
Scatter Plots
Can you see the pattern? Now the code changed from plt.bar() to plt.scatter(). I also added the s argument. The s stands for size, and it allows us to control how big we want the points on the graph.
Histograms
Looking at the code snippet, I added two new arguments.
1.
Bins — is an argument specific to a histogram and allows the user to customize how many bins they want.
2.
Alpha — is an argument that displays the level of transparency of the data points.
If I were to adjust both the bins and alpha, I would get something like this,
Overview
We just touched the surface of the power of matplotlib. Once you dive deeper into this subject, you can see how much customizability you can have creating colorful, detailed, and vibrant graphs.
There are a lot more graphs available in the matplotlib library as well as other popular libraries available in python, including seaborn, pandas plot, and plotly. It is worth exploring all the different options and finding which library suits your style of coding and analysis.
Stay tuned — I will be sharing a tutorial about customizing graphs in matplotlib.