예제 #1
0
    def setup(self):
        my_creds = tls.get_credentials_file()  # read credentials
        py.sign_in(my_creds['username'],
                   my_creds['api_key'])  # (New syntax!) Plotly sign in
        tls.embed('streaming-demos', '6')

        my_stream_ids = tls.get_credentials_file()['stream_ids']

        # Get stream id from stream id list
        self.my_stream_id = my_stream_ids[0]

        # Make instance of stream id object
        my_stream = Stream(
            token=self.my_stream_id,  # N.B. link stream id to 'token' key
            maxpoints=200)  # N.B. keep a max of 80 pts on screen

        # Initialize trace of streaming plot by embedding the unique stream_id
        my_data = Data(
            [Scatter(x=[], y=[], mode='lines+markers',
                     stream=my_stream)])  # embed stream id, 1 per trace

        # Add title to layout object
        my_layout = Layout(title='Quake monitor')

        # Make instance of figure object
        my_fig = Figure(data=my_data, layout=my_layout)

        # Initialize streaming plot, open new tab
        unique_url = py.plot(my_fig, filename='qm_first-stream')
    def setup(self):
        my_creds = tls.get_credentials_file()                  # read credentials
        py.sign_in(my_creds['username'], my_creds['api_key'])  # (New syntax!) Plotly sign in
        tls.embed('streaming-demos','6')

        my_stream_ids = tls.get_credentials_file()['stream_ids']

        # Get stream id from stream id list 
        self.my_stream_id = my_stream_ids[0]

        # Make instance of stream id object 
        my_stream = Stream(token=self.my_stream_id,  # N.B. link stream id to 'token' key
                           maxpoints=200)        # N.B. keep a max of 80 pts on screen


        # Initialize trace of streaming plot by embedding the unique stream_id
        my_data = Data([Scatter(x=[],
                                y=[],
                                mode='lines+markers',
                                stream=my_stream)]) # embed stream id, 1 per trace

        # Add title to layout object
        my_layout = Layout(title='Quake monitor')

        # Make instance of figure object
        my_fig = Figure(data=my_data, layout=my_layout)

        # Initialize streaming plot, open new tab
        unique_url = py.plot(my_fig, filename='qm_first-stream')
    def on_data(self, data):
        pprint(json.loads(data))
        global initime
        t = int(calctime(initime))

        all_data = json.loads(data)
        sentitweets = all_data['text'].encode("utf-8")
        sentitweets = " ".join(re.findall("[a-zA-Z]+", sentitweets))
        blob = TextBlob(sentitweets.strip())

        global positive
        global negative
        global compound
        global count

        count = count + 1
        senti = 0
        for sen in blob.sentences:
            senti = senti + sen.sentiment.polarity
            if sen.sentiment.polarity >= 0:
                positive = positive + sen.sentiment.polarity
            else:
                negative = negative + sen.sentiment.polarity
        compound = compound + senti
        print count
        print sentitweets.strip()
        print senti
        print t
        print str(positive) + '' + str(negative) + '' + str(compound)

        #data plot
        #x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        #y0 = positive
        y1 = compound
        y2 = negative

        stream_id = stream_ids[0]
        py.plot(
            Data([
                Scatter(
                    x=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'),
                    y=positive,
                    stream=Stream(token=stream_id, maxpoints=100))
            ]))

        stream = py.Stream(stream_id)
        stream.open()
        stream.write(dict(x=1, y=1))
        stream.close()

        tls.embed('streaming-demo', '12')
예제 #4
0
def iplot(figure_or_data, **plot_options):
    """Create a unique url for this plot in Plotly and open in IPython.

    plot_options keyword agruments:
    filename (string) -- the name that will be associated with this figure
    fileopt ('new' | 'overwrite' | 'extend' | 'append') -- 'new' creates a
        'new': create a new, unique url for this plot
        'overwrite': overwrite the file associated with `filename` with this
        'extend': add additional numbers (data) to existing traces
        'append': add additional traces to existing data lists
    world_readable (default=True) -- make this figure private/public

    """
    if 'auto_open' not in plot_options:
        plot_options['auto_open'] = False
    res = plot(figure_or_data, **plot_options)
    urlsplit = res.split('/')
    username, plot_id = urlsplit[-2][1:], urlsplit[-1]  # TODO: HACKY!

    embed_options = dict()
    if 'width' in plot_options:
        embed_options['width'] = plot_options['width']
    if 'height' in plot_options:
        embed_options['height'] = plot_options['height']

    return tools.embed(username, plot_id, **embed_options)
예제 #5
0
def iplot(figure_or_data, **plot_options):
    """Create a unique url for this plot in Plotly and open in IPython.

    plot_options keyword agruments:
    filename (string) -- the name that will be associated with this figure
    fileopt ('new' | 'overwrite' | 'extend' | 'append') -- 'new' creates a
        'new': create a new, unique url for this plot
        'overwrite': overwrite the file associated with `filename` with this
        'extend': add additional numbers (data) to existing traces
        'append': add additional traces to existing data lists
    world_readable (default=True) -- make this figure private/public

    """
    if 'auto_open' not in plot_options:
        plot_options['auto_open'] = False
    res = plot(figure_or_data, **plot_options)
    urlsplit = res.split('/')
    username, plot_id = urlsplit[-2][1:], urlsplit[-1]  # TODO: HACKY!

    embed_options = dict()
    if 'width' in plot_options:
        embed_options['width'] = plot_options['width']
    if 'height' in plot_options:
        embed_options['height'] = plot_options['height']

    return tools.embed(username, plot_id, **embed_options)
