2808 explained code solutions for 75 technologies


python-matplotlibHow to plot a function


In order to plot a function (defined with def) we can easily use Numpy:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
  return 2*x*x - 3*x + 7

x = np.linspace(-10, 10, 30)

plt.plot(x, f(x))

plt.show()ctrl + c
import matplotlib.pyplot as plt

loads Matplotlib module to use plotting capabilities

import numpy as np

load Numpy module for Python

def f(x)

defined function we need to plot

np.linspace

generate random values based on specified range and count

.plot(

plot specified data

x, f(x)

use x array for X-axis and f(x) values (will be automatically calculated by Numpy) for Y-axis

.show()

render chart in a separate window