Skip to content

Instantly share code, notes, and snippets.

@gj84
Created August 1, 2013 12:40
Show Gist options
  • Save gj84/6130978 to your computer and use it in GitHub Desktop.
Save gj84/6130978 to your computer and use it in GitHub Desktop.
Plot.ly example
#plot.ly Example
#
#This example plot
#https://plot.ly/~GermanJimenez/0/
import plotly
import os
def collatzDropTime(n):
c = 0
while n != 1:
if n % 2 == 0: n /= 2
else: n = ( 3 * n ) + 1
c += 1
return c
def average(l):
return sum(l)/len(l)
limit = 2000
#Collatz algorithm droping times-------------------------------------
#Coordinates
collatzTrace = {
'x': range( 1, limit ),
'y': [collatzDropTime( n ) for n in range(1,limit)]
}
#Styles
collatzTraceStyles = {
"type":"scatter",
"name":"Collatz droping times",
"line":{"color":"orange",
"width":2,
"dash":"solid",
"opacity":0.2
}
}
#Collatz algorithm droping times averages-----------------------------------
rang = limit/100
la = [collatzTrace['y'][x:x+rang] for x in range(0, len(collatzTrace['y']), rang)]
xs = range(1,limit+2,rang)
ys = [average(l) for l in la]
ys.append(ys[len(ys)-1])
#Coordinates
collatzAverages = {
'x': xs,
'y': ys,
}
#Styles
collatzAveragesStyles = {
"type":"scatter",
"name":"Droping times averages (%s)" % rang,
"line":{"opacity": 0.3,
"color":"gray",
"width":3,
"dash":"solid"
},
}
#-------------------------------------------------------------------------------
#response = plotly.signup(username, email) #Signup
#loggin
username = 'MyUserName'
email='MyEmail'
key = 'MyKey'
py = plotly.plotly(username, key)
response = py.plot(collatzTrace,collatzAverages)
py.style(collatzTraceStyles,collatzAveragesStyles)
url = response['url']
filename = response['filename']
if url:
#os.system('firefox %s' % url)
os.system('explorer %s' % url)
#os.system('google-chrome %s' % url)
#This example plot
#https://plot.ly/~GermanJimenez/0/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment