previous:Python Numpy Loading data files and operation

In this article I will be making sine and cosine graph and use matplotlib library of python and its functions.First I will describe the functions then , I will show you the code and output.I will end this article with some specifications to make you understand the code clearly.

1.plot() function:

As the name clearly suggests , it plots the points.Its two most importatnt parameter are x-axis and y-axis 1d array.

2.xlim(),ylim():

with xlim and ylim functions , we can limit the graph in x axis and yaxis

3.xticks(),yticks():

xticks,yticks are used to display points required for the clarity of the graph.

4.gca():

helps to create an object to get the current axes

5.legend():

to generate the legend of the graph generated using the labels of the functions plotted.

%matplotlib inline #1
import matplotlib.pyplot as plt#2
import numpy as np#3
x=np.linspace(-2*np.pi,2*np.pi,512,endpoint=True)#4
cosx,sinx=np.cos(x),np.sin(x)#5
plt.plot(x,cosx,linewidth='2.0',color='purple',label='cosine')#6
plt.plot(x,sinx,linewidth='2.0',color='yellow',label='sine')#7
plt.xlim(-2*np.pi,2*np.pi)#8
plt.ylim(-1,1)#9
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi],[r'$-2\pi$', r'$-\pi$', r'$0$', r'$+\pi$', r'$+2\pi$'])#10
plt.yticks([-1,-0.5,0,0.5,1])#11
ax=plt.gca()#12
ax.spines['right'].set_color('none')#13
ax.spines['top'].set_color('none')#14
ax.xaxis.set_ticks_position('bottom')#15
ax.spines['bottom'].set_position(('data',0))#16
ax.yaxis.set_ticks_position('left')#17
ax.spines['left'].set_position(('data',0))#18
plt.legend(loc='lower left')#19
plt.show()#20

4th line contains np.pi which is used for the value of pi that is 22/7

6th line has properties like linewidth (for settinf thickness of line generated),color,label(used for setting label for legend)

9th line, if we change it to plt.ylim(1,6) the plotted graph will be something like this:

10th line , we have parameter a bit different that the 11th line,which are character representation -2pi to 2pi

11th line,remember that tick functions and lim functions are different,if line changed as plt.yticks([-1,-0.5,0,0.5,1,2]) graph will be something like this:

12th line an object is created of name ax

the spines[] in 13th,14th,16th,18th are actually selectors of the boundaries of graph

the set_color attribute is used to make right and top spine invisible by setting the value none.also without lines

12 to 18 graph would look something like this:

15th and 17th line are used for setting the tick position

16th and 18th line are used for intersection of two axes in 0,0

19th line has plt.legend with attribute loc='lower left' so that legend will be displayed at lower left positions.

other positions are:

upper left

lower left

lower right

right

center left

center right

lower center

upper center

center

next:Python Matplotlib plotting a pie chart

## Comments