예제 #6
0
 def setUp(self):
     plot_info = {"un": "PlotlyImageTest", "fid": "2"}
     url = "https://plot.ly/~{un}/{fid}".format(**plot_info)
     self.display_obj = tls.embed(url)
     self.results = {}
     self.images = {}
     self.threads = []
     self.format_to_func = {
         "jpeg": self.jpeg_worker,
         "png": self.png_worker,
         "svg": self.svg_worker,
         "pdf": self.pdf_worker}
# see http://moderndata.plot.ly/color-scales-in-ipython-notebook/ for details on colorlover package
# which allows you to set scales
fig = Figure(data=event_plot.convert_df_plotly(conservative_wins_spot, gradcolor='Blues', addmarker=False),
             layout=event_plot.create_layout(title, xaxis, yaxis),
)

plot_url = py.iplot(fig, filename='conservative-wins-' + ticker)


# Embed the chart into the document using "embed". This essentially embeds the Javascript code, necessary to make it interactive.

# In[13]:

import plotly.tools as tls

tls.embed("https://plot.ly/~thalesians/245")


# Our final plot, will consist of three subplots, Labour wins, Conservative wins, and average moves for both. We also add a grid and a grey background for each plot.

# In[14]:

##### Plot market reaction during Conservative UK election wins
##### create a plot consisting of 3 subplots (from left to right)
##### 1. Labour wins, 2. Conservative wins, 3. Conservative/Labour mean move

# create a dataframe which grabs the mean from the respective Lab & Con election wins
mean_wins_spot = pandas.DataFrame()
mean_wins_spot['Labour Mean'] = labour_wins_spot['Labour Mean']
mean_wins_spot['Conservative Mean'] = conservative_wins_spot['Conservative Mean']
    summary_2012 = summary_departure_2012()
    summary_2013 = summary_departure_2013()
    summary_2014 = summary_departure_2014()
    month = month_list()
    average_dpt = []
    temporary = 0
    for run in month:
        temporary = 0
        temporary = (int(summary_2012[run]) + int(summary_2013[run]) + int(summary_2014[run])) // 3
        average_dpt.append(temporary)
    return average_dpt


def chart_average_departure():
    "Create Chart Amount Average Departure International Passenger Years 2012-2014"
    average_dpt = average_departure()
    month = month_list()
    trace0 = go.Bar(
        x=[run for run in month],
        y=[run for run in average_dpt],
        marker=dict(color="rgb(158,202,225)", line=dict(color="rgb(8,48,107)", width=1.5)),
        opacity=0.6,
    )
    data = [trace0]
    layout = go.Layout(title="Average International Passengers Departure per Month 2012-2014")
    fig = go.Figure(data=data, layout=layout)
    return py.plot(fig, filename="Average International Passengers Departure per Month 2012-2014")


tls.embed(chart_average_departure())
예제 #9
0
# ##### Data Analysis of 8.2 Million Rows with Python and SQLite
# 
# This notebook explores a 3.9Gb CSV file containing NYC's 311 complaints since 2003. It's the most popular data set in [NYC's open data portal](https://nycopendata.socrata.com/data).
# 
# This notebook is a primer on out-of-memory data analysis with
# - [pandas](http://pandas.pydata.org/): A library with easy-to-use data structures and data analysis tools. Also, interfaces to out-of-memory databases like SQLite.
# - [IPython notebook](ipython.org/notebook.html): An interface for writing and sharing python code, text, and plots.
# - [SQLite](https://www.sqlite.org/): An self-contained, server-less database that's easy to set-up and query from Pandas.
# - [Plotly](https://plot.ly/python/): A platform for publishing beautiful, interactive graphs from Python to the web.
# 
# The dataset is too large to load into a Pandas dataframe. So, instead we'll perform out-of-memory aggregations with SQLite and load the result directly into a dataframe with Panda's `iotools`. It's pretty easy to stream a CSV into SQLite and SQLite requires no setup. The SQL query language is pretty intuitive coming from a Pandas mindset.

# In[1]:

import plotly.tools as tls
tls.embed('https://plot.ly/~chris/7365')


# In[2]:

import pandas as pd
from sqlalchemy import create_engine # database connection
import datetime as dt
from IPython.display import display

import plotly.plotly as py # interactive graphing
from plotly.graph_objs import Bar, Scatter, Marker, Layout 


# #### Import the CSV data into SQLite
# 
예제 #10
0
#this module

import plotly
import re
import pandas as pd
import plotly.plotly as py
plotly.tools.set_credentials_file(username='******',
                                  api_key='D2EsU928WZxYqs9NRPdF')
plotly.tools.set_config_file(world_readable=True, sharing='public')
import plotly.graph_objs as go
import plotly.tools as tls
import cufflinks as cf
tls.embed('https://plot.ly/~cufflinks/8')

import getData


def vis(df, date, returns):
    print(plotly.__version__)
    spendingcategory = date
    amount = returns
    spendingcategory = [
        'Boat supply', 'eTransfer - Vacation Property rental Total',
        'Hotels & Motels Total', 'Retail - Books & Household items Total',
        'Services - Marketing Total'
    ]
    amount = [307.47, 45000, 2774.79, 18867.93, 4234.78]

    trace = go.Pie(labels=spendingcategory, values=amount)
    py.iplot([trace], filename='batman_pie_chart')
예제 #11
0
sorted_mails = sorted(mbox, key=extract_date)
mbox.update(enumerate(sorted_mails))
mbox.flush()

# Organize the dates of email receipt as a list
all_dates = []
mbox = mailbox.mbox(path)
for message in mbox:
    all_dates.append( str( parse( message['date'] ) ).split(' ')[0] )

# Count the emails
email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]

