Skip to content

Streaming circle shapes graph #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Dec 31, 2015 · 6 comments
Closed

Streaming circle shapes graph #268

ghost opened this issue Dec 31, 2015 · 6 comments

Comments

@ghost
Copy link

ghost commented Dec 31, 2015

Hi - is there a streaming example for a circle shape plot? In this example : https://plot.ly/python/shapes/#circle

trace0 = go.Scatter(
x=[1.5, 3.5],
y=[0.75, 2.5],
text=['Unfilled
Circle', 'Filled
Circle'],
mode='text',
)

How do I declare this for streaming? Will x and y be 2 dimensional arrays? For a line graph I normally declare like this:
trace_ctr = Scatter(
x=[],
y=[]
)
How do I do it for a circle?

@theengineear
Copy link
Contributor

@cloaked123, here's a full, working example of a stream with shapes (circles in this case):

import random
import time

import colorlover as cl

import plotly.plotly as py
import plotly.graph_objs as go

# Define a couple functions to make the important bits more readable
def get_shape(color, type='circle'):
    x_center = random.random() * 10
    y_center = random.random() * 10
    dx = random.random() * 2
    dy = random.random() * 2
    x0 = x_center - dx/2.0
    y0 = y_center - dy/2.0
    x1 = x_center + dx/2.0
    y1 = y_center + dy/2.0
    return {'type': type, 'xref': 'x', 'yref': 'y', 'fillcolor': color,
            'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1}

def move_shape(shape):
    dx = shape['x1'] - shape['x0']
    dy = shape['y1'] - shape['y0']
    x_center = (shape['x1'] + shape['x0']) / 2
    y_center = (shape['y1'] + shape['y0']) / 2
    x_center += (random.random() - .5) / 4
    y_center += (random.random() - .5) / 4
    dx += (random.random() - .5) / 4
    dy += (random.random() - .5) / 4
    shape['x0'] = x_center - dx/2
    shape['y0'] = y_center - dy/2
    shape['x1'] = x_center + dx/2
    shape['y1'] = y_center + dy/2

# Make the initial plot.
token = py.get_credentials()['stream_ids'][0]
num_shapes = 50
mod = 10
colors = cl.scales[str(mod)]['div']['RdYlBu']

# Traces hold stream information. In this case, we *at least*
# need a place holder trace to hold the streaming target.
trace0 = go.Scatter(stream={'token': token})

# The layout holds the shapes, so we dump all the shape information there.
# We'll update the layout via streaming later.
layout = {'width': 800, 'height': 800, 'autosize': False,
          'xaxis': {'range': [-5, 15]}, 'yaxis': {'range': [-5, 15]},
          'shapes': [get_shape(colors[i % mod]) for i in range(num_shapes)]}

fig = {'data': [trace0], 'layout': layout}
print py.plot(fig, filename='messin', auto_open=False)

# This is the important part. You can use the `layout` kwarg in the call
# to Stream.write(). Note that you *need* to send along an empty trace or
# the Python client will think you made a mistake.
s = py.Stream(token)
s.open()
for _ in range(500):
    for shape in layout['shapes']:
        move_shape(shape)
    s.write({}, layout=layout)
    time.sleep(.1)
s.close()

Helpful? The gist is that you need to use the layout kwarg in the Stream.write() function.

@ghost
Copy link
Author

ghost commented Jan 1, 2016

Wow this is awesome. Thanks a lot! 

Sent from my iPhone

On Thu, Dec 31, 2015 at 12:44 PM -0800, "Andrew" [email protected] wrote:

@cloaked123, here's a full, working example of a stream with shapes (circles in this case):

import random
import time

import colorlover as cl

import plotly.plotly as py
import plotly.graph_objs as go

Define a couple functions to make the important bits more readable

def get_shape(color, type='circle'):
x_center = random.random() * 10
y_center = random.random() * 10
dx = random.random() * 2
dy = random.random() * 2
x0 = x_center - dx/2.0
y0 = y_center - dy/2.0
x1 = x_center + dx/2.0
y1 = y_center + dy/2.0
return {'type': type, 'xref': 'x', 'yref': 'y', 'fillcolor': color,
'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1}

def move_shape(shape):
dx = shape['x1'] - shape['x0']
dy = shape['y1'] - shape['y0']
x_center = (shape['x1'] + shape['x0']) / 2
y_center = (shape['y1'] + shape['y0']) / 2
x_center += (random.random() - .5) / 4
y_center += (random.random() - .5) / 4
dx += (random.random() - .5) / 4
dy += (random.random() - .5) / 4
shape['x0'] = x_center - dx/2
shape['y0'] = y_center - dy/2
shape['x1'] = x_center + dx/2
shape['y1'] = y_center + dy/2

Make the initial plot.

token = py.get_credentials()['stream_ids'][0]
num_shapes = 50
mod = 10
colors = cl.scales[str(mod)]['div']['RdYlBu']

Traces hold stream information. In this case, we at least

need a place holder trace to hold the streaming target.

trace0 = go.Scatter(stream={'token': token})

The layout holds the shapes, so we dump all the shape information there.

We'll update the layout via streaming later.

layout = {'width': 800, 'height': 800, 'autosize': False,
'xaxis': {'range': [-5, 15]}, 'yaxis': {'range': [-5, 15]},
'shapes': [get_shape(colors[i % mod]) for i in range(num_shapes)]}

fig = {'data': [trace0], 'layout': layout}
print py.plot(fig, filename='messin', auto_open=False)

This is the important part. You can use the layout kwarg in the call

to Stream.write(). Note that you need to send along an empty trace or

the Python client will think you made a mistake.

s = py.Stream(token)
s.open()
for _ in range(500):
for shape in layout['shapes']:
move_shape(shape)
s.write({}, layout=layout)
time.sleep(.1)
s.close()

Helpful? The gist is that you need to use the layout kwarg in the Stream.write() function.


Reply to this email directly or view it on GitHub.

@ghost
Copy link
Author

ghost commented Jan 6, 2016

Hello -
Could you please help me with another issue:

In the following plot - how do I make the star marker appear above the
circle shapes instead of behind?

https://plot.ly/23/~cloaked/

Thanks
-Ravi

On Thu, Dec 31, 2015 at 3:44 PM, Andrew [email protected] wrote:

@cloaked123 https://github.com/cloaked123, here's a full, working
example of a stream with shapes (circles in this case):

import random
import time

import colorlover as cl

import plotly.plotly as py
import plotly.graph_objs as go

Define a couple functions to make the important bits more readable

def get_shape(color, type='circle'):
x_center = random.random() * 10
y_center = random.random() * 10
dx = random.random() * 2
dy = random.random() * 2
x0 = x_center - dx/2.0
y0 = y_center - dy/2.0
x1 = x_center + dx/2.0
y1 = y_center + dy/2.0
return {'type': type, 'xref': 'x', 'yref': 'y', 'fillcolor': color,
'x0': x0, 'x1': x1, 'y0': y0, 'y1': y1}

def move_shape(shape):
dx = shape['x1'] - shape['x0']
dy = shape['y1'] - shape['y0']
x_center = (shape['x1'] + shape['x0']) / 2
y_center = (shape['y1'] + shape['y0']) / 2
x_center += (random.random() - .5) / 4
y_center += (random.random() - .5) / 4
dx += (random.random() - .5) / 4
dy += (random.random() - .5) / 4
shape['x0'] = x_center - dx/2
shape['y0'] = y_center - dy/2
shape['x1'] = x_center + dx/2
shape['y1'] = y_center + dy/2

Make the initial plot.

token = py.get_credentials()['stream_ids'][0]
num_shapes = 50
mod = 10
colors = cl.scales[str(mod)]['div']['RdYlBu']

Traces hold stream information. In this case, we at least

need a place holder trace to hold the streaming target.

trace0 = go.Scatter(stream={'token': token})

The layout holds the shapes, so we dump all the shape information there.

We'll update the layout via streaming later.

layout = {'width': 800, 'height': 800, 'autosize': False,
'xaxis': {'range': [-5, 15]}, 'yaxis': {'range': [-5, 15]},
'shapes': [get_shape(colors[i % mod]) for i in range(num_shapes)]}

fig = {'data': [trace0], 'layout': layout}
print py.plot(fig, filename='messin', auto_open=False)

This is the important part. You can use the layout kwarg in the call

to Stream.write(). Note that you need to send along an empty trace or

the Python client will think you made a mistake.

s = py.Stream(token)
s.open()
for _ in range(500):
for shape in layout['shapes']:
move_shape(shape)
s.write({}, layout=layout)
time.sleep(.1)
s.close()

Helpful? The gist is that you need to use the layout kwarg in the
Stream.write() function.


Reply to this email directly or view it on GitHub
#268 (comment)
.

@theengineear
Copy link
Contributor

@cloaked123 , there's no current way to do that right now. There's an open issue for it in the plotly.js repo though:

plotly/plotly.js#148

@theengineear
Copy link
Contributor

Closing this down as it's not related to the python api at this point.

@ghost
Copy link
Author

ghost commented Jan 6, 2016

Thanks

On Wed, Jan 6, 2016 at 2:00 PM, Andrew [email protected] wrote:

@cloaked123 https://github.com/cloaked123 , there's no current way to
do that right now. There's an open issue for it in the plotly.js repo
though:

plotly/plotly.js#148 plotly/plotly.js#148


Reply to this email directly or view it on GitHub
#268 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant