How to plot functions in python
4 min read

How to plot functions in python

Intro

This article is made for the engineers out there, or for anyone wanting to learn about python. I'll try to keep this article as short as possible to get to the point. I'll use NumPy and matplotlib, so be sure to have them installed on your computer, if they're not, just open your command line and type:
pip install matplotlib
and
pip install numpy
(in any case, they're great libraries to have installed). So, let's get right into it.

Preparing our data

Before actually plotting the function, we need to know two things: On which interval do we want our function graphed (0 to 10 ?, -5 to 5 ? etc...), and how many points will we want to use to graph the function (this will have an impact on the smoothness of a function. As an example, we'll graph the function
f(x)=sin2(x)
Let's see how modifying those two criteria can impact our result, let's take two different scenarios:

  • Graphing from -10 to 10 with 1000 points
  • Graphing from -3 to 1 with 10 points

This is up to you to choose the best parameters.

To implement this, we'll use NumPy's linspace function:

import numpy as np

def f(x): #this can be any function
    return np.sin(x)**2

x = np.linspace(-10,10,1000) #(start, end, nb_points)
y = list(map(f, x)) #we pass an array of points "through" a function

plotting

Now, we need to actually plot the points. This time, we'll use matplotlib. This is actually very straightforward:

import matplotlib.pyplot as plt
plt.plot(x,y) #we plot our points
plt.show() #this is to show the graph

This is the result we'd get:

This is the most basic method, but we can add more information to the graph, for example:

A title

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return np.sin(x)**2

x = np.linspace(-10,10,1000)
y = list(map(f, x))
plt.title("this is a graph!")
plt.plot(x,y)
plt.show()

Or legend(s)

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return np.sin(x)**2

x = np.linspace(-10,10,1000)
y = list(map(f, x))
plt.title("this is a graph!")
plt.plot(x,y, label="f(x)=sin(x)^2")
plt.legend()
plt.show()

plotting multiple functions

If you need to plot multiple functions, be it for comparing them or whatever, matplotlib lets you do that pretty easily.
In the first case, if you want to plot them on the same graph, you can just use plot.plot() as many times as needed:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return np.sin(x)**2
    
def g(x):
    return np.sin(x**2)
    
def h(x):
    return 2*np.cos(x)

x = np.linspace(-10,10,1000)
y1 = list(map(f, x))
y2 = list(map(g, x))
y3 = list(map(h, x))

plt.title("comparing functions")
plt.plot(x,y1, label="f(x)=sin(x)^2")
plt.plot(x,y2, label="f(x)=sin(x^2)")
plt.plot(x,y3, label="f(x)=2cos(x)")
plt.legend()
plt.show()

In the second case, you'd want to plot the functions on different graphs. In this case, you can use matplotlib's plt.figure() function to create new graphs each time you plot a new function. However, you'll have to remember to setup the title and legend for each individual figure :

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return np.sin(x)**2
    
def g(x):
    return np.sin(x**2)
    
def h(x):
    return 2*np.cos(x)

x = np.linspace(-10,10,1000)
y1 = list(map(f, x))
y2 = list(map(g, x))
y3 = list(map(h, x))

plt.title("figure 1")
plt.plot(x,y1, label="f(x)=sin(x)^2")
plt.legend()

plt.figure()
plt.title("figure 2")
plt.plot(x,y2, label="f(x)=sin(x^2)")
plt.legend()

plt.figure()
plt.title("figure 3")
plt.plot(x,y3, label="f(x)=2cos(x)")
plt.legend()

plt.show()

Thanks to all the reader, I hope this can be usefull for some curious people, and subscribe to my newsletter, it's free and keeps you updated with the latest programming tips!