# Output the first element
email_count[0]

# Graph the data using plotly
x = []
y = []
for date, count in email_count:
    x.append(date)
    y.append(count)

py.iplot( Data([ Scatter ( x=x, y=y )]) )

# Restyling of the graph
import plotly.tools as tls

tls.embed('https://plot.ly/~kaz/65')
                "values": [cities_2012[run][1] for run in range(len(cities_2012) - 2, len(cities_2012) - 12, -1)],
                "labels": [cities_2012[run][0] for run in range(len(cities_2012) - 2, len(cities_2012) - 12, -1)],
                "domain": {"x": [0, 0.48]},
                "name": "Arrive",
                "hoverinfo": "label+value+name",
                "hole": 0.4,
                "type": "pie",
            },
            {
                "values": [cities_2013[run][1] for run in range(len(cities_2013) - 2, len(cities_2013) - 12, -1)],
                "labels": [cities_2013[run][0] for run in range(len(cities_2013) - 2, len(cities_2013) - 12, -1)],
                "domain": {"x": [0.52, 1]},
                "name": "Departure",
                "hoverinfo": "label+value+name",
                "hole": 0.4,
                "type": "pie",
            },
        ],
        "layout": {
            "title": "Top International Cities Arrive/Departure 2012-2014",
            "annotations": [
                {"font": {"size": 20}, "showarrow": False, "text": "Arrive", "x": 0.2, "y": 0.5},
                {"font": {"size": 20}, "showarrow": False, "text": "Departure", "x": 0.83, "y": 0.5},
            ],
        },
    }
    return py.plot(fig, filename="Top International Cities Air Traffic 2012-2013")


tls.embed(chart_top_cities_arv_dpt())
예제 #13
0
layout = dict(
    title = 'World Wide Alcohol Consumption',
    geo = dict(
        showframe = False,
        showcoastlines = False,
        projection = dict(
            type = 'Mercator'
        )
    )
)

fig = dict( data=data, layout=layout )
#py.plot( fig, validate=False, filename='d3-world-map' )

# embed the graph
import plotly.tools as tls
tls.embed("https://plot.ly/~pauluck/0")




# ### Conclusion

# #### The alcohol consumption is highest in Australia, also in few European and African countries.  The lowest consumption is in northern region of Africa.  The consumption in North and South America remains average.  The favorite alcohol is beer in America and around the world!

# In[ ]:



    for run in month:
        temporary = 0
        temporary = (int(summary_2012[run]) + int(summary_2013[run]) + int(summary_2014[run]))//3
        average_arv.append(temporary)
    return average_arv

def chart_average_arrive():
    "Create Chart Amount Average Arrive International Passenger Years 2012-2014"
    average_arv = average_arrive()
    month = month_list()
    trace0 = go.Bar(
        x=[run for run in month],
        y=[run for run in average_arv],
        marker=dict(
            color='rgb(158,202,225)',
            line=dict(
                color='rgb(8,48,107)',
                width=1.5,
            )
        ),
        opacity=0.6
    )
    data = [trace0]
    layout = go.Layout(
        title='Average International Passengers Arrive per Month 2012-2014',
    )
    fig = go.Figure(data=data, layout=layout)
    return py.plot(fig, filename='Average International Passengers Arrive per Month 2012-2014')

tls.embed(chart_average_arrive())
예제 #15
0
fig = Figure(
    data=event_plot.convert_df_plotly(conservative_wins_spot,
                                      gradcolor='Blues',
                                      addmarker=False),
    layout=event_plot.create_layout(title, xaxis, yaxis),
)

plot_url = py.iplot(fig, filename='conservative-wins-' + ticker)

# Embed the chart into the document using "embed". This essentially embeds the Javascript code, necessary to make it interactive.

# In[13]:

import plotly.tools as tls

tls.embed("https://plot.ly/~thalesians/245")

# Our final plot, will consist of three subplots, Labour wins, Conservative wins, and average moves for both. We also add a grid and a grey background for each plot.

# In[14]:

##### Plot market reaction during Conservative UK election wins
##### create a plot consisting of 3 subplots (from left to right)
##### 1. Labour wins, 2. Conservative wins, 3. Conservative/Labour mean move

# create a dataframe which grabs the mean from the respective Lab & Con election wins
mean_wins_spot = pandas.DataFrame()
mean_wins_spot['Labour Mean'] = labour_wins_spot['Labour Mean']
mean_wins_spot['Conservative Mean'] = conservative_wins_spot[
    'Conservative Mean']
예제 #16
0
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import random, requests

# Cluster analysis
from sklearn.cluster import KMeans  # linear methods
from sklearn.cluster import SpectralClustering  # nearest neighbors
from sklearn.datasets import make_moons
from sklearn.decomposition import PCA  # path collection
from sklearn.metrics import pairwise_distances  # for K means

import plotly.offline as py  # import plotly in offline mode
py.init_notebook_mode(connected=True)  # initialize the offline mode, with access to the internet or not.
import plotly.tools as tls
tls.embed('https://plot.ly/~cufflinks/8')  # conda install -c conda-forge cufflinks-py
# import cufflinks and make it offline
import cufflinks as cf
cf.go_offline()  # initialize cufflinks in offline mode

# First time you run plotly
# import plotly
# username = '******' # your.username
# api_key = '••••••••••' # find it under settings # your.apikey
# plotly.tools.set_credentials_file(username=username, api_key=api_key)

%matplotlib inline

# Data
sns.set(style="ticks")  # for scatterplot
df = sns.load_dataset("iris")
예제 #17
0
email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]


# In[26]:

email_count[0]


# In[27]:

x = []
y = []
for date, count in email_count:
    x.append(date)
    y.append(count)


# In[28]:

py.iplot( Data([ Scatter( x=x, y=y ) ]) )


# ##### Restyle the chart in Plotly's GUI

# In[10]:

import plotly.tools as tls
tls.embed('https://plot.ly/~jackp/3266')

예제 #18
0
# ##### Data Analysis of 8.2 Million Rows with Python and SQLite
#
# This notebook explores a 3.9Gb CSV file containing NYC's 311 complaints since 2003. It's the most popular data set in [NYC's open data portal](https://nycopendata.socrata.com/data).
#
# This notebook is a primer on out-of-memory data analysis with
# - [pandas](http://pandas.pydata.org/): A library with easy-to-use data structures and data analysis tools. Also, interfaces to out-of-memory databases like SQLite.
# - [IPython notebook](ipython.org/notebook.html): An interface for writing and sharing python code, text, and plots.
# - [SQLite](https://www.sqlite.org/): An self-contained, server-less database that's easy to set-up and query from Pandas.
# - [Plotly](https://plot.ly/python/): A platform for publishing beautiful, interactive graphs from Python to the web.
#
# The dataset is too large to load into a Pandas dataframe. So, instead we'll perform out-of-memory aggregations with SQLite and load the result directly into a dataframe with Panda's `iotools`. It's pretty easy to stream a CSV into SQLite and SQLite requires no setup. The SQL query language is pretty intuitive coming from a Pandas mindset.

# In[1]:

import plotly.tools as tls
tls.embed('https://plot.ly/~chris/7365')

# In[2]:

import pandas as pd
from sqlalchemy import create_engine  # database connection
import datetime as dt
from IPython.display import display

import plotly.plotly as py  # interactive graphing
from plotly.graph_objs import Bar, Scatter, Marker, Layout

# #### Import the CSV data into SQLite
#
# 1. Load the CSV, chunk-by-chunk, into a DataFrame
# 2. Process the data a bit, strip out uninteresting columns
예제 #19
0
import pandas as pd
import requests, json
import plotly.offline as py # import plotly in offline mode
py.init_notebook_mode(connected=True) # initialize the offline mode, with access to the internet or not.
import plotly.tools as tls
tls.embed('https://plot.ly/~cufflinks/8') # embed cufflinks.
# import cufflinks and make it offline
import cufflinks as cf
cf.go_offline() # initialize cufflinks in offline mode

# find the data using request at https://raw.githubusercontent.com/snorreralund/sds_dump/tsne_name_embedding
# df = pd.read_csv('tsne_name_embedding.csv')

#########################################
#   Scraping names from ankestyrelsen   #
#########################################
import pandas as pd
# links found looking here: https://ast.dk/born-familie/navne/navnelister/godkendte-fornavne
urls = {'female': 'https://ast.dk/_namesdb/export/names?format=xls&gendermask=1',
        'male': 'https://ast.dk/_namesdb/export/names?format=xls&gendermask=2',
        'unisex': 'https://ast.dk/_namesdb/export/names?format=xls&gendermask=3'}
dfs = {}
for name, url in urls.items():
    df = pd.read_excel(url,header)
    dfs[name] = df

# scrape ethnicity:
# Link located by looking in the network monitor for activity on this page:
# https://ast.dk/born-familie/navne/navnelister/udenlandske-navne
import requests
url = 'https://ast.dk/_namesdb/namecultures?published=1&page=1&pagesize=270'
예제 #20
0
dat = WDI(indicator='NY.GNP.PCAP.CD', country=c('CL','HU','UY'), start=1960, end=2012)
 
#a quick plot with legend, title and label
 
wb <- ggplot(dat, aes(year, NY.GNP.PCAP.CD, color=country)) + geom_line() 
+     xlab('Year') + ylab('GDI per capita (Atlas Method USD)') 
+     labs(title <- "GNI Per Capita ($USD Atlas Method)")

py$ggplotly(wb)


# We can add `py$ggplotly` to the call, which will draw the figure with Plotly's [R API](https://plot.ly/r). Then we can call it in a Notebook. You can similarly call any Plotly graph with the username and graph id pair.

# In[9]:

tls.embed('RgraphingAPI', '1457')


# Note: the data is called from a WDI database; if you make it with Plotly, the data is stored with the plot. I forked the data and shared it: [https://plot.ly/~MattSundquist/1343](https://plot.ly/~MattSundquist/1343). 

# If you want to use Plotly's default graph look, you can edit the graph with Python.

# In[10]:

fig = py.get_figure('RgraphingAPI', '1457')
fig.strip_style()
py.iplot(fig)


# Often we come to a visualization with data rather than coming to data with a visualization. In that case, Plotly is useful for quick exploration, with matplotlib or Plotly's API.
예제 #21
0
                       titlefont=dict(color='rgb(255,10,0)'),
                       tickfont=dict(color='rgb(255,10,0)'),
                       overlaying='y',
                       side='right',
                       rangemode='tozero',
                   ))

fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename='multple-trace-axes-streaming')

s_1 = py.Stream(stream_id=token_1)
s_2 = py.Stream(stream_id=token_2)

s_1.open()
s_2.open()

i = 0
while True:
    x = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
    delta = np.random.random()
    y1 = np.sin(i * 0.125 * np.pi) + 1 + np.random.random()
    y2 = np.cos(i * 0.125 * np.pi) + 2 + np.random.random()
    s_1.write(dict(x=x, y=y1))
    s_2.write(dict(x=x, y=y2))
    time.sleep(1)
    i += 1
s_1.close()
s_2.close()

