jmb277
jmb277

Reputation: 568

Function to make a Plotly Scattermapbox with multiple traces fails, Plotly, list comprehension

I am trying to use list comprehension for creating the data property of a Plotly Scattermapbox.

Here is the df:

import plotly.plotly as py
import plotly.graph_objs as go
import pandas as pd

df = pd.DataFrame(
    {"BOROUGH": ['MANHATTAN','MANHATTAN','MANHATTAN','QUEENS', 'QUEENS', 'QUEENS'],
     "CALL_QTY":[100, 10, 5, 15, 30, 45],
     "lat":[40.75, 40.72, 40.73, 40.72, 70.71, 40.721],
     "lng":[-73.99, -73.98, -70.97, -73.74, -73.73, -73.72]})

Here is the tuple that I'm trying to loop through:

u_sel = [list(a) for a in zip(['MANHATTAN', 'QUEENS'], # names
                              ['blue', 'orange'],      # colors
                              [0.6, 0.7])]             # opacity

And here is the function I've created that uses list comprehension:

def scattermap_data(df, u_sel):
    return([go.Scattermapbox(
        lat = df.loc[df['BOROUGH']==b].lat.astype('object'),
        lon = df.loc[df['BOROUGH']==b].lng.astype('object'),
        mode = 'markers',
        marker = dict(
            size=df.loc[df['BOROUGH']==b].CALL_QTY,
            sizeref=0.9,
            sizemode='area',
            color=color,
            opacity=opacity
        )
    )] for b, color, opacity in u_sel
    )

The problem: When I try to run the following:

data = scattermap_data(df, u_sel)
layout = go.Layout(autosize=False,
                   mapbox= dict(
                       accesstoken=mapbox_access_token,
                       zoom=10,
                       style = 'dark',
                       center= dict(
                           lat=40.721319,
                           lon=-73.987130)
                   ),
                   title = "O God, Why?")
fig = dict(data=data, layout=layout)
py.iplot(fig, filename='tmapbox')

ValueError: Invalid value of type 'builtins.generator' received for the 'data' property of Received value: <generator object scattermap_data.<locals>.<genexpr> at 0x000000000A72AF68>

My question: My impression is that this single df will be broken into two instances of scattermapbox trace elements - the structure of data, above would be something like:

data = [trace1, trace2]

(this looks similar in format to many plotly examples where a list of traces make up the data parameter of the dict in fig.

This example does work if I make two separate traces of go.Scattermapbox but I've got several plots that require this multiple-trace property and I do not want to duplicate code for every plot (i.e. make 2 different trace instances for every plot. I feel that I am close to getting this to work but I just need some tweaks.

Ancillary info: Plotly v. 3.3.0, python 3.6, I am new to list comprehensions and plotly and any help would be very much appreciated.

edit: added the import statement in case that was holding anyone back. edit2: improved question statement, title

Upvotes: 3

Views: 1567

Answers (1)

jmb277
jmb277

Reputation: 568

I figured out my problem: I should have used list() instead of the square brackets in the return of my function.

def scattermap_data(df, u_sel):
    return(list(go.Scattermapbox(
        lat = df.loc[df['BOROUGH']==b].lat.astype('object'),
        lon = df.loc[df['BOROUGH']==b].lng.astype('object'),
        mode = 'markers',
        marker = dict(
            size=df.loc[df['BOROUGH']==b].CALL_QTY,
            sizeref=0.9,
            sizemode='area',
            color=color,
            opacity=opacity
        )
    ) for b, color, opacity in u_sel)
)

Upvotes: 3

Related Questions