Plotting Sin and Cos Waves
Learn how to plot sin and cos waves using matplotlib in Python.
We'll cover the following
Introduction to Matplotlib
Matplotlib is a visualization library in Python for plotting 2D array plots. It is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It can be used to plot all sorts of graphs like line, bar, scatter, histogram, and so on.
We’ll import Matplotlib into our code like so:
from matplotlib import pyplot as plt
or
import matplotlib.pyplot as plt
The new thing here is: import matplotlib.pyplot as plt
. We are importing it as plt
for user ease.
Matplotlib is a vast library. So, we’ll only import the
pyplot
section. This is useful in saving memory and speeding up code. Otherwise, we’ll be importing gigabytes of libraries every time we want to print “Hello World” to the screen.
Learning to use Python skillfully means using a lot of libraries and functions. But we don’t have to remember them all. We can just google them on runtime whenever we need any function that belongs to a Python library.
Let’s work with one of the functions provided by NumPy, linspace()
.
linspace()
generates evenly spread out values. In the example above, it will generate 1000 values between 0 and 20. Printing 1000 values will take a lot of space here, so let’s see what happens when we generate only 10 values:
Get hands-on with 1400+ tech skills courses.