plotly Bubble Chart in Python (3 Examples)
Hi! This tutorial will show you how to build a bubble chart in plotly in the Python programming language.
Here is an overview:
Let’s get into the Python code!
Install & Import plotly
# install plotly pip install plotly # import plotly import plotly.express as px
With plotly installed and imported into our Python environment, we can now use its plot-building functions.
First, though, we will need to create a sample dataset that we will visualize.
Create Sample Dataset
We will make use of the gapminder dataset, which comes preloaded in the plotly library.
Therefore, run the lines of code below to load and preview the first 10 rows of the dataset:
df = px.data.gapminder() df.head(10) # country continent year lifeExp pop gdpPercap iso_alpha iso_num #0 Afghanistan Asia 1952 28.801 8425333 779.445314 AFG 4 #1 Afghanistan Asia 1957 30.332 9240934 820.853030 AFG 4 #2 Afghanistan Asia 1962 31.997 10267083 853.100710 AFG 4 #3 Afghanistan Asia 1967 34.020 11537966 836.197138 AFG 4 #4 Afghanistan Asia 1972 36.088 13079460 739.981106 AFG 4 #5 Afghanistan Asia 1977 38.438 14880372 786.113360 AFG 4 #6 Afghanistan Asia 1982 39.854 12881816 978.011439 AFG 4 #7 Afghanistan Asia 1987 40.822 13867957 852.395945 AFG 4 #8 Afghanistan Asia 1992 41.674 16317921 649.341395 AFG 4 #9 Afghanistan Asia 1997 41.763 22227415 635.341351 AFG 4
Now that we have loaded the sample dataset, we can go on to build our bubble chart.
Example 1: Build Basic Bubble Chart
In this first example, we will build a basic bubble chart:
fig = px.scatter(df.query("year==2002"), x="gdpPercap", y="lifeExp", size="pop", color="continent", hover_name="country", log_x=True, size_max=60) fig.show()
In the above example, we first filter the DataFrame (
df) to select only the data for the year 2002.
Then, we plot the GDP per capita (
gdpPercap) on the x-axis against life expectancy (
lifeExp) on the y-axis.
The size of each data point is determined by the population (
pop) and is color-coded by continent (
Additionally, when hovering over a point, the plot displays the name of the country (
log_x = True parameter means that the x-axis will be displayed on a logarithmic scale.
Finally, we use fig.show() to display the scatter plot.
Example 2: Create Faceted Bubble Charts
In this second example, we will turn the plot into a facet plot:
fig = px.scatter(df.query("year==2002"), x="gdpPercap", y="lifeExp", size="pop", color="continent",facet_col = "continent", hover_name="country", log_x=False, size_max=60) fig.show()
Here, we simply introduced and defined the
facet_col = argument as the
continent column in the DataFrame in the px.scatter() method to create the facet plot.
Now, the data for each continent has been separated in different facets, which makes it easier for analysis.
Example 3: Adjust Bubble Chart Opacity
In this last example, we will adjust the opacity of the plot:
fig = px.scatter(df.query("year==2002"), x="gdpPercap", y="lifeExp", size="pop", color="continent",opacity = 0.3, hover_name="country", log_x=True, size_max=60) fig.show()
Again, like in the last example, we only introduced and defined the
opacity = argument in the
px.scatter() method. We set the opacity to 0.3, which reduced the color intensity of the plot markers.
The opacity value is always between 0 and 1. Feel free to play with this parameter until you get the ideal setting for your use case.
Video, Further Resources & Summary
Do you need more explanations on how to build plotly bubble charts in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to build plotly bubble charts in Python.
The YouTube video will be added soon.
You can check out these other articles for more detailed examples and videos of these popular charts in plotly using the Python programming language:
- plotly Contour Plot in Python (4 Examples)
- plotly Table in Python (4 Examples)
- Draw Time Series in plotly Graph in Python (4 Examples)
- plotly Pie & Donut Chart in Python (4 Examples)
- plotly Map in Python (3 Examples)
- Introduction to Python Programming
This post has shown how to build plotly bubble charts in Python. There are other parameters in the px.scatter() method that can be used to further customize the appearance of the bubble chart. You may take a look at the documentation for more detail.
I hope you found this tutorial helpful! In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter