### Python Matplotlib and plotting sin and cos graphs

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.

The Functions:

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.

code:
%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

output

Specifications:

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

#### More Articles of Kislay Kanti Dhar:

Name Views Likes
Python Boto3:glacier remove tags from vault 178 0
Python Boto3:aws glacier delete vault access policy 153 0
Python Boto3:aws glacier get and set vault access policy 155 0
Python Boto3: aws glacier describe vault 169 0
Python boto3:aws glacier adding tags to vaults and listing them 148 0
Python Boto3 : getting list of Vaults of aws Glacier 203 0
Python boto3 : create aws glacier vault 233 0
configuring boto3 245 1
Rendering Html5 forms and getting forms data with Python Flask 296 0
Python Bisect module:sorting on the go 249 0
Python heapq: The library for implementing heaps and heapsort 191 0
Python DB2 Drop table 303 0
Python DB2 select from table 239 0
Python DB2 multiple insert record into table 276 0
Python Collections : using deque 452 0
Python Project:Making a voice recorder with Python 1697 4
Python DB2 how to get the tuples using ibm_db.fetch_tuple 467 0
Python Collections: understanding the counter 202 0
Python Collections:understanding ChainMap 276 0
Python Enum: exploring the enum datastructure 268 0
DB2: Inserting values into an existing table using Python 899 0
DB2: Creating a table with python code 775 0
DB2 :connecting IBM database server to python 754 0
IBM DB2: an introduction 228 0
Python UDP Client Server program 365 0
Python Socket: basic functions and concepts 256 0
Python TCP Client Server program 539 0
Python Project: Attendance Manager GUI application made with Tkinter 798 1
Python Program to get Google search results 205 0
Python Program for opening google search 189 0
Python program to find out the disk speed 257 0
Python Matplotlib scatter plots 294 1
Python Matplotlib plotting bar graphs 441 0
Python Matplotlib plotting a pie chart 364 0
Python Matplotlib and plotting sin and cos graphs 1327 0