tls.embed('streaming-demos', '124')
예제 #22
0
def ukelection():
    # Learn about API authentication here: https://plot.ly/python/getting-started
    # Find your api_key here: https://plot.ly/settings/api
    plotly_username = "******"
    with open('plotly_key.txt', 'r') as plotly_key_file:
        plotly_api_key = plotly_key_file

    plotly.tools.set_credentials_file(username=plotly_username,
                                      api_key=plotly_api_key)

    ticker = 'GBPUSD'  # will use in plot titles later (and for creating Plotly URL)

    ##### download market GBP/USD data from Quandl, Bloomberg or CSV file
    #source = "Bloomberg"
    source = "Quandl"
    # source = "CSV"

    csv_file = None

    event_plot = EventPlot()

    data_downloader = DataDownloader()
    start_date = event_plot.parse_dates(['01/01/1975'])

    if source == 'Quandl':
        vendor_ticker = "BOE/XUDLUSS"
    elif source == 'Bloomberg':
        vendor_ticker = 'GBPUSD BGN Curncy'
    elif source == 'CSV':
        vendor_ticker = 'GBPUSD'
        csv_file = 'D:/GBPUSD.csv'

    spot = data_downloader.download_time_series(vendor_ticker,
                                                ticker,
                                                start_date[0],
                                                source,
                                                csv_file=csv_file)

    labour_wins = [
        '28/02/1974', '10/10/1974', '01/05/1997', '07/06/2001', '05/05/2005'
    ]
    conservative_wins = [
        '03/05/1979', '09/06/1983', '11/06/1987', '09/04/1992', '06/05/2010'
    ]

    # convert to more easily readable format
    labour_wins_d = event_plot.parse_dates(labour_wins)
    conservative_wins_d = event_plot.parse_dates(conservative_wins)

    # only takes those elections where we have data
    labour_wins_d = [
        d for d in labour_wins_d if d > spot.index[0].to_pydatetime()
    ]
    conservative_wins_d = [
        d for d in conservative_wins_d if d > spot.index[0].to_pydatetime()
    ]

    spot.index.name = 'Date'

    # number of days before and after for our event study
    pre = -20
    post = 20

    # calculate spot path during Labour wins
    labour_wins_spot = event_plot.event_study(spot,
                                              labour_wins_d,
                                              pre,
                                              post,
                                              mean_label='Labour Mean')

    # calculate spot path during Conservative wins
    conservative_wins_spot = event_plot.event_study(
        spot, conservative_wins_d, pre, post, mean_label='Conservative Mean')

    ##### Create separate plots of price action during Labour and Conservative wins
    xaxis = 'Days'
    yaxis = 'Index'
    source_label = "Source: @thalesians/BBG/Wikipedia"

    ###### Plot market reaction during Labour UK election wins
    ###### Using default color scheme

    title = ticker + ' during UK gen elect - Lab wins' + '<BR>' + source_label

    fig = Figure(data=event_plot.convert_df_plotly(labour_wins_spot),
                 layout=event_plot.create_layout(title, xaxis, yaxis))

    py.plot(fig, filename='labour-wins-' + ticker)

    ###### Plot market reaction during Conservative UK election wins
    ###### Using varying shades of blue for each line (helped by colorlover library)

    title = ticker + ' during UK gen elect - Con wins ' + '<BR>' + source_label

    # also apply graduated color scheme of blues (from light to dark)
    # see http://moderndata.plot.ly/color-scales-in-ipython-notebook/ for details on colorlover package
    # which allows you to set scales
    fig = Figure(
        data=event_plot.convert_df_plotly(conservative_wins_spot,
                                          gradcolor='Blues',
                                          addmarker=False),
        layout=event_plot.create_layout(title, xaxis, yaxis),
    )

    plot_url = py.plot(fig, filename='conservative-wins-' + ticker)

    import plotly.tools as tls

    tls.embed("https://plot.ly/~thalesians/245")

    ##### Plot market reaction during Conservative UK election wins
    ##### create a plot consisting of 3 subplots (from left to right)
    ##### 1. Labour wins, 2. Conservative wins, 3. Conservative/Labour mean move

    # create a dataframe which grabs the mean from the respective Lab & Con election wins
    mean_wins_spot = pandas.DataFrame()
    mean_wins_spot['Labour Mean'] = labour_wins_spot['Labour Mean']
    mean_wins_spot['Conservative Mean'] = conservative_wins_spot[
        'Conservative Mean']

    fig = plotly.tools.make_subplots(rows=1, cols=3)

    # apply different color scheme (red = Lab, blue = Con)
    # also add markets, which will have varying levels of opacity
    fig['data'] += Data(
        event_plot.convert_df_plotly(conservative_wins_spot,
                                     axis_no=1,
                                     color_def=['blue'],
                                     addmarker=True) +
        event_plot.convert_df_plotly(
            labour_wins_spot, axis_no=2, color_def=['red'], addmarker=True) +
        event_plot.convert_df_plotly(mean_wins_spot,
                                     axis_no=3,
                                     color_def=['red', 'blue'],
                                     addmarker=True,
                                     showlegend=False))

    fig['layout'].update(title=ticker +
                         ' during UK gen elects by winning party ' + '<BR>' +
                         source_label)

    # use the scheme from https://plot.ly/python/bubble-charts-tutorial/
    # can use dict approach, rather than specifying each separately
    axis_style = dict(
        gridcolor='#FFFFFF',  # white grid lines
        ticks='outside',  # draw ticks outside axes
        ticklen=8,  # tick length
        tickwidth=1.5  #   and width
    )

    # create the various axes for the three separate charts
    fig['layout'].update(
        xaxis1=plotly.graph_objs.XAxis(axis_style, title=xaxis))
    fig['layout'].update(
        yaxis1=plotly.graph_objs.YAxis(axis_style, title=yaxis))

    fig['layout'].update(
        xaxis2=plotly.graph_objs.XAxis(axis_style, title=xaxis))
    fig['layout'].update(yaxis2=plotly.graph_objs.YAxis(axis_style))

    fig['layout'].update(
        xaxis3=plotly.graph_objs.XAxis(axis_style, title=xaxis))
    fig['layout'].update(yaxis3=plotly.graph_objs.YAxis(axis_style))

    fig['layout'].update(plot_bgcolor='#EFECEA')  # set plot background to grey

    plot_url = py.iplot(fig,
                        filename='labour-conservative-wins-' + ticker +
                        '-subplot')

    import plotly.tools as tls

    tls.embed("https://plot.ly/~thalesians/246")
