In [1]:
import matplotlib.pyplot as plt
In [7]:
# inline, sem nada, notebook
%matplotlib inline
In [3]:
import numpy as np
In [19]:
x = np.linspace(0, 5, 11)
x
Out[19]:
In [20]:
y = x ** 2
API Funcional (Scripting)¶
In [6]:
plt.plot(x, y)
# plt.show() # se vc n estiver no jupyter notebook, precisava chamar isso
Out[6]:
In [12]:
plt.plot(x, y)
plt.xlabel('Eixo X')
plt.ylabel('Eixo Y')
plt.title('Titulo')
Out[12]:
In [13]:
plt.plot(x, y)
plt.plot(x, x ** 3)
plt.xlabel('Eixo X')
plt.ylabel('Eixo Y')
plt.title('Titulo')
Out[13]:
Format string¶
fmt = '[marker][line][color]'
fmt = [color][marker][line]
e outras combinações qnd n eh ambiguo
https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot
In [30]:
plt.plot(x, y, 'xr--')
plt.plot(x, x ** 3, color='blue', linestyle='--', marker='o')
plt.xlabel('Eixo X')
plt.ylabel('Eixo Y')
plt.title('Titulo')
Out[30]:
In [41]:
plt.figure(figsize=(15, 5))
plt.subplot(1, 2, 1)
plt.plot(x, y, 'xr--')
plt.subplot(1, 2, 2)
plt.plot(x, x ** 3, color='blue', linestyle='--', marker='o')
Out[41]:
API Orientada a Objetos (POO)¶
A ideia principal é criar uma figura, e chamar métodos nela
In [42]:
fig = plt.figure()
In [52]:
fig = plt.figure()
rect = [0, 0, 1, 1]
axes = fig.add_axes(rect)
axes.plot(x, y)
axes.set_ylabel('Y Label')
axes.set_xlabel('X Label')
axes.set_title('Titulo')
plt.show()
In [70]:
fig = plt.figure()
axes1 = fig.add_axes([0, 0, 1, 1])
axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3])
axes1.plot(x, y)
axes2.plot(x**3, y)
axes1.text(4, 15, '<- Ponto top!')
Out[70]:
Resposta exercicio¶
In [77]:
fig = plt.figure()
axes1 = fig.add_axes([0, 0, 1, 1])
axes2 = fig.add_axes([0.55, 0.1, 0.4, 0.3])
axes1.plot(x, y)
axes2.plot(x**3, y)
Out[77]:
In [80]:
fig, axes = plt.subplots() # tuple unpacking
Tight layout: ajeita o posicionamento¶
In [101]:
plt.subplots(2, 2)
Out[101]:
In [100]:
plt.subplots(2, 2)
plt.tight_layout()
In [93]:
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
print(axes)
axes[0].plot(x, y)
axes[1].plot(y, x)
# plt.tight_layout()
Out[93]:
In [110]:
fig, axes = plt.subplots(2, 2, figsize=(5, 5), dpi=150)
print(axes)
colors = ['red', 'green', 'blue', 'yellow']
i = 0
for row in axes:
for ax in row:
ax.plot(x, y, color=colors[i])
i += 1
plt.tight_layout()
Resumo
- add_axes(): define o tamanho do axes, adicion um por um
- subplots(): mais automático, defininha numero de linhas e colunas
In [113]:
fig, axes = plt.subplots(2, 2, figsize=(5, 5), dpi=150)
print(axes)
colors = ['red', 'green', 'blue', 'yellow']
i = 0
for row in axes:
for ax in row:
ax.plot(x, y, color=colors[i])
i += 1
plt.tight_layout()
fig.savefig('meu_grafico.png')
In [149]:
fig, axes = plt.subplots(figsize=(10, 5))
axes.plot(x, y * 5, label='Grafico 2')
axes.plot(x, y, label='Grafico 1')
axes.set_ylabel('Eixo Y')
axes.legend(fontsize=12, shadow=True, title='Algoritmos')
Out[149]:
Estiliazar o gráfico¶
In [169]:
fig, axes = plt.subplots(figsize=(10, 5))
axes.plot(x, y * 5, label='Grafico 2', alpha=0.5, linewidth=10, color='red',
markerfacecolor='yellow',
markeredgewidth=2,
markeredgecolor='green',
marker='*', markersize=20)
axes.plot(x, y, label='Grafico 1')
Out[169]:
In [180]:
fig, axes = plt.subplots(figsize=(10, 5))
axes.plot(x, y * 5, label='Grafico 2')
axes.plot(x, y, label='Grafico 1')
axes.set_ylabel('Eixo Y')
axes.set_ylim([-100, 200])
Out[180]:
In [ ]:
fig, axes = plt.subplots(figsize=(10, 5))
axes.plot(x, y * 5, label='Grafico 2')
axes.plot(x, y, label='Grafico 1')
axes.set_ylabel('Eixo Y')
axes.set_ylim([-100, 200])
axes.set_yticks([]) # remover
mudando o label do eixo x¶
In [200]:
fig, axes = plt.subplots(figsize=(10, 5))
axes.plot(x, y * 5, label='Grafico 2')
axes.plot(x, y, label='Grafico 1')
axes.set_ylabel('Eixo Y')
axes.set_ylim([-100, 200])
# plt.xticks(range(len(x)), labels=[string.ascii_letters[i] for i in range(len(x))])
# ax.set_xticks(x)
axes.set_xticklabels([string.ascii_letters[i] for i in range(len(x))])
plt.show()
In [ ]: