Bruk av Matplotlib i Python

Les om Matplotlib her:

For hvert eksempel kan man laste ned et Jupyter Notebook-dokument og bruke som utgangspunkt for egne plott.

In [1]:
import matplotlib.pyplot as plt

Vil lage et plott av funksjonen

$f(x) = x² $

for området $[-10, 10]$

Metode:

  • $x$-verdiene legges i listen $X$
  • $f(x)$-verdiene legges i listen $Y$
In [2]:
X = list(range(-10, 11))
print(X)
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [3]:
Y = []

for x in X:
    Y.append(x**2)

print(Y)
[100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [4]:
plt.plot(X, Y)
plt.show()
In [5]:
plt.plot(X, Y)
plt.axis([-11, 11, -10, 110])
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()
In [6]:
plt.plot(X, Y, 'o')       # o, ., x, X, etc
plt.axis([-11, 11, -10, 110])
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()