✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
📊 Dash 2.0 is Arriving. Register here.

Mathematical functions on dropdown results

Hello !

Using the two links here and here, I managed to successfully create functioning drop down menus that are responsive! It was a lot of fun experimenting with ipywidgets and Plotly.

However, I’m having a hard time figuring out how to plot mathematical functions. The graph selected is currently a scatter plot, but ideally, I’d like to have a bar plot (which I know is go.Bar) that shows the different salaries (as it does now) and one other bar that is a different color that represents the mean for that Job Title for that Borough.

Additionally, right after the code runs, I get an empty figure that doesn’t have any of the axis that I created, and doesn’t show any graphical information. This changes once the user selects a criteria, but from my understanding, I made a default selection. Did I make a mistake somewhere?

Here is an image of what I get after running the code:

The code that I used is shown below:
I imported the dataset (linked below) and saved it as pay.

pay['Base Salary'] = pay['Base Salary'].replace(('\,', '\.(.*)'), '', regex = True).astype(np.int64)
pay = pay.where(pay['Pay Basis'] == 'per Annum').dropna(how = 'all', axis = 0)

use_date = widgets.Dropdown(
    options = list(pay['Fiscal Year'].unique()),
    description = 'Year : ',
    value = 2016.0
)

job_list = list(pay['Title Description'].unique())
status_list = list(pay['Leave Status as of June 30'].unique())

container = widgets.HBox(childern = [use_date])

status = widgets.Dropdown(
    options = status_list,
    value = 'CEASED',
    description = 'Status : '
)


title = widgets.Dropdown(
    options = job_list,
    value = '* ATTENDING DENTIST',
    description = 'Job Title:   ',
)

filter_list = [i and j and k for i, j, k in zip(pay['Fiscal Year'] == 2016.0,
                                                pay['Leave Status as of June 30'] == 'CEASED',
                                                pay['Title Description'] == '*ATTENDING DENTIST')]

trace1 = go.Scatter(x = pay[filter_list]['Work Location Borough'], y = pay[filter_list]['Base Salary'], 
                    mode = 'markers', marker_size = 10)

g = go.FigureWidget(data = [trace1], layout = go.Layout(width = 750, height = 750, font_size = 12, 
                                                        title = dict(text = 'NYC Payroll by Borough')))

def validate():
    if title.value in pay['Title Description'].unique():
        return True
    else:
        return False

def response(change):
    if validate():
        if use_date.value:
            filter_list = [i and j and k for i, j, k in zip(pay['Fiscal Year'] == use_date.value, 
                                                            pay['Leave Status as of June 30'] == status.value,
                                                            pay['Title Description'] == title.value)]
            temp_df = pay[filter_list]
        else:
            filter_list = [i and j for i,j in zip(pay['Leave Status as of June 30'] == status.value,
                                                  pay['Title Description'] == title.value)]
            temp_df = pay[filter_list]
        
        with g.batch_update():
            g.data[0].x = temp_df['Work Location Borough']
            g.data[0].y = temp_df['Base Salary']
            g.layout.barmode = 'overlay'
            g.layout.xaxis.title = 'Location'
            g.layout.yaxis.title = 'Salary'
        
title.observe(response, names = "value")
status.observe(response, names = "value")
use_date.observe(response, names = "value")

container2 = widgets.HBox([title, status])
widgets.VBox([container, container2, g])

The dataset comes from here.

Any assistance with this would be greatly appreciated.

Thank you!!

Hi @c_alva,

check your dates. at first glance it seems you have floats, and that could be affecting your plot. Perhaps indexing your x axis may also help.