✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🐇 Announcing Dash VTK for 3d simulation graphics. Check out the March webinar.

Create a drop-down menu for a scatterplot

Hi,

I’m trying to do something relatively simple: I want a drop-down menu so that I can visualize my time-series scatterplot for a single variable at the time (or in this context, the Tweets of news organizations one at the time). I tried fidgeting with widgets and cufflinks, and I have referenced the https://plot.ly/python/dropdowns/ Plotly documentation, but I am at a loss after many trials.

I am using Python Pandas.

Here’s my code below:

import plotly.plotly as py
from ipywidgets import interact
from IPython.display import display

def plot_it(Name):
    fig = {
        'data': [
        {
            'x': tw_df.Date_Tweeted, 
            'y': tw_df.Compound[tw_df.Name=='BBC News (World)'], 
            'text': tw_df.Tweet[tw_df.Name=='BBC News (World)'], 
            'mode': 'markers', 
            'name': '@BBCWorld'},
        {
            'x': tw_df.Date_Tweeted, 
            'y': tw_df.Compound[tw_df.Name=='FRANCE 24 English'], 
            'text': tw_df.Tweet[tw_df.Name=='FRANCE 24 English'], 
            'mode': 'markers', 
            'name': '@France24_en'},
         {
            'x': tw_df.Date_Tweeted, 
            'y': tw_df.Compound[tw_df.Name=='CNN'], 
            'text': tw_df.Tweet[tw_df.Name=='CNN'], 
            'mode': 'markers', 
            'name': '@CNN'} ]
'layout': {
            'xaxis': {'title': 'Day & Time of Tweet'},
            'yaxis': {'title': "Sentiment Analysis Compound Score"},
            'title': {'title':'Media Sentiment Analysis Tweets, April 25-26th 2018'}
        } 
}
        } 
    }
    display(py.iplot(fig,filename='grouped-scatter'))

I was able to generate a drop-down menu with my widget, but it’s not communicating with my data, so it’s basically useless (and I’ve tried inserting it into my plot script, to no avail):

interact(plot_it, Name=['BBC News (World)', 'FRANCE 24 English', 'CNN'])

Is this what I am missing? If I understand correctly I need to sort my data into a dictionary?

updatemenus = list([
    dict(active=-1,
         buttons=list([   
            dict(label = 'CNN',
                 method = 'update',
                 args = [{'x': tw_df.Date_Tweeted, 
            'y': tw_df.Compound[tw_df.Name=='CNN'], 
            'text': tw_df.Tweet[tw_df.Name=='CNN'], 
            'mode': 'markers', 
            'name': '@CNN'
                         }
        ]),
])
         )])

I’ve been stuck on this for a while now, any help is immensely appreciated. Thanks!

Carle,

Did anyone ever help you with your use case? I’m stuck trying to figure out the same problem. How do I get the interact dropdown widget to see my data?