Thursday, May 9, 2019

Pandas - 33 (Data Visualization- Adding Elements to the Chart)

Usually we represent the data using lines or markers and assign the range of values using two axes. But in certain cases this may not be enough. There are many other elements that can be added to a chart in order to enrich it with additional information. Let's discuss a few elements-

Adding text to a chart

The text elements that can be added to a chart are title and axis labels. In the previous post we've already seen how we can add the title to a chart with the title() function. To add the axis labels we can use the xlabel() and ylabel() functions which take as an argument a string, which will be the shown text. See the following program:

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

plt.axis([0,5,0,20])
plt.title('Title goes here')
plt.xlabel('Counting')
plt.ylabel('Square values')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()



The output of the program is shown below:


There are certain pre-defined keywords using which we can change the characteristics of the text. For example, we can modify the title by changing the font and increasing the size of the characters.
We can also modify the color of the axis labels to accentuate the title of the plot. Let's modify our program and use the keywords:

plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()


Now our output of the program changes to:

As seen in the output, the font type,size and color of the title text has been changed. The xlabel text color is now blue and ylabel text color is green.

It is also possible to add text to any position within a chart by using the text().

text(x,y,s, fontdict=None, **kwargs)

The first two arguments are the coordinates of the location where we want to place the text. s is the string of text to be added, and fontdict (optional) is the font that we want to use. Finally, we can add the keywords.

Let's add a label to each point of the plot shown in the previous output. As the first two arguments to the text() function are the coordinates of the graph, we have to use the coordinates of the four points of the plot shifted slightly on the y-axis. See the following program:

plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')

plt.show()

The output of the program is shown below and we can see that now each point of the plot has a label:

Integrate LaTeX expressions 

matplotlib offers the possibility to integrate LaTeX expressions which allows us to insert mathematical expressions within the chart. This is done by adding a LaTeX expression to the text, enclosing it between two $ characters.

The interpreter will recognize them as LaTeX expressions and convert them to the corresponding graphic, which can be a mathematical expression, a formula, mathematical characters, or just Greek letters. We have to precede the string containing LaTeX expressions with an r, which indicates raw text, in order to avoid unintended escape sequences. In order to further enrich the text to be shown in the plot, use the keywords, add the formula describing the trend followed by the point of the plot and enclose it in a colored bounding box. See the following program:

plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')

plt.text(1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow','alpha':0.2})

 
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()


The output of the program is shown below:


Adding a Grid element

A grid element is necessary in order to better understand the position occupied by each point on the chart. Adding a grid to a chart is done by adding the grid() function passing True as an argument as shown in the following program which adds a grid to our previous program:

plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')

plt.text(1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow','alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.show()


The output of the program is shown below:

Adding a grid makes it easier to read the values of the data points represented on a chart.

Adding a Legend

pyplot provides a specific function for adding a legend object: legend(). We add a legend to our chart with the legend() function and a string indicating the words with which we want the series to be shown. In the following program, we assign the First series name to the input data array:

plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')

plt.text(1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow','alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.legend(['First series'])
plt.show()


The output of the program is shown below and we can see a legend is added in the upper-right corner by default:

To change the default behavior we can add a few kwargs. For example, the position occupied by the legend is set by assigning numbers from 0 to 10 to the loc kwarg. Each of these numbers characterizes one of the corners of the chart. A value of 1 is the default, that is, the upper-right corner. In the following program we will move the legend in the upper-left corner so it will not overlap with the points represented in the plot:

plt.axis([0,5,0,20])
plt.title('Title goes here',fontsize=20,fontname='Times New Roman',color='red')
plt.xlabel('Counting',color='blue')
plt.ylabel('Square values',color='green')
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.text(1,1.5,'First')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Fourth')

plt.text(1.1,12,r'$y = x^2$',fontsize=20,bbox={'facecolor':'yellow','alpha':0.2})
plt.grid(True)
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.plot([1,2,3,4],[0.8,3.5,8,15],'g^')
plt.plot([1,2,3,4],[0.5,2.5,4,12],'b*')
plt.legend(['First series','Second series','Third series'],loc=2)

plt.show() 


In the above program, the same plot shows more series simultaneously. Each series in the chart is characterized by a specific color and a specific marker. Thus each series will be characterized by a call to the plot() function and the order in which they are defined will correspond to the order of the text labels passed as an argument to the legend() function. The output of the program is shown below:



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