How to Create a Bubble Chart in Plotly Python

Spread the love

Bubble Chart –

A bubble chart is a scatter plot in which a third dimension of the data is shown through the size of markers.

To create a Bubble chart we will use the gapminder data. So, Let’s read the data first.

import plotly.express as px

df = px.data.gapminder()
df.head()

Let’s check the minimum and maximum year in the dataframe.

print("Min Year: ", df['year'].min())
print("Max Year: ", df['year'].max())
#output
Min Year:  1952
Max Year:  2007

Let’s create a Bubble chart for the year 2007

# select only 2007 data
df_2007 = df[df['year']==2007]

1 . Create a Bubble Chart with Plotly Express –

Let’s create a bubble chart with plotly express which makes it easy to create various kinds of plots easily in plotly.

We will plot gdpPercap on the x-axis and lifeExp on the y-axis. We will use the pop column for the size of the markers and use the continent column for the color parameter to distinguish between different continents.

fig = px.scatter(df_2007, x='gdpPercap', y='lifeExp',size='pop', 
                 color='continent', log_x=True, size_max=60)
fig.show()

2 . Create an animated Bubble Chart with Plotly Express –

We can also create an animated bubble chart using plotly express. To create an animated bubble chart we need to use the animation_frame and animation_group arguments.

fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", 
                 animation_group="country", size="pop", 
                 color="continent", log_x=True, size_max=55)
fig.show()

Related Posts –

1 . How to install plotly python with pip

2 . How to create a Line Chart with Plotly Python

3 . How to create Scatter plot in Plotly Python

4 . How to create a Bar Chart in Plotly Python

5 . How to create Horizontal Bar Chart in Plotly Python

6 . How to create a Histogram in plotly python

7 . How to Create a Box Plot in Plotly Python

8 . How to create a Pie Chart in Plotly Python

9 . How to create a Dot Plot in Plotly Python

10 . How to Create Heatmap with Plotly Python

11 . How to Create a Violin Plot in Plotly Python

12 . How to Create Subplots in Plotly Python

Rating: 1 out of 5.

Leave a Reply