Datenvisualisierung mit

Matplotlib

Matplotlib

Öffnet ein Terminal (auf Windows: Git-Bash) und gebt ipython ein

Als erstes: IPython interaktiv machen:

In [1]:
%matplotlib inline
# bei euch: %maplotlib qt

Man kann beliebige Python-Kommandos eingeben.

Um mit Matplotlib arbeiten zu können, muss die Bibliothek erst einmal importiert werden:

In [2]:
from pylab import *

Ein einfaches Beispiel:

In [3]:
x = linspace(0, 1)
plot(x, x**2)
# Falls nicht interaktiv: show()
Out[3]:
[<matplotlib.lines.Line2D at 0x32680d0>]

Anderes Beispiel: \(\sin(t)\)

In [4]:
t = linspace(0, 2 * pi)
plot(t, sin(t))
#plot(t, sin(t), 'r--')
#plot(t, sin(t), 'go')
Out[4]:
[<matplotlib.lines.Line2D at 0x31cb690>]

Tabelle mit allen Farben und Styles: matplotlib.axes.Axes.plot

In [5]:
plot(t, sin(t))
#xlim(0, 2 * pi)
#ylim(-1.2, 1.2)
Out[5]:
[<matplotlib.lines.Line2D at 0x330df90>]

Es fehlt noch etwas...

XKCD comic on why you should label your axes.

XKCD comic on why you should label your axes.

In [6]:
with xkcd():
    plot(t, sin(t))
    xlabel('t')
    ylabel('sin(t)')
In [7]:
plot(t, sin(t))
xlabel('$t$')
ylabel('$\sin(t)$')
Out[7]:
<matplotlib.text.Text at 0x3823250>
In [8]:
plot(t, sin(t), label=r'$\sin(t)$')
legend()
#legend(loc='lower left')
Out[8]:
<matplotlib.legend.Legend at 0x383b750>

x und f(x) sind einfach nur Datenpunkte:

In [9]:
x = array([1, 2, 3, 4])
x
#x**2
#plot(x, x**2, 'ro-')
Out[9]:
array([1, 2, 3, 4])

Laden von Daten

In [10]:
x, y = loadtxt('example_data.txt', unpack=True)

plot(x, y, 'k.')

t = linspace(0, 10)
plot(t, 5 * t, 'r-')
Out[10]:
[<matplotlib.lines.Line2D at 0x331a350>]

Auslagern in ein Skript

Speichert den folgenden Code in eine Textdatei plot.py ab.

Öffnet ein Terminal und startet das Programm:

python2 plot.py
In [11]:
from pylab import *

x = linspace(0, 1)
plot(x, x**2, 'b-')
savefig('plot.pdf')

Mit savefig speichert man die Abbildung.

In diesem Fall sollte die Datei plot.pdf erstellt worden sein.

Es gibt viele Ausgabeformate: pdf, png, svg, LaTeX

Komplexere Abbildungen

Natürlich kann man mehrere Linien in einen Plot packen:

In [12]:
x = linspace(0, 1)

plot(x, x**2, label='$x^2$')
plot(x, x**4)
plot(x, x**6, 'o', label='$x^6$')

legend(loc='best')
Out[12]:
<matplotlib.legend.Legend at 0x3ff55d0>

Es werden nur die Linien in der Legende angezeigt, die einen Label haben.

Man kann auch mehrere Plots in ein Bild packen:

In [13]:
x = linspace(0, 2 * pi)

subplot(1, 2, 1)
plot(x, x**2)
xlim(0, 2 * pi)

subplot(1, 2, 2)
plot(x, sin(x))
xlim(0, 2 * pi)
Out[13]:
(0, 6.283185307179586)

Plot im Plot:

In [14]:
plot(x, x**2)

axes([0.2, 0.45, 0.3, 0.3])
plot(x, x**3)
Out[14]:
[<matplotlib.lines.Line2D at 0x4494d90>]

Fehlerbalken sind ganz wichtig:

In [15]:
x = linspace(0, 2 * pi, 10)
errX = 0.4 * randn(10)
errY = 0.4 * randn(10)

errorbar(x + errX, x + errY, xerr=0.4, yerr=0.4, fmt='o')
Out[15]:
<Container object of 3 artists>

Logarithmische (und auch viele andere) Skalierung der Achsen ist auch möglich:

In [16]:
x = linspace(0, 10)

plot(x, exp(-x))
yscale('log')
#xscale('log')

Manchmal braucht man einen Polarplot:

In [17]:
r = linspace(0, 10, 1000)
#r = linspace(0, 10, 50)
theta = 2 * pi * r

polar(theta, r)
Out[17]:
[<matplotlib.lines.Line2D at 0x4119f90>]

Man kann sehr viele Sachen mit Ticks machen…

In [18]:
x = linspace(0, 2 * pi)

plot(x, sin(x))
xlim(0, 2 * pi)
xticks([0, pi / 2, pi, 3 * pi / 2, 2 * pi], [r"$0$", r"$\frac{\tau}{4}$", r"$\frac{\tau}{2}$", r"$\frac{3\tau}{4}$", r"$\tau$"])
title(r"$\tau$ FTW!")
Out[18]:
<matplotlib.text.Text at 0x4a4b8d0>
In [19]:
months = ['January',
          'February',
          'March',
          'April',
          'May',
          'June',
          'July',
          'August',
          'September',
          'October',
          'November',
          'December']

plot(arange(12), rand(12))
xticks(arange(12), months, rotation=45, rotation_mode='anchor', ha='right', va='top')
xlim(0, 11)
Out[19]:
(0, 11)
In [20]:
subplot(2, 1, 1)
plot(x, sin(x), 'b-')
xlabel('Test')
xlim(0, 2 * pi)

subplot(2, 1, 2)
plot(x, cos(x), 'r-')
xlim(0, 2 * pi)

tight_layout()