Bubble Charts

Bubble Charts#

A bubble chart is a scatter plot in which the marker size is tied to a third dimension of the data. We can create bubble charts in plotly.express using the px.scatter() function and assigning the size parameter to a data field.

An example using a single year of the global population data, where per capita GDP is the X axis value, and average life expectancy is the Y axis value. Marker marker size is determined by population.

import plotly.express as px # import statements
df = px.data.gapminder() # load data
fig = px.scatter(df.query("year==2007"), x="gdpPercap", y="lifeExp", size="pop", color="continent", hover_name="country", log_x=True, size_max=60) # create figure
fig # show figure

In this example, we use px.scatter() to create a scatter plot.

  • By setting size to pop, the marker size is determined by the numeric value in the pop field.

  • We also set a maximum marker size using size_max.

  • By setting color to continent, the marker color is determined by the continent field string.

  • We set a name or title for the hover labels using hover_name.

  • Setting log_x to True (the default for this attribute is False) means the X axis will be log-scaled in cartesian coordinates.

Additional Resources#