2.4. Examples: Lecture 2#
2.4.1. Use of `matplotlib.pyplot`
#
drawing_01.py
#!/usr/bin/python
'''
example on the use of matplotlib.pyplot
'''
import matplotlib.pyplot as plt
def main () :
'''
Funzione che implementa il programma principale
'''
fig, ax = plt.subplots (nrows = 1, ncols = 1)
print (ax)
plt.savefig ('drawing_01.png')
plt.show ()
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
if __name__ == "__main__":
main ()
2.4.2. Drawing a function#
drawing_02.py
#!/usr/bin/python
'''
example on the use of matplotlib.pyplot
to draw a function
'''
import matplotlib.pyplot as plt
import numpy as np
def func (x) :
return np.cos (x - np.pi / 2.)
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
def main () :
'''
Funzione che implementa il programma principale
'''
fig, ax = plt.subplots (nrows = 1, ncols = 1)
# preparing the set of points to be drawn
x_coord = np.linspace (0, 2 * np.pi, 10000)
y_coord_1 = np.sin (x_coord)
y_coord_2 = np.arange (0., x_coord.size)
for i in range (x_coord.size):
y_coord_2[i] = func (x_coord[i])
# visualisation of the image
ax.plot (x_coord, y_coord_1, label='sin (x)')
ax.plot (x_coord, y_coord_2, linestyle = 'dashed', label='cos (x) - pi/2')
ax.set_title ('Comparing trigonometric functions', size=14)
ax.set_xlabel ('x')
ax.set_ylabel ('y')
ax.legend ()
plt.savefig ('drawing_02.png')
plt.show ()
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
if __name__ == "__main__":
main ()