Introduction to matplot

Matplotlib

UserGuide

  • It is plotting library for 2D plotting used in academic publising on hardcopy and in interactive environment.
  • It can be used in Python script, in Python and IPython shells, Jupyter notebook, web application servers. These four...

What it can generate ?

How it is installed ?

  1. It can be installed as its own package
  2. With third party distribution
  3. From source, it can be built
  4. Clone the latest repo

Its Dependencies ? on [30 July 2020] Following is taken from documentation

txt

Python (>= 3.6)
FreeType (>= 2.3)
libpng (>= 1.2)
NumPy (>= 1.11)
setuptools
cycler (>= 0.10.0)
dateutil (>= 2.1)
kiwisolver (>= 1.0.0)
pyparsing

- To get the better user interface toolkit, optional can be insalled.

tk (>= 8.3, != 8.6.0 or 8.6.1): for the Tk-based backends;
PyQt4 (>= 4.6) or PySide (>= 1.0.3): for the Qt4-based backends;
PyQt5: for the Qt5-based backends;
PyGObject: for the GTK3-based backends;
wxpython (>= 4): for the WX-based backends;
cairocffi (>= 0.8) or pycairo: for the cairo-based backends;
Tornado: for the WebAgg backend;
For better support of animation output format and image file formats, LaTeX, etc., you can install the following:

ffmpeg/avconv: for saving movies;
ImageMagick: for saving animated gifs;
Pillow (>= 3.4): for a larger selection of image file formats: JPEG, BMP, and TIFF image files;
LaTeX and GhostScript (>=9.0) : for rendering text with LaTeX.

Concepts behind the matplotlib

  1. The work is done on many levels from general to specific.
  2. Once can visiulize data easily as well as control necessary high and low level detail.
  3. It is all done through object library so the more specific can be accused its less specific object.
  4. matplotlib is said to be the state-machine environment provide by matplotlib.pyplot module.

Pyplot is like a matlab environment, so should not be difficult. The first level in object hirararcy is the pyplot library. The user uses this object to draw figures and controls its attributes.

In [ ]:
import matplotlib.pyplot as pyplot
import numpy as np

fig = pyplot.figure()
fig.suptitle('No axes on this figure')

# Draw two by two figures ( that is four boxex)
fig.ax_lst = pyplot.subplots(2,2)
# Draw only one
fig.ax_lst = pyplot.subplots(1,1)
  • The above figure is just a description how easy it is to plat a figure, it is very simple and other software such as r, matplot provides the smae high level abstraction.

Note: Matplotlib figures and plots works with numpy arrays as input. Other libraries arrly-like object such as pandas np.matrix may or may not work. It is better that they can be converted to np.array object.

Using numpy to draw a sin funciton

In [56]:
# run above cells so that librararie are imported, if not, import them again

import matplotlib.pyplot as pyplot
import numpy as np 

x = np.arange(0,10,0.2)
y = np.sin(x)

# creating a figure and set of subplots. pyplot.subplots() creates two object at one time, it implicitly 
# creates a fig object and show a subplot created in ax
fig, ax = pyplot.subplots()

# Though ax points to a subplots objects, it is still empty so fill it
ax.plot(x,y)
pyplot.show()
2020-07-30T17:19:08.451113image/svg+xmlMatplotlib v3.3.0, https://matplotlib.org/
In [59]:
### What happened above ?

# `np.arange`, numpy has number of function that creates an array as shown below.
# The function above creates an arry startgin from 0 and ending to 10 with a difference of 0.2
import numpy;a = numpy.arange(0,10,0.2);a
Out[59]:
array([0. , 0.2, 0.4, 0.6, 0.8, 1. , 1.2, 1.4, 1.6, 1.8, 2. , 2.2, 2.4,
       2.6, 2.8, 3. , 3.2, 3.4, 3.6, 3.8, 4. , 4.2, 4.4, 4.6, 4.8, 5. ,
       5.2, 5.4, 5.6, 5.8, 6. , 6.2, 6.4, 6.6, 6.8, 7. , 7.2, 7.4, 7.6,
       7.8, 8. , 8.2, 8.4, 8.6, 8.8, 9. , 9.2, 9.4, 9.6, 9.8])
In [60]:
# We need to draw something, and we decided to draw the output of `sin` funciton so we saved the outpu in y variable
# The return value of `sin(x)` function that is a tuple and need to be printed using print(y)
b = np.sin(a) #print(y)
In [68]:
# Now we have x that goes from 0 to 10 with a difference of 0.2 that is we are going to plot 
# values of y against x. to plot a figure we need to use matplot.pyplot object. This method provides a subplot object that
# is very convinent to plotting any values using object within fig object 
import matplotlib.pyplot as plt;
fig, ax = plt.subplots()
2020-07-30T17:46:57.614660image/svg+xmlMatplotlib v3.3.0, https://matplotlib.org/
In [75]:
# The above draws only an empty figure, even though `plt.show()` is not even used explicitly. 
# pyplot to plot an object we have used the following
ax.plot(a,b)
pyplot.show()
In [77]:
### The above does not work it has to be done in one go
fig2,fx=plt.subplots()
fx.plot(a,b)
plt.show()
2020-07-30T18:03:06.413486image/svg+xmlMatplotlib v3.3.0, https://matplotlib.org/
In [ ]:
 

Comments