Usando o matplotlib

O matplolib é um pacote de plotar gráficos para o Python. Ele é bastante versátil, popular e possui inúmeros exemplos e tutoriais. Confira em:

Como exemplo, vamos reproduzir a figura do fill_demo, disponível em:

É importante adicionar a primeira linha como um magic do Jupyter: %matplotlib inline:


In [5]:
import sys,os
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
    sys.path.append(ia898path)
import ia898.src as ia

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np

In [2]:
"""
Simple demo of the fill function.
"""

x = np.linspace(0, 1, 500)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

fig, ax = plt.subplots()

ax.fill(x, y, zorder=10)
ax.grid(True, zorder=5)
plt.show()


Veja outros exemplos da galeria do matplotlib:

Plotando duas figuras


In [9]:
f = mpimg.imread('../data/cameraman.tif')
plt.figure(1)
plt.imshow(f,cmap='gray')
h = ia.histogram(f)
plt.figure(2)
plt.plot(h)


Out[9]:
[<matplotlib.lines.Line2D at 0x7f9daceab908>]

In [12]:
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
#ax1.imshow(255-f,cmap='gray')
ax2.plot(h)


Out[12]:
[<matplotlib.lines.Line2D at 0x7f9dacf31be0>]