from lightning import Lightning

from numpy import random, asarray, sqrt, arctan2, pi, clip
from seaborn import color_palette
from sklearn import datasets
from colorsys import hsv_to_rgb

"""
Explanation: <img style='float: left' src="http://lightning-viz.org/images/logo.png"> <br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scatter plots in <a href='http://lightning-viz.org/'><font color='#9175f0'>Lightning</font></a>
<hr> Setup
End of explanation
"""


lgn = Lightning(ipython=True, host='http://public.lightning-viz.org')

"""
Explanation: Connect to server
End of explanation
"""


n = 100
x = random.randn(n)
y = random.randn(n)
lgn.scatter(x, y)

"""
Explanation: <hr> Random points with default styling
End of explanation
"""


n = 100
x = random.randn(n)
y = random.randn(n)
c = [145,117,240]
s = 18
lgn.scatter(x, y, color=c, size=s)

"""
Explanation: <hr> Random big purple points
Style options like color and size can be passed a single value, which affects all points
End of explanation
"""


n = 100
x = random.randn(n)
y = random.randn(n)
c = [asarray(color_palette('Blues', 100)[random.choice(range(100))])*255 for i in range(n)]
a = random.rand(n)
s = random.rand(n)*15+8
lgn.scatter(x, y, color=c, alpha=a, size=s)

"""
Explanation: <hr> Random points with all styling options
Style options can also be passed as lists with one value per point, either scalars (for alpha and size) or arrays (for color). 
<br>
In this example we also use seaborn's excellent color_palette tool to select our colors.
End of explanation
"""


d, g = datasets.make_blobs(n_features=2, n_samples=200, centers=5, cluster_std=2.0, random_state=100)
x = d[:, 0]
y = d[:, 1]
lgn.scatter(x, y, group=g, alpha=0.8, size=12)

"""
Explanation: <hr> Clustered points with group labels
Instead of specifying colors directly as rgb, you can specify labels (or group assignments).
<br>
Here we use scikitlearn to generate clusters and then color according to cluster label.
End of explanation
"""


n = 200
x = random.randn(n)
y = random.randn(n)
v = random.rand(n)
lgn.scatter(x, y, values=v, alpha=0.6, colormap='YlOrRd')

"""
Explanation: <hr> Color by value
We can color points by a numerical value by passing that value, and a Colorbrewer colormap.
End of explanation
"""


n = 200
x = random.randn(n)
y = random.randn(n)
r = map(lambda (x, y): sqrt(x ** 2 + y ** 2), zip(x,y))
t = map(lambda (x, y): arctan2(x, y), zip(x,y))
c = map(lambda (r, t): asarray(hsv_to_rgb(t / (2 * pi), r, 0.9))*255, zip(r, t))
s = asarray(r) * 10 + 2
lgn.scatter(x, y, color=c, size=s, alpha=0.6)

"""
Explanation: <hr> Fun with colors
We can use geometry to set colors based on point position and get a pretty result.
End of explanation
"""


x = random.randn(100)
y = random.randn(100)

lgn.scatter(x, y, xaxis='my axis label 1', yaxis='my axis label 2')

"""
Explanation: Axis labels
We can add axis labels by providing extra arguments
End of explanation
"""


