Data-Viz

Last updated: May 8th, 20202020-05-08Project preview
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]:
array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
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]:
[<matplotlib.lines.Line2D at 0x118227990>]
In [12]:
plt.plot(x, y)
plt.xlabel('Eixo X')
plt.ylabel('Eixo Y')
plt.title('Titulo')
Out[12]:
Text(0.5, 1.0, 'Titulo')
In [13]:
plt.plot(x, y)
plt.plot(x, x ** 3)
plt.xlabel('Eixo X')
plt.ylabel('Eixo Y')
plt.title('Titulo')
Out[13]:
Text(0.5, 1.0, 'Titulo')

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]:
Text(0.5, 1.0, 'Titulo')
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]:
[<matplotlib.lines.Line2D at 0x11b7e9d10>]

API Orientada a Objetos (POO)

A ideia principal é criar uma figura, e chamar métodos nela

In [42]:
fig = plt.figure()
<Figure size 432x288 with 0 Axes>
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]:
Text(4, 15, '<- Ponto top!')

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]:
[<matplotlib.lines.Line2D at 0x11dc89f10>]
In [80]:
fig, axes = plt.subplots() # tuple unpacking

Tight layout: ajeita o posicionamento

In [101]:
plt.subplots(2, 2)
Out[101]:
(<Figure size 432x288 with 4 Axes>,
 array([[<matplotlib.axes._subplots.AxesSubplot object at 0x11fedf450>,
         <matplotlib.axes._subplots.AxesSubplot object at 0x11f490390>],
        [<matplotlib.axes._subplots.AxesSubplot object at 0x11ed220d0>,
         <matplotlib.axes._subplots.AxesSubplot object at 0x11bcfd650>]],
       dtype=object))
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()
[<matplotlib.axes._subplots.AxesSubplot object at 0x11fc0dbd0>
 <matplotlib.axes._subplots.AxesSubplot object at 0x11fbf5090>]
Out[93]:
[<matplotlib.lines.Line2D at 0x11fd51710>]
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()
[[<matplotlib.axes._subplots.AxesSubplot object at 0x11e416510>
  <matplotlib.axes._subplots.AxesSubplot object at 0x11a574990>]
 [<matplotlib.axes._subplots.AxesSubplot object at 0x11ffeee10>
  <matplotlib.axes._subplots.AxesSubplot object at 0x11ec46390>]]

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')
[[<matplotlib.axes._subplots.AxesSubplot object at 0x11f2ff2d0>
  <matplotlib.axes._subplots.AxesSubplot object at 0x11ef96110>]
 [<matplotlib.axes._subplots.AxesSubplot object at 0x11f356610>
  <matplotlib.axes._subplots.AxesSubplot object at 0x11f38cb10>]]
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]:
<matplotlib.legend.Legend at 0x11e6a5390>

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]:
[<matplotlib.lines.Line2D at 0x11ec622d0>]
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]:
(-100.0, 200.0)
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 [ ]:
 
Notebooks AI
Notebooks AI Profile20060