Plotting is essential for understanding science and engineering. Python is an open source easy to learn and understand high level language. If one wishes to use python as programming language for scientific and computing language, then tools for plotting and visualization of data is necessary.

Matplotlib is the answer for plotting graphics by the python community. It bears similarity with matlab graphical tool. Matplotlib is a python module/package that contains classes and functions for plotting python data and functions. Matplotlib is open source and free to use.

To use Matplotlib, you have to first install python and numpy. While python is the basic language construct, numpy is the package/module that manipulates the python data types easily as array. In other words, numpy is the array package.

Within matplotlib there are two well known modules- pyplot and pylab. To use any of these we include them into the python file as matplotlib.pyplot or matplotlib.pylab. They define graphical interfaces such as figure, axes, title etc as objects. The difference between is that- pyplot is used for basic plotting, pylab is used for advanced purposes. pyplot has objects and functions for displaying basic graphical views and pylab has more advanced objects and functions for displaying graphics such as interactive graphical views.

Basic Plotting with pyplot

Some examples for displaying data using pyplot is as follows.

First we plot a number "a" using pyplot as follows:

import numpy as np

import matplotlib.pyplot as plt

a = np.arrange(10)

plt.plot(a)

plt.show()

In the above program code, we imported numpy and matplotlib.pyplot modules with abbreviated names np and plt for code writing conviniences.

The number "a" to be plotted is produced using the arrange function of the numpy module. Then we used the plot() and consequently show() functions of plt or matplotlib.pyplot module to first plot and then display the plot to the users.

The figure displayed is shown below:

In the above example, we passed only one argument in the plot() function. plot() functions takes one argument optionally but can take more than then one parameters. If we for example wish to provide the x axis and y axis values we can do so as illustrated by the code below.

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0,100)

y = x**2

plt.plot(x,y)

plt.show()

Plotting more than functions or data on the same figure is useful for comparison purpose. This can be done by simple extension of the above code.

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0,100)

y1 = x**2

y2 = x**2.2

plt.plot(x,y1)

plt.plot(x,y2)

plt.show()

But graph plots without any information about the graph looks incomplete. Users or customers should know what the x-axis and y-axis represents. They should have titles, legends and if there are more than one data plot on the same figure, they should have different markers, colors and widths for readily identification.

If you have used matlab these features are available in matlab. Python via matplotlib also have all these features. These are as follows-

One example program is as follows-

from matplotlib import pylab as pl

import numpy as np

pl.figure(figsize=(9,7), dpi=100)

pl.subplot(1,1,1)

x = np.linspace(-np.pi*2, np.pi*2, 10e4, endpoint=True)

y1, y2 = np.sin(x), np.sin(2*x)

pl.plot(x, y1, color = "blue", linewidth = 1.0, linestyle = "-", label = "$sin(x)$")

pl.plot(x, y2, color = "red", linewidth = 1.0, linestyle = "-", label = "$sin(2x)$")

pl.xlim(-np.pi*2, np.pi*2)

pl.xticks(-np.linspace(-2.5*np.pi,2.5*np.pi,9,endpoint=True))

pl.ylim(-1.2,1.2)

pl.yticks(np.linspace(-1,1,5,endpoint=True))

pl.title('$sine(x)$ and $sin(2x)$')

pl.grid(True)

pl.legend()

pl.show()

Running this python program we get the following figure:

Matplotlib is the answer for plotting graphics by the python community. It bears similarity with matlab graphical tool. Matplotlib is a python module/package that contains classes and functions for plotting python data and functions. Matplotlib is open source and free to use.

To use Matplotlib, you have to first install python and numpy. While python is the basic language construct, numpy is the package/module that manipulates the python data types easily as array. In other words, numpy is the array package.

Within matplotlib there are two well known modules- pyplot and pylab. To use any of these we include them into the python file as matplotlib.pyplot or matplotlib.pylab. They define graphical interfaces such as figure, axes, title etc as objects. The difference between is that- pyplot is used for basic plotting, pylab is used for advanced purposes. pyplot has objects and functions for displaying basic graphical views and pylab has more advanced objects and functions for displaying graphics such as interactive graphical views.

Basic Plotting with pyplot

Some examples for displaying data using pyplot is as follows.

First we plot a number "a" using pyplot as follows:

import numpy as np

import matplotlib.pyplot as plt

a = np.arrange(10)

plt.plot(a)

plt.show()

In the above program code, we imported numpy and matplotlib.pyplot modules with abbreviated names np and plt for code writing conviniences.

The number "a" to be plotted is produced using the arrange function of the numpy module. Then we used the plot() and consequently show() functions of plt or matplotlib.pyplot module to first plot and then display the plot to the users.

The figure displayed is shown below:

In the above example, we passed only one argument in the plot() function. plot() functions takes one argument optionally but can take more than then one parameters. If we for example wish to provide the x axis and y axis values we can do so as illustrated by the code below.

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0,100)

y = x**2

plt.plot(x,y)

plt.show()

Plotting more than functions or data on the same figure is useful for comparison purpose. This can be done by simple extension of the above code.

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(0,100)

y1 = x**2

y2 = x**2.2

plt.plot(x,y1)

plt.plot(x,y2)

plt.show()

But graph plots without any information about the graph looks incomplete. Users or customers should know what the x-axis and y-axis represents. They should have titles, legends and if there are more than one data plot on the same figure, they should have different markers, colors and widths for readily identification.

If you have used matlab these features are available in matlab. Python via matplotlib also have all these features. These are as follows-

- title( )
- xlabel( ) and ylabel( )
- xticks( ) and yticks( )
- grid( )
- legend( )
- subplot( )

One example program is as follows-

from matplotlib import pylab as pl

import numpy as np

pl.figure(figsize=(9,7), dpi=100)

pl.subplot(1,1,1)

x = np.linspace(-np.pi*2, np.pi*2, 10e4, endpoint=True)

y1, y2 = np.sin(x), np.sin(2*x)

pl.plot(x, y1, color = "blue", linewidth = 1.0, linestyle = "-", label = "$sin(x)$")

pl.plot(x, y2, color = "red", linewidth = 1.0, linestyle = "-", label = "$sin(2x)$")

pl.xlim(-np.pi*2, np.pi*2)

pl.xticks(-np.linspace(-2.5*np.pi,2.5*np.pi,9,endpoint=True))

pl.ylim(-1.2,1.2)

pl.yticks(np.linspace(-1,1,5,endpoint=True))

pl.title('$sine(x)$ and $sin(2x)$')

pl.grid(True)

pl.legend()

pl.show()

Running this python program we get the following figure:

## 0 comments:

## Post a Comment