This tutorial will show you how to use the Numpy sin function to compute the trigonometric sine in Python.
I’ll explain the syntax of np.sin, how the function works, and how to use it.
If you need something specific, you can click on any of the following links.
Table of Contents:
Before we jump into the syntax, let’s start with a quick overview.
A Quick Introduction to Numpy Sin
The Numpy sin function is fairly straight-forward. It computes the trigonometric sine in Python.
You can use the Numpy sin function to compute the trigonometric sine of single values, but you can also use it to compute the sine values of arrays of numbers.
I’ll show you how to do both, but first, we’ll look at the syntax.
The syntax of np.sin
The syntax for the Numpy sin function is simple:
Keep in mind that this syntax assumes that you’ve imported Numpy with the alias
Acceptable formats for the input
Let’s quickly talk about the input to the np.sin function.
In the above syntax explanation, you’ll notice the argument to the function, which I’ve called “
This can take several different forms:
- a number (integer or float)
- a Numpy array (filled with numbers)
- a Python list of numbers, or list-like object
So there are several possible input types to the function.
The output of np.sin
As an output, the np.sin function computes the trigonometric sine of the input.
But the exact structure of the output depends on the input.
If the input is a single number, then the output will be a single number (the sine of the input). If the input is a Numpy array, then the output will be a Numpy array that contains the element-wise sines of the input values.
In addition to the input argument, np.sine a couple of optional parameters:
These are somewhat rarely used, but so I won’t explain them here.
Examples of how to use Numpy sin
Now that we’ve looked at the syntax for the Numpy sine function, let’s look at some examples.
- Compute the sine of 0
- Compute the sine of π/2
- Use the Numpy sine function on a Numpy array of values
- Plot the sine (using Plotly)
Preliminary code: Import Numpy and Set Up Plotly
Before you run these examples, you will need to run some setup code.
Specifically, you need to import Numpy and you need to import Plotly (which we’ll use to plot the sine function).
First, let’s import Numpy and Plotly.
You can do that with the following code:
import numpy as np import plotly.express as px
Set Up Image Rendering
Next, before we run example 4 (where we’ll plot the sine function), we need to set things up so that the plot will render in an IDE.
By default, Plotly renders output visualizations in browser windows.
So if you’re using an Integrated Development Environment (like Spyder, PyCharm, etc), you’ll need to set your IDE up to render plots from Plotly.
Note: if you’re using Jupyter, you can skip this code!
To set up Plotly to render your plots as svg images in your IDE, run the following code:
import plotly.io as pio pio.renderers.default = 'svg'
Once you’ve done this, you’ll be ready to run the examples.
EXAMPLE 1: Compute the sine of 0
We’ll start with a simple example.
Here, we’ll compute the sine of 0:
Obviously, this is a very simple example.
Here, we’re using Numpy to compute the sine of 0, which is 0. (The output is actually a float, so technically, the output is 0.0).
EXAMPLE 2: Compute the sine of π/2
Next, we’ll compute the sine of π/2.
Again, this is very simple.
We’re computing the sine of π/2. To do this, we’re actually using
np.pi/2 as the argument to the function.
The output is 1.0.
EXAMPLE 3: Use the Numpy sine function on a Numpy array of values
Now, we’ll compute the sine of an array of values.
Remember: Numpy sine can operate on a single number, a Numpy array, a Python list, or a list-like object.
Create Numpy Array
Here, we’ll create a Numpy array of 300 evenly spaced values, using Numpy linspace.
x_values = np.linspace(start = -np.pi, stop = np.pi, num = 300)
Once you run this,
x_values will contain 300 evenly spaced values from -π to π.
Compute Sine using Numpy
Now that we have an array of x values, we’ll compute the sine of those values using np.sine:
sine_values = np.sin(x_values)
And just to take a look, we’ll print the first 10 values:
array([-0.02101245, -0.04201562, -0.06300024, -0.08395704, -0.10487677, -0.12575019, -0.14656808, -0.16732125, -0.18800053])
This is pretty straight forward.
The input to np.sin is a Numpy array.
The output is the element-wise sine of each value of that input array.
EXAMPLE 4: Plot the sine values using Plotly
Finally, let’s plot the data sine values we computed in example 3.
Here, we’re going to use the Plotly line function to create a line chart of our sine values.
px.line(x = x_values, y = sine_values)
Here, we’re plotting the sine values computed by Numpy sine in example 3.
The x-axis input to px.line is the values contained in
And on the y-axis, we’re plotting the sine values in the array called
sine_values, which we computed with Numpy sine.
Leave your other questions in the comments below
Do you have other questions about how to use np.sin to compute sine values in Python?
If so, leave your questions in the comments section below.
Join our course to learn more about Numpy
In this tutorial, I’ve explained how to compute the trigonometric sine in Python using Numpy sine.
This should help you with computing sines for some math operations, but if you really want to learn Numpy, there’s a lot more to learn.
If you’re serious about mastering Numpy, and serious about data science in Python, you should consider joining our premium course called Numpy Mastery.
Numpy Mastery will teach you everything you need to know about Numpy, including:
- How to create Numpy arrays
- How to reshape, split, and combine your Numpy arrays
- What the “Numpy random seed” function does
- How to use the Numpy random functions
- How to perform mathematical operations on Numpy arrays
- and more …
Moreover, this course will show you a practice system that will help you master the syntax within a few weeks. We’ll show you a practice system that will enable you to memorize all of the Numpy syntax you learn. If you have trouble remembering Numpy syntax, this is the course you’ve been looking for.
Find out more here: