Learning Data Visualizations with Matplotlib on Python
Matplotlib is a library for data visualization with Python. Data Visualization means viewing data in an image form examples pie chart, histogram, bar chart etc. It was created by John Hunter. He created it to try to replicate MatLab’s (another programming language) plotting capabilities in Python. Pros of Matplotlib are:
- Generally easy to get started for simple plots
- Support for custom labels and texts
- Great control of every element in a figure
- High-quality output in many formats
- Very customizable in general
Before you can use Matplotlib you have to install the library through your terminal with the following codes
conda install matplotlib
or pip install matplotlib
to import the library you need to do enter the code shown below
import matplotlib.pyplot as plt
This does not however makes the plots to be displayed to make the plots to be displayed enter the code below
%matplotlib inline
The line above is only for jupyter notebooks, if you are using another editor, you’ll use: plt.show() at the end of all your plotting commands to display or show the figure.
Examples of plots using Matplotlib
Assuming you have a numpy array of x= [ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ] and y = x ** 2
We would be discussing object orineted programming method here as it a more formal approach to creating plots basically is to create figure objects and then just call methods or attributes off of that object. This approach is nicer when dealing with a canvas that has multiple plots on it. For creating plots this how to start
1. Create blank figure (empty canvas)
fig = plt.figure()
2. Add set of axes to figure(left, bottom, width, height (range 0 to 1))
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
3. Plot on that set of axes
axes.plot(x, y, ‘b’)
the Result is shown below
Figure size, aspect ratio and DPI
Matplotlib allows the aspect ratio, DPI and figure size to be specified when the Figure object is created. You can use the figsize
and dpi
keyword arguments.
figsize
is a tuple of the width and height of the figure in inchesdpi
is the dots-per-inch (pixel per inch).
The code is
fig = plt.figure(figsize=(8,4), dpi=100
to Set the label on x or y axis and also the title you can enter the code below
axes.set_xlabel(‘Set X Label’)
axes.set_ylabel(‘Set y Label’)
axes.set_title(‘Set Title’)
Setting colors, linewidths, linetypes
Matplotlib gives lots of options for customizing colors, linewidths, and linetypes.
For line colors with the normal matlab syntax we enter the code in the image below.
We can also define colors by their names or RGB hex codes and optionally provide an alpha value using the color and alpha keyword arguments. Alpha indicates opacity.
Changing Line width and line style
we can use the `linewidth` or `lw` keyword argument to change the line width. The line style can be selected using the `linestyle` or `ls` keyword arguments:
The result is shown below:
-To get more resources you can check the web page for matplotlib http://www.matplotlib.org.