NumPy Cos – A Complete Guide

NumPy Cos Featured Image

Welcome to the second tutorial of the series NumPy Trigonometric Function. In this tutorial, we will understand about NumPy Cos function.

NumPy provides many Trigonometric functions and NumPy Cos is one of them. Just like Numpy Sine produces the output in the range [-1, 1], the output of the Cosine function is the same.

We will practice a lot of examples to make our understanding clear and let’s get started.

What is NumPy Cos?

NumPy Cos is one of the trigonometric functions provided by NumPy Library and computes the trigonometric cosine of a single number as well as a NumPy Array of angles.

Note: NumPy Cos function can be accessed as numpy.cos

Syntax of NumPy Cos

NumPy Cos takes angles in radians as an argument. However, the angle in degrees can also be given as the argument.

Syntax: numpy.cos(input) where input can be a single number as well as a NumPy Array

Cos of Single Angle

Let’s try some examples of the Numpy Cos function to help us understand it better.

Numpy Cosine on Pi Values

import numpy as np

print("Printing the Cosine Values\n")

print("Cosine of 0 is :",np.cos(0))

print("Cosine of pi/6 is :",np.cos(np.pi/6))

print("Cosine of pi/3 is :",np.cos(np.pi/3))

print("Cosine of pi/2 is :",np.cos(np.pi/2))

print("Cosine of pi is :",np.cos(np.pi))


Printing the Cosine Values

Cosine of 0 is : 1.0
Cosine of pi/6 is : 0.8660254037844387
Cosine of pi/3 is : 0.5000000000000001
Cosine of pi/2 is : 6.123233995736766e-17
Cosine of pi is : -1.0
  • Every output is pretty much clear except the output of the Cosine of pi/2.
  • Numpy Cosine of pi/2 provides a different output – the output is in scientific notation with an exponent of 10-17 which is equal to 0.

Now, let’s see how can we pass angles in degrees as an argument to the numpy.cos function.

Numpy Cos Function with the Deg2Rad function

To compute the cosine of angles in which the argument for the cos function is in degrees function deg2rad is used.

import numpy as np

print("Cosine of 30 degrees is :",np.sin(np.deg2rad(30)))

print("Cosine of 60 degrees is :",np.sin(np.deg2rad(60)))

print("Cosine of 90 degrees is :",np.sin(np.deg2rad(90)))

print("Cosine of 180 degrees is :",np.sin(np.deg2rad(180)))


Cosine of 30 degrees is : 0.49999999999999994
Cosine of 60 degrees is : 0.8660254037844386
Cosine of 90 degrees is : 1.0
Cosine of 180 degrees is : 1.2246467991473532e-16

That was about passing angles in degrees as an argument to numpy.cos() function.

Now, let’s see how can we compute the cosine of an array of angles.

Numpy Cosine on Multiple Angles

The cos function also takes a Numpy Array of angles as an argument but we must make sure that the angles are converted to radians.

Numpy Cos on An Array on Angles

import numpy as np

# A NumPy array with all the angles in degrees
a = np.array((0 , 30 , 45 , 60 , 90))

print("Cosine Values :\n",np.cos(a*np.pi/180))

# A NumPy array with all the angles is radians
b = np.array((0 , np.pi/2 , np.pi/3 , np.pi))

print("Cosine Values :\n",np.cos(b))


Cosine Values :
 [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01
Cosine Values :
 [ 1.000000e+00  6.123234e-17  5.000000e-01 -1.000000e+00]

In the above snippet, the output is a NumPy Array where the values are quite strange. But if you carefully observe it then you will understand that the output is in scientific notation.

Numpy Cosine On an Evenly-Spaced Numpy Array

In this example, we will create a NumPy Array of 30 evenly spaced values using numpy.linspace.

import numpy as np

a = np.linspace(-(2*np.pi) , 2*np.pi , 30)

print("Cosine Values: ",np.cos(a))


Cosine Values:  [ 1.          0.90757542  0.64738628  0.26752834 -0.161782   -0.56118707
 -0.85685718 -0.99413796 -0.94765317 -0.72599549 -0.37013816  0.05413891
  0.46840844  0.79609307  0.97662056  0.97662056  0.79609307  0.46840844
  0.05413891 -0.37013816 -0.72599549 -0.94765317 -0.99413796 -0.85685718
 -0.56118707 -0.161782    0.26752834  0.64738628  0.90757542  1.        ]

Here, we have created a NumPy Array using numpy.linspace which has 30 evenly spaced angles in radians ranging from -2pi to 2pi.

The output is also a NumPy Array which is the cosine of the elements of the array.

Visualizing the Cos Function

import numpy as np

# Importing the Matplotlib Library
import matplotlib.pyplot as plt

# Creating a NumPy Array of 30 evenly-spaced elements
a = np.linspace((-2*np.pi),(2*np.pi),30)

# Storing the cosine values in a NumPy Array
b = np.cos(a)

plt.plot(a, b, color = "blue", marker = "o")


Cosine Plot
Cosine Plot

plt.plot() function is used to plot the Cosine Function which takes four arguments.

  • The first argument is the NumPy Array of angles (created in Line No 7), plotted on the X-axis(Horizontal Axis).
  • The second argument is the output of the cos function stored in as a NumPy Array, plotted on the Y-axis(Vertical Axis).
  • The third argument is the color of the plot.
  • The fourth argument is the marker value which emphasizes each point with a specified marker. There are different types of markers that can be used to denote the points on the curve.

You now know how the curve of the cosine function looks like.


In this tutorial, we understood how to use the NumPy Cos function with examples. If you are using Jupyter Notebook then after writing each line of code in each cell press shift+enter to get the output.

Your task is to use the NumPy Cos function to compute the cosine of more values of your choice.

In the next tutorial, we will go through the NumPy Tan function in detail. Till then, Stay Tuned.


NumPy Documentation – NumPy Cos

Matplotlib – Get Started