# - [Plotly](https://plot.ly/python/): A platform for publishing beautiful, interactive graphs from Python to the web.
# 
# ####In Short... How you can go from this:

# In[1]:

from IPython.display import Image
Image(filename='assets/prices.png', width = 700)


# ####To this: 

# In[2]:

import plotly.tools as tls
tls.embed('https://plot.ly/~otto.stegmaier/609/previous-min-and-max-prices/')


# In[3]:

Image(filename='assets/logo.png')


# ##### Why we are working on this:
# 
# At [Liftopia](http://www.liftopia.com/) we are working on bringing dynamic pricing into the ski industry. We help consumers ski more by offering tickets for purchase in advance at lower prices in exchange for their commitment. We help resorts control their pricing, drive more predictable revenue and grow their businesses. 
# 
# Since one of our core business channels is pricing and selling lift tickets for our resort partners, our analytics team needs to be able to communicate our pricing plans to our resort partners in a  simple, but effective manner. The ski areas we work with often offer tickets on 120 days of the year at upwards of 10 different price points on each day of the season. If you do the math - that can mean trying to communicate 1,200 different prices for one product. Some resorts offer over 10 different products. Now we are at 12,000 data points. Want to see the junior and child ticket pricing too? Now that's 36,000 data points.
# 
# In an effort to communicate our pricing plans more effecitvely - we decided to use [Plot.ly](Plot.ly) to help us build web based interactive visualizations we can share with our partners. 
# 
# - [xlwings](xlwings.org): A python library with tools to connect pandas to data stored in excel workbooks.
# - [Plotly](https://plot.ly/python/): A platform for publishing beautiful, interactive graphs from Python to the web.
#
# ####In Short... How you can go from this:

# In[1]:

from IPython.display import Image
Image(filename='assets/prices.png', width=700)

# ####To this:

# In[2]:

import plotly.tools as tls
tls.embed('https://plot.ly/~otto.stegmaier/609/previous-min-and-max-prices/')

# In[3]:

Image(filename='assets/logo.png')

# ##### Why we are working on this:
#
# At [Liftopia](http://www.liftopia.com/) we are working on bringing dynamic pricing into the ski industry. We help consumers ski more by offering tickets for purchase in advance at lower prices in exchange for their commitment. We help resorts control their pricing, drive more predictable revenue and grow their businesses.
#
# Since one of our core business channels is pricing and selling lift tickets for our resort partners, our analytics team needs to be able to communicate our pricing plans to our resort partners in a  simple, but effective manner. The ski areas we work with often offer tickets on 120 days of the year at upwards of 10 different price points on each day of the season. If you do the math - that can mean trying to communicate 1,200 different prices for one product. Some resorts offer over 10 different products. Now we are at 12,000 data points. Want to see the junior and child ticket pricing too? Now that's 36,000 data points.
#
# In an effort to communicate our pricing plans more effecitvely - we decided to use [Plot.ly](Plot.ly) to help us build web based interactive visualizations we can share with our partners.
#
# To do this we connected one of our pricing tools to a python script that interacts with Plotly's API.This notebook walks through a simplified version of that process. Note - the data used in this example is intended to show how we use Plotly from Excel - if you want to talk to us about our beliefs abour pricing - get in touch! ([email protected])
#
    pre_stat = []
    for i in range(len(name)):
        pre_stat.append(name[i])
        pre_stat.append(amount[i])
        stat.append(pre_stat)
        pre_stat = []
    return stat

def merge(stat_1, stat_2):
    """Return dic that merge form two dic"""
    stat = {}
    for i in list(stat_1):
        stat[i] = stat_1[i] + stat_2[i]
    return stat

def chart_top_cities_domestic():
    "Create Pie Chart Top Domestic Cities Arrive-Departure Passenger Years 2012-2014"
    top_cities_domes = top_cities()
    top_cities_domes = top_cities_domes[2]
    fig = {
        "data": [{
                "labels" : [top_cities_domes[run][0] for run in range(len(top_cities_domes)-2,len(top_cities_domes)-12,-1)],
                "values" : [top_cities_domes[run][1] for run in range(len(top_cities_domes)-2,len(top_cities_domes)-12,-1)],
                "type" : "pie"
                }],
        "layout" : {"title" : "Top Domestic Cities 2012-2014"}
    }
    return py.plot(fig, filename='Top Domestic Cities 2012-2014')

tls.embed(chart_top_cities_domestic())
# (*) To communicate with Plotly's server, sign in with credentials file

# (*) Useful Python/Plotly tools
import plotly.tools as tls

# (*) Graph objects to piece together plots

# Embed an existing Plotly streaming plot
tls.embed('streaming-demos', '6')

