Example #1
0
def plotly_map():
    mapbox_access_token = 'pk.eyJ1Ijoic3VodHdpbnMiLCJhIjoi' +\
                        'Y2pnNGJvbXRhMGpoNDJwcWRva3JieWgwcCJ9' +\
                        '.cmsuwG65XkGUh2pv07nIVg'

    data = Data([
        Scattermapbox(
            lat=['37.7765', '37.791377'],
            lon=['-122.4506', '-122.392609'],
            mode='markers',
            marker=Marker(size=15),
            text=["University of San Francisco, Main Campus",
                  "University of San Francisco, Downtown Campus"])])
    layout = Layout(
        autosize=True,
        hovermode='closest',
        mapbox=dict(
            accesstoken=mapbox_access_token,
            bearing=0,
            center=dict(
                lat=37.773972,
                lon=-122.431297
            ),
            pitch=0,
            zoom=11
        ),
    )

    fig = dict(data=data, layout=layout)
    output = plotly.offline.plot(fig, include_plotlyjs=False,
                                 output_type='div')

    activities_grouped_df = activities_df.groupby(['date'], as_index=False)['miles'].sum()
    activities_grouped_df['dow'] = activities_grouped_df.date.apply(lambda x: x.weekday())
    activities_grouped_df['week_start'] = activities_grouped_df.date.apply(lambda x: x - timedelta(days=x.weekday()))

    miles_per_week = activities_grouped_df.groupby(['week_start'], as_index=False).miles.sum()
    by_week_df = pd.DataFrame(activities_grouped_df.week_start.unique(), columns=['week_start'])

    for i in range(7):
        by_week_df['{}'.format(i)] = i

    for i in range(7):
        by_week_df = pd.merge(by_week_df, activities_grouped_df, left_on=['week_start', '{}'.format(i)],
                              right_on=['week_start', 'dow'], how='left', suffixes=('', '_{}'.format(i)))

    by_week_df = by_week_df[['week_start', 'miles', 'miles_1', 'miles_2', 'miles_3', 'miles_4', 'miles_5', 'miles_6']]
    by_week_df.columns = ['week_start', 'miles_0', 'miles_1', 'miles_2', 'miles_3', 'miles_4', 'miles_5', 'miles_6']
    by_week_df['year'] = by_week_df['week_start'].apply(lambda x: x.year)
    by_week_df.fillna(0, inplace=True)
    by_week_df = pd.merge(by_week_df, miles_per_week, how='left', on='week_start')

    days_dict = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'}

    dimensions = list()

    for i in range(7):
        dimensions.append(
            dict(range=[0, 20],
                 constraintrange=[0, 20],
                 label='{}'.format(days_dict[i]), values=by_week_df['miles_{}'.format(i)]))

    data = [
        go.Parcoords(
            line=dict(color=by_week_df['miles'],
                      colorscale='Hot',
                      showscale=True,
                      reversescale=True),
            opacity=0.5,
            dimensions=dimensions, hoverinfo='text')

    ]

    layout = go.Layout(
        plot_bgcolor='#E5E5E5',
        paper_bgcolor='#E5E5E5',
        title='Miles per week broken down by day'
    )

    fig = go.Figure(data=data, layout=layout)

    output2 = plotly.offline.plot(fig, include_plotlyjs=False,
                                 output_type='div')

    activities_df['week_start'] = activities_df.date.apply(lambda x: x - timedelta(days=x.weekday()))
    activities_grouped_df_2 = activities_df.groupby(['workout_type', 'week_start'], as_index=False)['miles'].sum()

    by_week_activity_df = pd.DataFrame(activities_grouped_df_2.week_start.unique(), columns=['week_start'])

    for i in activities_df.workout_type.unique():
        by_week_activity_df['{}'.format(i)] = i

    for i in range(4):
        by_week_activity_df = pd.merge(by_week_activity_df, activities_grouped_df_2,
                                       left_on=['week_start', '{}'.format(activities_df.workout_type.unique()[i])],
                                       right_on=['week_start', 'workout_type'], how='left',
                                       suffixes=('', '_{}'.format(activities_df.workout_type.unique()[i])))

    by_week_activity_df = by_week_activity_df[['week_start', 'miles', 'miles_Workout', 'miles_Long Run', 'miles_Race']]
    by_week_activity_df.columns = ['week_start', 'miles_Run', 'miles_Workout', 'miles_Long Run', 'miles_Race']
    by_week_activity_df.fillna(0, inplace=True)

    by_week_activity_df['miles_Run'] = np.array(by_week_activity_df['miles_Run']) + np.array(
        by_week_activity_df['miles_Long Run'])

    by_week_activity_df['miles_Run'] = np.array(by_week_activity_df['miles_Run']) + np.array(
        by_week_activity_df['miles_Long Run'])

    data = []
    custom_colours = ['blue', 'orange', 'red']
    j = 0
    for i in ['Run', 'Workout', 'Race']:
        data.append(go.Bar(
            x=by_week_activity_df['week_start'],
            y=by_week_activity_df['miles_{}'.format(i)],
            marker=dict(color=custom_colours[j]),
            name=i))
        j += 1

    layout = dict(
        barmode='stack',
        hovermode='closest',
        title='Miles per week',
        xaxis=dict(
            rangeselector=dict(
                buttons=list([
                    dict(count=1,
                         label='1m',
                         step='month',
                         stepmode='backward'),
                    dict(count=6,
                         label='6m',
                         step='month',
                         stepmode='backward'),
                    dict(count=1,
                         label='YTD',
                         step='year',
                         stepmode='todate'),
                    dict(count=1,
                         label='1y',
                         step='year',
                         stepmode='backward'),
                    dict(step='all')
                ])
            ),
            rangeslider=dict(),
            type='date'
        )
    )

    fig = go.Figure(data=data, layout=layout)

    output3 = plotly.offline.plot(fig, include_plotlyjs=False,
                                 output_type='div')

    return(output,output2,output3)
Example #2
0
# In[15]:

dates_borough['COUNT'] = 1


# In[16]:

date_borough_sum = dates_borough.groupby(['borough', "date"]).sum()
date_borough_sum.head()


# In[17]:

data = []
for g, df in date_borough_sum.reset_index().groupby('borough'):
    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")
def fig2():
    # (!) Set 'size' values to be proportional to rendered area,
    #     instead of diameter. This makes the range of bubble sizes smaller
    sizemode = 'area'

    # (!) Set a reference for 'size' values (i.e. a population-to-pixel scaling).
    #     Here the max bubble area will be on the order of 100 pixels
    sizeref = df['Population'].max() / 1e2 ** 2

    colors = {
        'Asia': "rgb(255,65,54)",
        'Europe': "rgb(133,20,75)",
        'Africa': "rgb(0,116,217)",
        'North America': "rgb(255,133,27)",
        'South America': "rgb(23,190,207)",
        'Antarctica': "rgb(61,153,112)",
        'Oceania': "rgb(255,220,0)",
    }

    # Define a hover-text generating function (returns a list of strings)
    def make_text(X):
        return 'Country: %s\
        <br>Life Expectancy: %s years\
        <br>Population: %s million' \
               % (X['Name'], X['LifeExpectancy'], X['Population'] / 1e6)

        # Define a trace-generating function (returns a Scatter object)

    def make_trace(X, continent, sizes, color):
        return Scatter(
                x=X['GNP'],  # GDP on the x-xaxis
                y=X['LifeExpectancy'],  # life Exp on th y-axis
                name=continent,  # label continent names on hover
                mode='markers',  # (!) point markers only on this plot
                text=X.apply(make_text, axis=1).tolist(),
                marker=Marker(
                        color=color,  # marker color
                        size=sizes,  # (!) marker sizes (sizes is a list)
                        sizeref=sizeref,  # link sizeref
                        sizemode=sizemode,  # link sizemode
                        opacity=0.6,  # (!) partly transparent markers
                        line=Line(width=3, color="white")  # marker borders
                )
        )

    # Initialize data object
    data = Data()

    # Group data frame by continent sub-dataframe (named X),
    #   make one trace object per continent and append to data object
    for continent, X in df.groupby('Continent'):
        sizes = X['Population']  # get population array
        color = colors[continent]  # get bubble color

        data.append(
                make_trace(X, continent, sizes, color)  # append trace to data object
        )

        # Set plot and axis titles
    title = "Life expectancy vs GNP from MySQL world database (bubble chart)"
    x_title = "Gross National Product"
    y_title = "Life Expectancy [in years]"

    # Define a dictionary of axis style options
    axis_style = dict(
            type='log',
            zeroline=False,  # remove thick zero line
            gridcolor='#FFFFFF',  # white grid lines
            ticks='outside',  # draw ticks outside axes
            ticklen=8,  # tick length
            tickwidth=1.5  # and width
    )

    # Make layout object
    layout = Layout(
            title=title,  # set plot title
            plot_bgcolor='#EFECEA',  # set plot color to grey
            hovermode="closest",
            xaxis=XAxis(
                    axis_style,  # add axis style dictionary
                    title=x_title,  # x-axis title
                    range=[2.0, 7.2],  # log of min and max x limits
            ),
            yaxis=YAxis(
                    axis_style,  # add axis style dictionary
                    title=y_title,  # y-axis title
            )
    )

    # Make Figure object
    fig = Figure(data=data, layout=layout)

    # (@) Send to Plotly and show in notebook
    py.iplot(fig, filename='s3_life-gdp')
