Here are some possibilies:
import plotly.plotly as py
import numpy as np
# some parametric eqution
def F(t, R=1):
return R*np.cos(t), R*np.sin(t)
t = np.linspace(0, 2*np.pi, 100)
x, y = F(t)
trace = dict(
x=x,
y=y,
text=t
)
layout = dict(
autosize=False,
width=500,
height=500,
hovermode='closest'
)
fig = dict(data=[trace], layout=layout)
py.iplot(fig, filename='parametric-curve')
def condition(xi, yi):
return xi * yi >= 0 # or some condition in the complex plane
def separate_traces_by_condition(condition, x, y):
data = []
state = condition(x[0], y[0])
trace = dict(x=[x[0]], y=[y[0]])
for xi, yi in zip(x, y)[1:]:
state_at_i = condition(xi, yi)
if state is state_at_i:
trace['x'].append(xi)
trace['y'].append(yi)
else:
data.append(trace)
trace = dict(x=[], y=[])
state = state_at_i
data.append(trace)
return data
t = np.linspace(0, 2*np.pi, 1000)
x, y = F(t)
data = separate_traces_by_condition(condition, x, y)
len(data) # 4 traces, each with a different color
4
layout = dict(
autosize=False,
width=500,
height=500,
hovermode='closest'
)
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='parametric-curve-2')
t = np.linspace(0, 2*np.pi, 500)
x, y = F(t)
trace = dict(
mode='markers', # line plot can't have a colorscale
x=x,
y=y,
marker=dict(
color=t,
showscale=True
)
)
layout = dict(
autosize=False,
width=500,
height=500,
hovermode='closest'
)
fig = dict(data=[trace], layout=layout)
py.iplot(fig, validate=False, filename='parametric-curve-3')
# N.B. please enter validate=False as
# marker colorscale is not part of our validation system yet
About Plotly
Big thanks to
from IPython.display import display, HTML
import urllib2
url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'
display(HTML(urllib2.urlopen(url).read()))