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 ?¶
- It can draw
plot, histograms, power spectra, bar charts, errorcharts, scatterplots,etc - Sample Plots
- Thumbnail gallery
How it is installed ?¶
- It can be installed as its own package
- With third party distribution
- From source, it can be built
- 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¶
- The work is done on many levels from
general to specific. - Once can visiulize data easily as well as control necessary high and low level detail.
- It is all done through object library so the more specific can be accused its less specific object.
-
matplotlibis said to be thestate-machine environmentprovide bymatplotlib.pyplotmodule.
Pyplot is like a matlab environment, so should not be difficult. The first level in object hirararcy is the
pyplotlibrary. 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, matplotprovides the smae high level abstraction.
Note: Matplotlib figures and plots works with
numpyarrays as input. Other librariesarrly-likeobject such aspandasnp.matrix may or may not work. It is better that they can be converted tonp.arrayobject.
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()
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]:
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()
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()
In [ ]:
Comments