Featured image of post Plot Graphs with Python and Matplotlib in Google Colab

Plot Graphs with Python and Matplotlib in Google Colab

Use NumPy and Matplotlib in Google Colab to plot sine and cosine curves from 0 to 2π.

This example plots sine and cosine curves in a Google Colab notebook with Python, NumPy, and Matplotlib. Colab’s interface and menu labels can change, so create a blank notebook using the current interface.

Requirements

You need a Google Account and an internet connection. With Colab’s hosted runtime, you can run the code without setting up a local Python environment.

Run the notebook cell

Paste the code into a code cell in a new notebook and run the cell. x contains 500 evenly spaced values from 0 through 2π. The plot evaluates sine and cosine at those radian values and displays a legend.

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 2 * np.pi, 500)
plt.plot(x, np.sin(x), label="sin curve")
plt.plot(x, np.cos(x), label="cos curve")
plt.legend()
plt.show()

Example result

The cell displays a graph with the sine and cosine curves. If a library is unavailable in the hosted runtime, run %pip install numpy matplotlib in a cell, then rerun the example.

Plot of sine and cosine

Plot of sine and cosine

Official references

comments powered by Disqus