You are on page 1of 4

Bubble plot

The bubble chart in Plotly is created using the scatter plot. It can be
created using the scatter() method of plotly.express. A bubble chart is a
data visualization which helps to displays multiple circles (bubbles) in a
two-dimensional plot as same in scatter plot. A bubble chart is primarily
used to depict and show relationships between numeric variables.

Bubble charts display data as a cluster of circles. The required data to


create bubble chart needs to have the xy coordinates, size of the bubble
and the colour of the bubbles. The colours can be supplied by the library
itself.

import matplotlib.pyplot as plt

import numpy as np

# create data
x = np.random.rand(40)
y = np.random.rand(40)
z = np.random.rand(40)
colors = np.random.rand(40)
# use the scatter function
plt.scatter(x, y, s=z*1000,c=colors)
plt.show()
Bubble Plot in Python:
Using Matplotlib, we can make bubble plot in Python using the
scatter() function. To make bubble plot, we need to specify size
argument “s” for size of the data points.

We can see that the points in the scatter plots are bubbles now
based on the value of size variable. By default, Matplotlib
makes the bubble color as blue. We have also added
transparency to the bubbles in the bubble plot using
alpha=0.5.

Color Bubble Plot By Variable in Python


Let us color the bubbles differently using another variable in
the bubble plot. The scatter() function has the argument “c” for
specifying colors. And we use the argument c=”Colors” to color
the bubble by a variable. Here, Colors is the quantitative
variable that we created when we constructed the dataframe.

import matplotlib.pyplot as plt


x =[5, 7, 8, 7, 2, 17, 2, 9,
4, 11, 12, 9, 6]

y =[99, 86, 87, 88, 100, 86,


103, 87, 94, 78, 77, 85, 86]

plt.scatter(x, y, c ="blue")

# To show the plot


plt.show()

N = 13

colors = np.random.rand(N)
area = (25 * np.random.rand(N))**2

df = pd.DataFrame({
'X': x,
'Y': y,
'Colors': colors,
"bubble_size":area})
plt.scatter('X', 'Y',
s='bubble_size',
alpha=0.5,
data=df,c=colors)
plt.xlabel("X", size=16)
plt.ylabel("y", size=16)
plt.title("Bubble Plot with Matplotlib", size=18)

You might also like