def fig2():
    # (!) Set 'size' values to be proportional to rendered area,
    #     instead of diameter. This makes the range of bubble sizes smaller
    sizemode = 'area'

    # (!) Set a reference for 'size' values (i.e. a population-to-pixel scaling).
    #     Here the max bubble area will be on the order of 100 pixels
    sizeref = df['Population'].max() / 1e2 ** 2

    colors = {
        'Asia': "rgb(255,65,54)",
        'Europe': "rgb(133,20,75)",
        'Africa': "rgb(0,116,217)",
        'North America': "rgb(255,133,27)",
        'South America': "rgb(23,190,207)",
        'Antarctica': "rgb(61,153,112)",
        'Oceania': "rgb(255,220,0)",
    }

    # Define a hover-text generating function (returns a list of strings)
    def make_text(X):
        return 'Country: %s\
        <br>Life Expectancy: %s years\
        <br>Population: %s million' \
               % (X['Name'], X['LifeExpectancy'], X['Population'] / 1e6)

        # Define a trace-generating function (returns a Scatter object)

    def make_trace(X, continent, sizes, color):
        return Scatter(
                x=X['GNP'],  # GDP on the x-xaxis
                y=X['LifeExpectancy'],  # life Exp on th y-axis
                name=continent,  # label continent names on hover
                mode='markers',  # (!) point markers only on this plot
                text=X.apply(make_text, axis=1).tolist(),
                marker=Marker(
                        color=color,  # marker color
                        size=sizes,  # (!) marker sizes (sizes is a list)
                        sizeref=sizeref,  # link sizeref
                        sizemode=sizemode,  # link sizemode
                        opacity=0.6,  # (!) partly transparent markers
                        line=Line(width=3, color="white")  # marker borders
                )
        )

    # Initialize data object
    data = Data()

    # Group data frame by continent sub-dataframe (named X),
    #   make one trace object per continent and append to data object
    for continent, X in df.groupby('Continent'):
        sizes = X['Population']  # get population array
        color = colors[continent]  # get bubble color

        data.append(
                make_trace(X, continent, sizes, color)  # append trace to data object
        )

        # Set plot and axis titles
    title = "Life expectancy vs GNP from MySQL world database (bubble chart)"
    x_title = "Gross National Product"
    y_title = "Life Expectancy [in years]"

    # Define a dictionary of axis style options
    axis_style = dict(
            type='log',
            zeroline=False,  # remove thick zero line
            gridcolor='#FFFFFF',  # white grid lines
            ticks='outside',  # draw ticks outside axes
            ticklen=8,  # tick length
            tickwidth=1.5  # and width
    )

    # Make layout object
    layout = Layout(
            title=title,  # set plot title
            plot_bgcolor='#EFECEA',  # set plot color to grey
            hovermode="closest",
            xaxis=XAxis(
                    axis_style,  # add axis style dictionary
                    title=x_title,  # x-axis title
                    range=[2.0, 7.2],  # log of min and max x limits
            ),
            yaxis=YAxis(
                    axis_style,  # add axis style dictionary
                    title=y_title,  # y-axis title
            )
    )

    # Make Figure object
    fig = Figure(data=data, layout=layout)

    # (@) Send to Plotly and show in notebook
    py.iplot(fig, filename='s3_life-gdp')
Example #5
0
def test_validate_error():
    data = Data()
    data.append({'not-a-key': 'anything'})
    data.validate()