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
topop
, the marker size is determined by the numeric value in thepop
field.We also set a maximum marker size using
size_max
.By setting
color
tocontinent
, the marker color is determined by thecontinent
field string.We set a name or title for the hover labels using
hover_name
.Setting
log_x
toTrue
(the default for this attribute isFalse
) means theX
axis will be log-scaled in cartesian coordinates.