Plotting of Functions

Pylab has a vast amount of plotting functions which supports simple plotting with just two commands (plot and show) up to complex animations of 3D data. Plotting a sine wave can be achieved with:
 
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2*np.pi,100)
y = np.sin(x)
plt.plot(x,y)
plt.show()
where I have used the numpy linspace command to create an array which runs from 0 to two pi with 100 samples.

If you want to create more plot-windows you can use the command plt.figure. Access the windows with the commands plt.figure(1), plt.figure(2).

It is also possible to combine plot windows into one with the command subplot.

How to make it look nice? Here is an example:

plt.xlabel('time (s)')
plt.ylabel('voltage (V)')
plt.title('That is an AC voltage over time!')
plt.grid(True)

Generally the best approach is to use an example and then customise it to your needs.



github / contact

Creative Commons License