Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

If not all categories are represented in each year, there will be an error #2

Open
gabrielpreda opened this issue Aug 31, 2018 · 1 comment

Comments

@gabrielpreda
Copy link

Scenario:

Define data with the following pattern:
Categories: C1, C2, C3
Items: I1, I2, I3, I4, I5, I6

Year Category Item Value
2010 C3 I6 1
2010 C3 I6 1
2011 C1 I1 3
2011 C2 I3 2
2012 C1 I1 2
2012 C2 I3 1
2012 C3 I5 3

Because Category C1 and C2 are not represented in year 2010, there will be a crash in bubbly, as following:

/opt/conda/lib/python3.6/site-packages/bubbly/bubbly.py in bubbleplot(dataset, x_column, y_column, bubble_column, z_column, time_column, size_column, color_column, x_logscale, y_logscale, z_logscale, x_range, y_range, z_range, x_title, y_title, z_title, title, colorbar_title, scale_bubble, colorscale, marker_opacity, marker_border_width, show_slider, show_button, show_colorbar, show_legend, width, height)
88 bubble_column, z_column, size_column,
89 sizeref, scale_bubble, marker_opacity, marker_border_width,
---> 90 category=category)
91 if z_column:
92 trace['type'] = 'scatter3d'

/opt/conda/lib/python3.6/site-packages/bubbly/bubbly.py in get_trace(grid, col_name_template, x_column, y_column, bubble_column, z_column, size_column, sizeref, scale_bubble, marker_opacity, marker_border_width, color_column, colorscale, show_colorbar, colorbar_title, category)
397
398 trace = {
--> 399 'x': grid.loc[grid['key']==col_name_template.format(x_column, category), 'value'].values[0],
400 'y': grid.loc[grid['key']==col_name_template.format(y_column, category), 'value'].values[0],
401 'text': grid.loc[grid['key']==col_name_template.format(bubble_column, category), 'value'].values[0],

IndexError: index 0 is out of bounds for axis 0 with size 0

Reason:
When the grid is created, this issue appears:

    for year in years:
        for category in categories:
            **dataset_by_year_and_cat = dataset[(dataset[time_column] == int(year)) & (dataset[category_column] == category)]**
            for col_name in column_names:
                # Each column name is unique
                temp = col_name_template.format(year, col_name, category)
                if dataset_by_year_and_cat[col_name].size != 0:
                    grid = grid.append({'value': list(dataset_by_year_and_cat[col_name]), 'key': temp}, ignore_index=True) 

Missing category in current year

@mb7419
Copy link

mb7419 commented Mar 27, 2020

In the get_trace function we can put the existing code in try block and change as below to fix issues of missing categories in a year not being represented on the graph
try:

existing code

except:
trace = {
'x': [],
'y': [],
'text': category,
'mode': 'markers'
}

    if size_column:
        trace['marker'] = {
            'sizemode': 'area',
            'sizeref': sizeref,
            'size': 0,
        }
    else:
        trace['marker'] = {
            'size': 10*scale_bubble,
        }

    if marker_opacity:
        trace['marker']['opacity'] = marker_opacity

    if marker_border_width:
        trace['marker']['line'] = {'width': marker_border_width}

    if color_column:
            trace['marker']['color'] = ""
            trace['marker']['colorbar'] = {'title': colorbar_title}
            trace['marker']['colorscale'] = colorscale

    if category:
        trace['name'] = category

return trace

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants