Wednesday, May 8, 2019

Pandas - 32 (Data Visualization- matplotlib and NumPy)

The matplot library  has its foundation as the NumPy library. We have already seen how to pass lists as arguments, both to represent the data and to set the extremes of the axes. Actually, these lists have been converted internally in NumPy arrays.


Therefore, we can directly enter NumPy arrays as input data. This array of data,which have been processed by pandas, can be directly used with matplotlib without further processing. See the following program:

import matplotlib.pyplot as plt
import math
import numpy as np

t = np.arange(0,2.5,0.1)
y1 = np.sin(math.pi*t)
y2 = np.sin(math.pi*t+math.pi/2)
y3 = np.sin(math.pi*t-math.pi/2)


plt.plot(t,y1,'b*',t,y2,'g^',t,y3,'ys')


plt.show()

plt.plot(t,y1,'b--',t,y2,'g',t,y3,'r-.')

plt.show()



In the above program we are using the sin() function to plot three different trends in the same
plot. To generate points following a sinusoidal trend, we are using the NumPy library. To generate a series of points on the x-axis we are using the arange() function, while for the values on the y-axis we use the map() function to apply the sin() function on all the items of the array (without using a for loop).


The output of the program is shown below which is a plot showing three sinusoidal trends phase-shifted by π / 4 represented by markers:

The plot represents the three different temporal trends with three different colors and markers. To differentiate the three trends with something other than color, we use the pattern composed of different combinations of dots and dashes ( - and . ). This is implemented through the code -

plt.plot(t,y1,'b--',t,y2,'g',t,y3,'r-.')

plt.show()


which gives us the following plot :


Kwargs

The objects that make up a chart have many attributes that characterize them. These attributes are all default values, but can be set through the use of keyword args, often referred as kwargs.

These keywords are passed as arguments to functions. In reference documentation of the various functions of the matplotlib library, we find them referred to as kwargs in the last position. For example the plot() function that you are using in these examples is referred to in the following way.

matplotlib.pyplot.plot(*args, **kwargs)

Let's change the the thickness of the lines for the plot shown in the output of the previous program. See the following code:

plt.plot(t,y1,'b--',t,y2,'g',t,y3,'r-.',linewidth=5.0)

plt.show()


Using the kwarg linewidth=5.0, the thickness of lines in the plot changes as shown in the output below:

The pyplot commands that we used are routed to the display of a single figure. However, matplotlib can be used to manage multiple figures simultaneously, and within each figure, it offers the ability to view different plots defined as subplots. Thus when working with pyplot, we must always keep in mind the concept of the current Figure and current Axes (that is, the plot shown within the figure).

In the following program two subplots are represented in a single figure:

import matplotlib.pyplot as plt
import math
import numpy as np

t = np.arange(0,5,0.1)
y1 = np.sin(2*np.pi*t)
y2 = np.sin(2*np.pi*t)
plt.subplot(211)

plt.plot(t,y1,'b-.')

plt.subplot(212)

plt.plot(t,y2,'r--')

plt.show()




In the above program we used the subplot() function which in addition to subdividing the figure in different drawing areas, is also used to focus the commands on a specific subplot. The argument passed to the subplot() function sets the mode of subdivision and determines which is the current subplot. The current subplot will be the only figure that will be affected by the commands.

The argument of the subplot() function is composed of three integers. The first number defines how many parts the figure is split into vertically. The second number defines how many parts the figure is divided into horizontally. The third issue selects which is the current subplot on which you can direct
commands.

Then we display two sinusoidal trends (sine and cosine) by dividing the canvas vertically in two horizontal subplots.The numbers to pass as an argument are 211 and 212. The output of the program is shown below:
Now we'll divide the figure in two vertical subplots. The numbers to be passed as arguments to the subplot() function are 121 and 122. See the following program:

import matplotlib.pyplot as plt
import math
import numpy as np

t = np.arange(0.,1.,0.05)
y1 = np.sin(2*np.pi*t)
y2 = np.cos(2*np.pi*t)
plt.subplot(121)

plt.plot(t,y1,'b-.')

plt.subplot(122)

plt.plot(t,y2,'r--')

plt.show()


The output of the program is shown below in which the figure has been divided into two vertical subplots:



Here I am ending today’s post. Until we meet again keep practicing and learning Python, as Python is easy to learn!
Share:

0 comments:

Post a Comment