# Note that the time point correspond to internal clock of the servers,
# that is UTC time.
        temporary = (int(summary_2013[run]) + int(summary_2014[run]) +
                     int(summary_2015[run])) // 3
        average_dpt.append(temporary)
    return average_dpt


def chart_average_departure():
    "Create Chart Amount Average Departure International Passenger Years 2013-2015"
    average_dpt = average_departure()
    month = month_list()
    trace0 = go.Bar(x=[run for run in month],
                    y=[run for run in average_dpt],
                    marker=dict(color='rgb(158,202,225)',
                                line=dict(
                                    color='rgb(8,48,107)',
                                    width=1.5,
                                )),
                    opacity=0.6)
    data = [trace0]
    layout = go.Layout(
        title='Average International Passengers Departure per Month 2013-2015',
    )
    fig = go.Figure(data=data, layout=layout)
    return py.plot(
        fig,
        filename=
        'Average International Passengers Departure per Month 2013-2015')


tls.embed(chart_average_departure())
    for run in month:
        temporary = 0
        temporary = (int(summary_2012[run]) + int(summary_2013[run]) + int(summary_2014[run]))//3
        average_arv.append(temporary)
    return average_arv

def chart_average_arrive():
    "Create Chart Amount Average Arrive International Passenger Years 2012-2014"
    average_arv = average_arrive()
    month = month_list()
    trace0 = go.Bar(
        x=[run for run in month],
        y=[run for run in average_arv],
        marker=dict(
            color='rgb(158,202,225)',
            line=dict(
                color='rgb(8,48,107)',
                width=1.5,
            )
        ),
        opacity=0.6
    )
    data = [trace0]
    layout = go.Layout(
        title='Average International Passengers Arrive per Month 2012-2014',
    )
    fig = go.Figure(data=data, layout=layout)
    return py.plot(fig, filename='Average International Passengers Arrive per Month 2012-2014')

tls.embed(chart_average_arrive())
예제 #29
0
# ##### Count and graph emails received per day

# In[25]:

email_count = [(g[0], len(list(g[1]))) for g in itertools.groupby(all_dates)]

# In[26]:

email_count[0]

# In[27]:

x = []
y = []
for date, count in email_count:
    x.append(date)
    y.append(count)

# In[28]:

py.iplot(Data([Scatter(x=x, y=y)]))

# ##### Restyle the chart in Plotly's GUI

# In[10]:

import plotly.tools as tls

tls.embed('https://plot.ly/~jackp/3266')
예제 #30
0
        month.append(arrive_2014[run][0])
    return month


def chart_arv_dpt_2014():
    "Create Chart Amount Arrive and Departure International Passenger Years 2014"
    summary_arv = summary_arrive_2014()
    summary_dpt = summary_departure_2014()
    month = month_list()
    trace0 = go.Bar(x=[run for run in month],
                    y=[summary_arv[run] for run in month],
                    name='Arrive',
                    marker=dict(color='rgb(49,130,189)'))
    trace1 = go.Bar(x=[run for run in month],
                    y=[summary_dpt[run] for run in month],
                    name='Departure',
                    marker=dict(color='rgb(204,204,204)', ))
    data = [trace0, trace1]
    layout = go.Layout(
        title='International Passengers Arrive/Departure per Month - 2014',
        xaxis=dict(tickangle=-45, ),
        barmode='group',
    )
    fig = go.Figure(data=data, layout=layout)
    return py.plot(
        fig,
        filename='International Passengers Arrive/Departure per Month - 2014')


tls.embed(chart_arv_dpt_2014())
		#client.messages.create(to='+17073155745', from_='+17073108595',
		#	body = outmsg)
		#4 minute cycle to override above cycle
		#time.sleep(250) 

		#format, write to file 
		ctime1 = strftime("%Y-%m-%d %H:%M:%S" , gmtime())
		ctime2 = ctime1.replace(" ", "")
		ctime2 = ctime2.replace(":", "_")
		#fh = open(ctime2, "w")
		#fh.write("Date: " + ctime1[0:10] + " \n")
		#fh.write("Current time: " + ctime1[12:19] +" \n")
		#fh.write("Average: " + savgb + " breaths per " + sltime + " seconds\n")
		#fh.write("Breath Deviation to Variance ratio: " + sDVr + " \n")
		#fh.write("Scaling factor: " + sscalar + " \n") 
		#fh.write("-----------------------------\n")
		#fh.write(summsg)
		#fh.close() 

	#plot point every 50ms
	time.sleep(0.05)
	

#close stream
snt.close()

#embed streaming plot
tls.embed('streaming-demos','12')

예제 #32
0
    data.append(Scatter(x= df.date, y=df.COUNT,name=g))
layout = Layout(xaxis=XAxis(title="Date"), yaxis=YAxis(title="Accident Count"))


# In[18]:

py.iplot(Figure(data=Data(data), layout=layout), filename='nypd_crashes/over_time')


# Luckily for us, while this graph is a bit of a mess, we can still zoom in on specific times and ranges. This makes plotly perfect for exploring datasets. You can create a high level visual of the data then zoom into a more detailed level.
# 
# See below where using the above graph I could zoom in on a particular point and anontate it for future investigation.

# In[19]:

tls.embed("https://plot.ly/~bill_chambers/274")


# In[20]:

car_types = store.select("nypd", "columns=['vehicle_type_code_1', 'vehicle_type_code_2']")
car_types['COUNT'] = 1


# In[21]:

code_1 = car_types.groupby('vehicle_type_code_1').sum()
code_2 = car_types.groupby('vehicle_type_code_2').sum()


