Plotly Charts¶


  • Author: Diego Inácio
  • GitHub: github.com/diegoinacio
  • Notebook: plotly_charts.ipynb

Examples of interactive chart visualization using Plotly.

[PT-BR content]

In [1]:
from plotly import __version__
from plotly.subplots import make_subplots
from plotly.graph_objs import *

print(__version__)
4.14.3
In [2]:
import numpy as np

Gráficos¶


Exemplos de gráficos separados por tipos e utilizando dados aleatórios.

Linha¶


  • Linha [plot]: Visualização de gráfico linha linear.
  • Linhas: Visualização de gráfico linha com variação de estilo e marcação.
In [3]:
# Dados para linha
N = 8
t = np.linspace(0, 1, N)

linA = Scatter(
    x=t, y=t, name='linear',
   mode='lines+markers',
   marker={'symbol': 'circle', 'size': 7},
   showlegend=False
)

linB = Scatter(
    x=t, y=t, name='linear',
   mode='lines+markers',
   line={'dash': 'dash'},
   marker={'symbol': 'star', 'size': 10},
   showlegend=False
)

quad = Scatter(
    x=t, y=t**2, name='quadrática',
   mode='lines+markers',
   line={'dash': 'dashdot'},
   marker={'symbol': 'square', 'size': 7},
   showlegend=False
)

cubi = Scatter(
    x=t, y=t**3, name='cúbica',
   mode='lines+markers',
   line={'dash': 'dot'},
   marker={'symbol': 'triangle-up', 'size': 10},
   showlegend=False
)
In [4]:
%%time
fig = make_subplots(
    rows=1, cols=2, print_grid=False,
    subplot_titles=['Linha', 'Linhas']
)

fig.append_trace(linA, 1, 1)
fig.append_trace(linB, 1, 2)
fig.append_trace(quad, 1, 2)
fig.append_trace(cubi, 1, 2)

fig.show()
Wall time: 567 ms

Dispersão¶


  • Dispersão [scatter]: Visualização de gráfico de disperção, com variação de cor.
  • Dispersão em bolha [bubble chart]: Visualização de gráfico de disperção, com variação de cor e escala.
In [5]:
# Dados para dispersão
N = 128
x = np.random.rand(N)
y = np.random.rand(N)
c = np.random.rand(N)
s = np.random.rand(N)
s = np.pi*s*16

dispA = Scatter(
    x=x, y=y, name='pontos', mode='markers',
    marker={
        'color': c, 'colorscale': 'Rainbow',
        'size': 4, 'opacity': 0.75,
        'line': {'width': 0}
    },
    showlegend=False
)

dispB = Scatter(
    x=x, y=y, name='bolhas', mode='markers',
    marker={
        'color': c, 'colorscale': 'Rainbow',
        'size': s, 'opacity': 0.25,
        'line': {'width': 0}
    },
    showlegend=False
)
In [6]:
%%time
fig = make_subplots(
    rows=1, cols=2, print_grid=False,
    subplot_titles=['Dispersão', 'Dispersão em Bolha']
)

fig.append_trace(dispA, 1, 1)
fig.append_trace(dispB, 1, 2)

fig.show()
Wall time: 31.3 ms

Barra¶


  • Barras [bar chart]: Visualização de gráfico em barras agrupados paralelamente.
  • Barras empilhadas [bar chart stacked]: Visualização de gráfico em barras agrupados de forma empilhada.
In [7]:
# Dados para barras
N = 8

# média
Hx = np.random.randint(18, 65, size=N)
Mx = np.random.randint(18, 65, size=N)

# desvio padrão
Hs = np.random.randint(1, 5, size=N)
Ms = np.random.randint(1, 5, size=N)

igrupos = ['G{}'.format(g + 1) for g in range(N)]

barrHg = Bar(
    x=igrupos, y=Hx, name='Homens',
    marker={'color': 'cyan'},
    error_y={'array': Hs}
)

barrMg = Bar(
    x=igrupos, y=Mx, name='Mulheres',
    marker={'color': 'red'},
    error_y={'array': Ms}
)

barrHs = Bar(
    x=igrupos, y=Hx,
    marker={'color': 'cyan'},
    error_y={'array': Hs},
    showlegend=False
)

barrMs = Bar(
    x=igrupos, y=Mx,
    hovertext=Mx,
    offset=-0.4, base=Hx,
    marker={'color': 'red'},
    error_y={'array': Ms},
    showlegend=False
)
In [8]:
%%time
fig = make_subplots(
    rows=1, cols=2,
    print_grid=False,
    subplot_titles=['Barras', 'Barras empilhadas']
)

fig.append_trace(barrHg, 1, 1)
fig.append_trace(barrMg, 1, 1)
fig.append_trace(barrMs, 1, 2)
fig.append_trace(barrHs, 1, 2)

fig.show()
Wall time: 25 ms