Ce notebook est une très courte introduction à plotly. Quelques liens utiles :
Important : Par défaut plotly communique avec ses serveurs pour proposer des
sercices collaboratifs et d'intégrations web. Pour éviter, si besoin, se passage
par les serveurs plotly, il faudra penser à utiliser le mode offline
.
In [1]:
import plotly
import plotly.graph_objs as go
import numpy as np
# mode offline
plotly.offline.init_notebook_mode()
In [2]:
def signal(t):
return np.sin(2 * t) * np.exp(-4e-2 * t)
def envelope(t):
return np.exp(-4e-2 * t)
In [3]:
t = np.linspace(0, 80, 200)
trace = go.Scatter(
x = t,
y = signal(t),
)
data = go.Data([trace])
plotly.offline.iplot(data)
plotly.plotly.image.save_as(data, "/Users/gvallver/git/python_sciences/plotly/graph1.png")
In [4]:
t = np.linspace(0, 80, 200)
trace = go.Scatter(
x = t,
y = signal(t)
)
envp = go.Scatter(
x = t,
y = envelope(t),
line = go.Line(
color = "red",
dash = "dash"
)
)
envm = go.Scatter(
x = t,
y = -envelope(t),
line = go.Line(
color = "red",
dash = "dash"
)
)
data = go.Data([trace, envp, envm])
plotly.offline.iplot(data)
plotly.plotly.image.save_as(data, "/Users/gvallver/git/python_sciences/plotly/graph2.png")
In [5]:
t = np.linspace(0, 80, 200)
trace = go.Scatter(
x = t,
y = signal(t)
)
envp = go.Scatter(
x = t,
y = envelope(t),
line = go.Line(
color = "red",
dash = "dash"
)
)
envm = go.Scatter(
x = t,
y = -envelope(t),
line = go.Line(
color = "red",
dash = "dash"
)
)
# on regroupe les traces dans l'objet Data
data = go.Data([trace, envp, envm])
# on s'occupe de la mise en forme globale via le Layout
layout = go.Layout(
title = "Ma super fonction amortie",
xaxis = go.XAxis(
title = "temps (s)"
),
yaxis = go.YAxis(
title = "Signal (ua)"
)
)
# la figure est un objet Data + un objet Layout
figure = go.Figure(data=data, layout=layout)
plotly.offline.iplot(figure)
plotly.plotly.image.save_as(figure, "/Users/gvallver/git/python_sciences/plotly/graph3.png")
In [6]:
t = np.linspace(0, 80, 200)
trace = go.Scatter(
x = t,
y = signal(t),
name = "signal" # nom de la trace
)
envp = go.Scatter(
x = t,
y = envelope(t),
name = "envelope", # nom de la trace
line = go.Line(
color = "red",
dash = "dash"
)
)
envm = go.Scatter(
x = t,
y = -envelope(t),
showlegend = False,
line = go.Line(
color = "red",
dash = "dash"
)
)
data = go.Data([trace, envp, envm])
layout = go.Layout(
title = "Ma super fonction amortie",
xaxis = go.XAxis(
title = "temps (s)"
),
yaxis = go.YAxis(
title = "Signal (ua)"
)
)
figure = go.Figure(data=data, layout=layout)
plotly.offline.iplot(figure)
plotly.plotly.image.save_as(figure, "/Users/gvallver/git/python_sciences/plotly/graph4.png")
In [7]:
t = np.linspace(0, 80, 200)
trace = go.Scatter(
x = t,
y = signal(t),
name = "signal"
)
envp = go.Scatter(
x = t,
y = envelope(t),
name = "envelope",
line = go.Line(
color = "red",
dash = "dash"
)
)
envm = go.Scatter(
x = t,
y = -envelope(t),
showlegend = False,
line = go.Line(
color = "red",
dash = "dash"
)
)
data = go.Data([trace, envp, envm])
layout = go.Layout(
title = "Ma super fonction amortie",
xaxis = go.XAxis(
title = "temps (s)",
range = [0, 40]
),
yaxis = go.YAxis(
title = "Signal (ua)",
range = [-1.2, 1.5]
)
)
figure = go.Figure(data=data, layout=layout)
plotly.offline.iplot(figure)
plotly.plotly.image.save_as(figure, "/Users/gvallver/git/python_sciences/plotly/graph5.png")
En conclusion : à vous de jouer et comme pour matplotlib, rendez-vous sur la la galerie.