This tutorial outlines how to perform plotting and data visualization in python using Matplotlib library. The objective of this post is to get you familiar with the basics and advanced plotting functions of the library. It contains several examples which will give you hands-on experience in generating plots in python.
READ MORE »Table of Contents
What is Matplotlib?
It is a powerful python library for creating graphics or charts. It takes care of all of your basic and advanced plotting requirements in Python. It took inspiration fromMATLAB
programming language and provides a similar MATLAB like interface for graphics. The beauty of this library is that it integrates well with pandas package which is used for data manipulation. With the combination of these two libraries, you can easily perform data wrangling along with visualization and get valuable insights out of data. Like ggplot2 library in R, matplotlib library is the grammar of graphics in Python and most used library for charts in Python.
Basics of Matplotlib
First step you need to install and load matplotlib library. It must be already installed if you used Anaconda for setting up Python environment.Install library
If matplotlib is not already installed, you can install it by using the command
pip install matplotlib
Import / Load Library
We will import Matplotlib’s Pyplot module and used alias or short-form as plt
from matplotlib import pyplot as plt
Elements of Graph
Different elements or parts of a standard graph are shown in the image below -
Figure
You can think of the figure as a big graph consisting of multiple sub-plots. Sub-plot can be one or more than one on a figure. In graphics world, it is called 'canvas'.
Axes
You can call them 'sub-plots'.
Axis
It's the same thing (x or y-axis) which you studied in school or college. A standard graph shows the marks on the axis. In matplotlib library, it is called ticks
and text or value in ticks is called ticklabels
.
Basic Plot
x = [1, 2, 3, 4, 5]
y = [5, 7, 3, 8, 4]
plt.bar(x,y)
plt.show()
If you are using Jupyter Notebook, you can submit this command
%matplotlib inline
once to display or show plots automatically without need to enter plt.show()
after generation of each plot.Functions used for different types of plots
The following tables explain different graphs along with functions defined for these graphs in matplotlib library.Type of Plot | Function |
---|---|
line plot (Default) | plt.plot( ) |
vertical bar plots | plt.bar( ) |
horizontal bar plots | plt.barh( ) |
histogram | plt.hist( ) |
boxplot | plt.box( ) |
area plots | plt.area( ) |
scatter plots | plt.scatter( ) |
pie plots | plt.pie( ) |
hexagonal bin plots | plt.hexbin( ) |
from Planet Python
via read more
No comments:
Post a Comment