# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Gráficos con PlotLy

# +
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tls

tls.set_credentials_file(username="DiegoGV95", api_key="<KEY>")
# -

import plotly
plotly.__version__

help(plotly)

import numpy as np
help(np.random)

# # Scatter Plots sencillos

n = 2000
random_x = np.random.randn(n)
random_y = np.random.randn(n)

trace = go.Scatter(x = random_x, y = random_y, mode = "markers")

py.iplot([trace], filename="basic-scatter")

plt_url = py.plot([trace], filename="basic-scatter-inline")

# # Gráficos combinados

N = 200
rand_x = np.linspace(0,1,N) #200 puntos equidistantes entre 0 y 1 
rand_y0 = np.random.randn(N) + 3
rand_y1 = np.random.randn(N)
rand_y2 = np.random.randn(N)-3

trace0 = go.Scatter(x = rand_x, y = rand_y0, mode = "markers", name = "Puntos")
trace1 = go.Scatter(x = rand_x, y = rand_y1, mode = "lines", name = "Líneas")
trace2 = go.Scatter(x = rand_x, y = rand_y2, mode = "lines+markers", name = "Puntos y líneas")
data = [trace0, trace1, trace2]

py.iplot(data, filename = "scatter-line-plot")

# # Estilizado de gráficos

trace = go.Scatter(x = random_x, y = random_y, name = "Puntos de estilo guay", mode = "markers",
                  marker = dict(size=12, color="rgba(140, 20, 20, 0.8)", line=dict(width=2, color="rgb(10,10,10)")))

layout = dict(title = "Scatter Plot Estilizado", xaxis = dict(zeroline=False), yaxis = dict(zeroline=False))

fig = dict(data=[trace], layout = layout)
py.iplot(fig)

# +
trace = go.Scatter(x=random_x, y=random_y, name="Puntos de estilo guay", mode="markers",
                  marker=dict(size=8, color="rgba(10, 80, 220, 0.25)", line=dict(width=1, color="rgb(10,10,80)")))

fig = dict(data=[trace], layout = layout)
py.iplot(fig)

# +
trace = go.Histogram(x=random_x, name = "Histograma")

layout = dict(title = "Histograma")
fig = dict(data=[trace], layout=layout)
py.iplot(fig)
# -

# # Información al hacer Hover

import pandas as pd

data = pd.read_csv("../datasets/usa-population/usa_states_population.csv")
data.head()

data.shape

N = 53
c = ["hsl("+str(h) + ", 50%, 50%)" for h in np.linspace(0,360,N)]

# +
l = []
y = []

for i in range(int(N)):
    y.append((2000+i))
    trace0 = go.Scatter(
        x = data["Rank"], 
        y = data["Population"] + i*1e6, 
        mode = "markers", 
        marker = dict(size=14, line=dict(width=1), color=c[i], opacity=0.3),
        name = str(data["State"])
        )
        
    l.append(trace0)
# -

layout = go.Layout(
    title = "Población de los estados de USA",
    hovermode = "closest",
    xaxis = dict(title = "ID", ticklen = 5, zeroline = False, gridwidth = 2),
    yaxis = dict(title = "Población", ticklen = 5, gridwidth = 2),
    showlegend = False
)

fig = go.Figure(data = l, layout = layout)
py.iplot(fig)

trace = go.Scatter(y = np.random.randn(1000), 
                   mode = "markers", 
                   marker = dict(size=16, color=np.random.randn(1000), colorscale="Viridis",showscale=True)
                  )

py.iplot([trace])

# # Datasets muy grandes

N = 100000
trace = go.Scattergl(x = np.random.randn(N), 
                     y = np.random.randn(N), 
                     mode = "markers", 
                     marker = dict(color="#BAD5FF", line=dict(width=1))) #Tono azulado

py.iplot([trace], filename="basic-scatter-inline")



StarcoderdataJupyterScriptsDedupFiltered