Black Lives Matter. Please consider donating to Black Girls Code today.

Export Selection onClick Plotly Python

Hi everyone,

My goal is to allow the user to select a portion of the plot and then they can press a button to export that data into as a plot or a variable (vector) in Python. There is this topic using JavaScript ([Resolved] Export Selection onClick Plotly.js). I wished I could do the exactly same thing, but in Python.
For example

import plotly
import plotly.offline as offline
import plotly.graph_objs as go
import plotly.plotly as py
import numpy as np

offline.init_notebook_mode()

color_list = [‘rgb(244, 167, 66)’, ‘rgb(49,130,189)’,
‘rgb(244, 65, 86)’,‘rgb(100, 201, 137)’]

N = 100
random_x = np.linspace(1, 100, N)
random_y0 = np.random.randn(N)+5
random_y1 = np.random.randn(N)
random_y2 = np.random.randn(N)-5
random_y3 = np.random.randn(N)+10

random_y = [random_y0, random_y1, random_y2, random_y3]
data = []

for i in range(0,len(random_y)):
trace = go.Scatter(
x = random_x,
y = random_y[i],
mode = ‘lines+markers’,
name = random_y[i],
marker = dict(color=color_list[i])
)
data.append(trace)

layout = dict(
title=‘Time Series with Rangeslider’,
xaxis=dict(
rangeselector=dict(
buttons=list([
dict(count=1,
label=‘Zoon Out’,
step=‘all’,
stepmode=‘backward’),
])
),
rangeslider=dict(),
)
)

fig = dict(data=data, layout=layout)
py.iplot(fig, filename = “Time Series with Rangeslider”)

Could anyone help me?
Thank you!

1 Like