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

Bubble Chart - inverting the size of markers


Using plotly.graph_objects, I am trying to create a bubble chart where the marker size is set based on a long integer array.

By default, the larger the integer, the larger the ‘bubble’ or size. However, I’d like to do the opposite and have those points with the smallest integer value to have a larger respective bubble size in the plot.
Is there anyway to automatically invert the sizing property to do this? Or a easy workaround?

For example, if I have the following data records below, I would want c to be the largest on the plot followed by b a

point  integer-value
a      20
b      10 
c      5


I think the easiest way to achieve what you are looking for is to use a Python list comprehension to create a new array that has the inverse (1/x) of the values in your original size array. You can then use that new array when creating your figure, passing it to the size and sizeref attributes, like this:

import plotly.graph_objects as go

size = [20, 40, 60, 80, 100, 80, 60, 40, 20, 40]

inverse_size = [1/i for i in size]

fig = go.Figure(data=[go.Scatter(
    x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    y=[11, 12, 10, 11, 12, 11, 12, 13, 12, 11],


How to use these attributes is documented at https://plotly.com/python/bubble-charts/#scaling-the-size-of-bubble-charts.

I hope that answers your question!

That’s perfect - thank you! @joseph.damiba

I also was also just able to create a simple function you can apply to your original list to swap the values accordingly so I’ll share it here for any one else’s reference:

def swap_relative_values(lst):
    returns the input list (in same order) with each value
    swapped based on the maximum value in the list
        input list: [1, 3, 1, 5]
        output list: [5, 3, 1, 1]
    max_val = max(lst)
    swapped_list = [abs(x - max_val)+1 for x in lst]
    return swapped_list