# In[22]:
예제 #33
0
import pandas as pd
from tia.bbg import LocalTerminal
import numpy as np
from datetime import datetime
from operator import itemgetter
#import matplotlib
#import matplotlib.pyplot as plt
import plotly
import plotly.plotly as py #for plotting
import plotly.graph_objs as go
import plotly.dashboard_objs as dashboard
plotly.tools.set_credentials_file(username='******', api_key='yuwwkc1sb0')
import plotly.tools as tls
import plotly.figure_factory as ff
tls.embed('https://plot.ly/~dpsugasa/1/')


#set the script start time
start_time = datetime.now()

trade_sheet = pd.read_csv(r'D:\Users\dpsugasa\Desktop\SAN_options/default_SAN.csv')
trade_sheet.Date = pd.to_datetime(trade_sheet.Date)
trade_sheet.Expiry = pd.to_datetime(trade_sheet.Expiry)
trade_sheet = trade_sheet.drop('Vol', axis=1)
trade_sheet['Amount'] = trade_sheet['Amount']/100

opt_tickers = trade_sheet['Ticker'].unique().tolist()
#current_opts = opt_tickers[-int((len(opt_tickers)/6)):] #takes the most recent options; 6 trades
#hist_opts = opt_tickers[:-int((len(opt_tickers)/6))] #takes the historical options
opt1_qtty = 2325
예제 #34
0
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Mar  1 14:50:56 2018

@author: caseytaylor
"""

import plotly
import plotly.plotly as py
import plotly.graph_objs as go
import plotly.tools as tools
import itertools

# Embed Plotly plot in an IPython notebook:
tools.embed('dfreder1', '69')

dfreder1 = py.get_figure('dfreder1', '69')
# print(dfreder1.to_string())

# get data object
years = dfreder1.get_data()[0]['x'] 

unique_years = list(set(years))

# get number of bridges per year
dictionary = {year:years.count(year) for year in unique_years}

# delete pair u'(21658': 1
dictionary2 = {i:dictionary[i] for i in dictionary if i!=u'(21658'}
unique_years2 = [i for i in unique_years if i!= u'(21658']
    summary_dpt = summary_departure_2013()
    month = month_list()
    trace0 = go.Bar(
        x=[run for run in month],
        y=[summary_arv[run] for run in month],
        name='Arrive',
        marker=dict(
            color='rgb(49,130,189)'
        )
    )
    trace1 = go.Bar(
        x=[run for run in month],
        y=[summary_dpt[run] for run in month],
        name='Departure',
        marker=dict(
            color='rgb(204,204,204)',
        )
    )
    data = [trace0, trace1]
    layout = go.Layout(
        title='International Passengers Arrive/Departure per Month - 2013',
        xaxis=dict(
            tickangle=-45,
        ),
        barmode='group',
    )
    fig = go.Figure(data=data, layout=layout)
    return py.plot(fig, filename='International Passengers Arrive/Departure per Month - 2013')

tls.embed(chart_arv_dpt_2013())
예제 #36
0
def chart_arv_dpt():
    "Create Chart Amount Average Arrive and Departure International Passenger Years 2013-2015"
    average_arv = average_arrive()
    average_dpt = average_departure()
    month = month_list()
    trace0 = go.Bar(x=[run for run in month],
                    y=[run for run in average_arv],
                    name='Arrive',
                    marker=dict(color='rgb(49,130,189)'))
    trace1 = go.Bar(x=[run for run in month],
                    y=[run for run in average_dpt],
                    name='Departure',
                    marker=dict(color='rgb(204,204,204)', ))
    data = [trace0, trace1]
    layout = go.Layout(
        title=
        'Average International Passengers Arrive/Departure per Month 2013-2015',
        xaxis=dict(tickangle=-45, ),
        barmode='group',
    )
    fig = go.Figure(data=data, layout=layout)
    return py.plot(
        fig,
        filename=
        'Average International Passengers Arrive/Departure per Month 2013-2015'
    )


tls.embed(chart_arv_dpt())
예제 #37
0
topState = pd.DataFrame(topState.tail(10)[::-1])
bottomState = statedata.groupby('Percent').max()
bottomState = pd.DataFrame(bottomState.head(10))
stateheaders = ['PERCENT', 'STATE','ERROR']
print('Top Ten STATES Percentage of People Below Poverty Level')
print('_______________________________________________________')
print tabulate(topState,cityheaders)
print ('\n')
print('Bottom Ten STATES Percentage of People Below Poverty Level')
print('__________________________________________________________')
print tabulate(bottomState,cityheaders)


# In[138]:

tls.embed("https://plot.ly/~dterrel3/89")


# In[139]:

tls.embed("https://plot.ly/~dterrel3/100")


# In[140]:

tls.embed("https://plot.ly/~dterrel3/216")


# In[144]:

tls.embed("https://plot.ly/~dterrel3/225")
예제 #38
0
    state = GPIO.input(04)

    # x and y are scalar
    x = datetime.datetime.now().strftime("%m-%d-%Y %H:%M:%S.%f")
    # y = (np.cos(shapeparam*counter/50.)*np.cos(counter/50.)+np.random.randn(1))[0]

    # initialize a scaling parameter for the output
    # scaling = counter + int((-1^(np.random.randint(5))*(np.random.randint(5))*0.1))

    if state:
        outv = 8
        # outv = 8 + int((-1^(np.random.randint(5)))*counter/scaling)
    else:
        outv = 0
        # outv = 0 + int((-1^(np.random.randint(5)))*counter/scaling)

    y = outv

    # write to Plotly stream
    snt.write(dict(x=x, y=y))

    # plot point every 70ms
    time.sleep(0.07)

# close stream
snt.close()

# embed streaming plot
tls.embed("streaming-demos", "12")