Monday, December 30, 2019

Introducing New Elements to a Plot

Charts are supposed to make your data visually appealing. To do this, it is important to ensure you use the correct chart to represent the data you need, because not all charts are suitable for any kind of data. The basic lines and markers will not be sufficient in making the charts appealing. You should think of getting additional elements into the chart for this purpose.

How to add text to a chart

With the title() function, you can introduce an elaborate title into the chart. Beyond that, you should also be able to introduce the axis label. This is done with the xlabel() and ylabel() functions. Remember that when you introduce a new function like the axis label functions, they create an argument within the string of code you are working with. We want to introduce the axis labels to a chart. This is the first step because they help you identify the values that will be assigned to every axis as you plot data. Your illustration should follow the code below:

import matplotlib.pyplot as plt
plt.axis([0,5,0,20])
plt.title('My first plot')
plt.xlabel('Counting')
plt.ylabel('Square values')
plt.plot([1,2,3,4], [1,4,9,16],'ro')
plt.show()


When you run the above program you should have the following plot:

You can perform basic editing for all the text you have entered that describe the plot. Basic editing includes altering the font and font size, colors, or any other tweaks that you might need for the plot to be appealing. Following the example above, we can further tweak the title as follows:

plt.axis([0,5,0,20])
plt.title('My first plot',fontsize=18,fontname='Comic Sans MS')
plt.xlabel('Counting',color='black')
plt.ylabel('Square values',color='black')
plt.plot([1,2,3,4], [1,4,9,16],'ro')
plt.show()


The output should be as shown below:







The Matplotlib functionality allows you to perform more edits to the chart. For example, you can introduce new text into the chart using the text () function,

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


In the function outlined above, the coordinates x and y represent the location of the text you are introducing into the chart. s, represents the string of text you are adding to the chart at the specified location. The fontdict() function represents the font you use for the new text. However, this function is optional. Once you have these figured out, you can then introduce keywords into the code. Let’s
have a look at the example below to illustrate this:

plt.axis([0,5,0,20])
plt.title('My first plot',fontsize=20,fontname='Times New Roman')
plt.xlabel('Counting',color='gray')
plt.ylabel('Square values',color='gray')
plt.text(1,1.4,'First')
plt.text(2,4.4,'Second')
plt.text(3,9.4,'Third')
plt.text(4,16.4,'Fourth')
plt.plot([1,2,3,4], [1,4,9,16],'ro')
plt.show()


The output should be as shown below:

Matplotlib is specifically built to help you introduce mathematical expressions into your work using the LaTeX expressions. When keyed in correctly, the interpreter will recognize the expressions and aptly convert them into the necessary expression graphic. This is how to introduce formula, expressions, or other unique characters into your plot. When writing LaTeX expressions, remember to use an r before the expression so that the interpreter can read it as raw text.

plt.axis([0,5,0,20])
plt.title('My first plot',fontsize=20,fontname='Times New Roman')
plt.xlabel('Counting',color='gray')
plt.ylabel('Square values',color='gray')
plt.text(1,1.4,'First')
plt.text(2,4.4,'Second')
plt.text(3,9.4,'Third')
plt.text(4,16.4,'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()


Your plot should have a y=x2 expression in a yellow background as shown below:

 More often, you can go online and create charts that allow you to automatically add or remove grids. You can do this in Python, too. A grid is important in your work because it shows you the position of all the points plotted on the chart. To add a grid, introduce the grid() function as shown below, passing it as true.

plt.axis([0,5,0,20])
plt.title('My first plot',fontsize=20,fontname='Times New Roman')
plt.xlabel('Counting',color='gray')
plt.ylabel('Square values',color='gray')
plt.text(1,1.4,'First')
plt.text(2,4.4,'Second')
plt.text(3,9.4,'Third')
plt.text(4,16.4,'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 is as shown below:



If you want to do away with the grid, you should plot the condition as false as shown below:

plt.grid(True)



Share:

0 comments:

Post a Comment