File tree Expand file tree Collapse file tree 5 files changed +106
-0
lines changed Expand file tree Collapse file tree 5 files changed +106
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ 12. Write a program that makes use of a function to accept a list of n integers and displays a histogram.
3+ Author: Anshul Verma
4+ '''
5+
6+ import matplotlib .pyplot as plt
7+
8+ def inputList ():
9+ '''
10+ To take a input of list of integers from user
11+ Returns the list
12+ '''
13+ ls = []
14+ length = int (input ('Enter the length of list: ' ))
15+ for i in range (0 , length , 1 ):
16+ ls .append (int (input ('Enter element {}:' .format (i + 1 ))))
17+ # Using eval
18+ # ls = eval(input('Enter the integers: '))
19+ return ls
20+
21+ if __name__ == "__main__" :
22+
23+ list = inputList ()
24+ plt .hist (list , )
25+ plt .xlabel ('Integer' )
26+ plt .ylabel ('Frequency' )
27+ plt .title ('Histogram' )
28+ plt .savefig ('hist.png' , dpi = 275 , bbox_inches = 'tight' ) # To save the plot
29+ plt .show ()
Original file line number Diff line number Diff line change 1+ '''
2+ 13. Write a program that makes use of a function to display sine, cosine, polynomial and exponential curves.
3+ Author: Anshul Verma
4+ '''
5+
6+ import matplotlib .pyplot as plt
7+ import math
8+
9+ def sineCurve ():
10+ '''
11+ To plot sine function
12+ '''
13+
14+ plt .subplot (2 , 2 , 1 )
15+ degrees = range (0 , 360 + 1 )
16+ sinValues = [math .sin (math .radians (i )) for i in degrees ]
17+ plt .plot (sinValues )
18+ plt .xlabel ('Degrees' )
19+ plt .ylabel ('Sin Values' )
20+ plt .title ('Sine Curve' )
21+ plt .grid ()
22+
23+ def cosineCurve ():
24+ '''
25+ To plot cos function
26+ '''
27+
28+ plt .subplot (2 , 2 , 3 )
29+ degrees = range (0 , 360 + 1 )
30+ cosValues = [math .cos (math .radians (i )) for i in degrees ]
31+ plt .plot (degrees , cosValues )
32+ plt .xlabel ('Degrees' )
33+ plt .ylabel ('Cosine Values' )
34+ plt .title ('Cosine Curve' )
35+ plt .grid ()
36+
37+ def polynomialCurve ():
38+ '''
39+ To plot a polynomial function
40+ '''
41+ def polynomial (x ):
42+ return (8 * x * x )
43+ plt .subplot (2 , 2 , 2 )
44+ x = range (- 51 , 50 + 2 )
45+ y = [polynomial (i ) for i in x ]
46+ plt .plot (x , y )
47+ plt .xlabel ('x' )
48+ plt .ylabel ('y = 8x^2' )
49+ plt .title ('Polynomial Curve' )
50+ plt .grid ()
51+
52+ def expCurve ():
53+ '''
54+ To plot sine function
55+ '''
56+
57+ plt .subplot (2 , 2 , 4 )
58+ x = []
59+ for i in range (0 , 100 * 10 ):
60+ x .append ((- 5 ) + (0.01 )* i )
61+ y = [math .exp (i ) for i in x ]
62+ plt .plot (x , y )
63+ plt .xlabel ('x' )
64+ plt .ylabel ('y = e^x' )
65+ plt .title ('Exponetial Curve' )
66+ plt .grid ()
67+
68+ if __name__ == "__main__" :
69+ plt .figure (figsize = (9 , 5 )) # To set the figure size
70+ sineCurve ()
71+ cosineCurve ()
72+ polynomialCurve ()
73+ expCurve ()
74+ plt .tight_layout ()
75+ plt .savefig ('plot.png' , dpi = 275 , bbox_inches = 'tight' ) # To save the plot
76+ plt .show ()
77+
You can’t perform that action at this time.
0 commit comments