Matplotlib - Bubble Plots



A bubble plot is a type of scatter plot where each data point is represented by a marker (generally a circle or "bubble") on a two-dimensional axis. In addition to the traditional x and y coordinates, a third variable is used to determine the size of the marker, creating the illusion of three dimensions. The size of each bubble provides additional information about the data point.

Bubble Plots

Bubble Plots in Matplotlib

We can create a bubble plot in Matplotlib using the scatter() function. This function allows us to customize the appearance of the bubble plot, including markers, colors, and sizes of the points.

This function takes two arrays or lists as input, where each array corresponds to the values of a different variable. The points are then plotted on a set of axes, with the position of each point determined by the values of the two variables.

The scatter() Function

In Matplotlib, a bubble plot can be created using the scatter() function, where the "s" parameter is used to set the size of the markers (bubbles). The scatter function allows you to represent three dimensions of data: the x and y coordinates determine the position of the markers, while the s parameter controls their size.

Following is the syntax of the scatter() function in Matplotlib −

plt.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, ...)

Where,

  • x and y is an array or list representing the x-coordinate and y-coordinate values respectively of the data points.
  • s (optional) is the size of the markers.
  • c (optional) is the color of the markers.
  • marker (optional) is the marker style.
  • cmap (optional) is the colormap for mapping the color of the markers.
  • norm (optional) is the normalize object for mapping the data values to the colormap.
  • vmin, vmax (optional) is the minimum and maximum values for normalizing the colormap.

These are just a few parameters; there are more optionals parameters available for customization.

Basic Bubble Plot

In a Basic Bubble Plot using Matplotlib, we represent data points as bubbles on a two-dimensional plane. The position of each bubble is determined by its corresponding x and y coordinates, while the size of the bubble indicates a third dimension, representing a quantitative variable.

Example

In the following example, we are creating a basic bubble plot using plt.scatter() function. The size of each bubble is determined by the "sizes" list, and the "alpha" parameter controls the transparency. The edges of the bubbles are set to "white" −

import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
sizes = [50, 100, 75, 120, 90]
# Creating a bubble plot 
plt.scatter(x, y, s=sizes, alpha=0.5, edgecolors='w', linewidth=2)
# Adding title and axis labels
plt.title('Basic Bubble Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Displaying the plot
plt.show()

Output

After executing the above code, we get the following output −

Basic Bubble Plot

Color-Mapped Bubble Plot

We enhance the basic bubble plot in Matplotlib by using a color dimension to create a Color-Mapped Bubble Plot. In this type of plot, each bubble not only conveys information through its x and y coordinates but also displays a unique color, creating a color-mapped effect. This coloring is often associated with a third variable, adding another layer of meaning to the plot.

We construct the Color-Mapped Bubble Plot using the plt.scatter() function, with the "c" parameter specifying the color mapping.

Example

In here, we are creating a bubble plot by adding color to each bubble. The "colors" list assigns a unique color to each bubble, creating a color-mapped effect. We are outlining the bubbles with black edges −

import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
sizes = [50, 100, 75, 120, 90]
colors = ['red', 'green', 'blue', 'orange', 'purple']
# Color-Mapped Bubble Plot
plt.scatter(x, y, s=sizes, c=colors, alpha=0.8, edgecolors='k', linewidth=2)
# Adding title and axis labels
plt.title('Color-Mapped Bubble Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Displaying the plot
plt.show()

Output

Following is the output of the above code −

Color-Mapped Bubble Plot

Customized Bubble Plot

We can also customize the appearance of the bubbles in a bubble plot. This is achieved by changing the marker style, edge color, transparency etc. using various parameters in the plt.scatter() function.

Example

Now, we are customizing the bubble plot by changing the marker style to a triangle (marker='^') −

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
sizes = [50, 100, 75, 120, 90]
colors = ['red', 'green', 'blue', 'orange', 'purple']
# Customized Bubble Plot
plt.scatter(x, y, s=sizes, c=colors, alpha=0.8, edgecolors='k', linewidth=2, marker='^')

# Adding title and axis labels
plt.title('Customized Bubble Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# Displaying the plot
plt.show()

Output

Output of the above code is as follows −

Customized Bubble Plot

Bubble Plot with Annotations

In a bubble plot with annotations using Matplotlib, data points are represented as bubbles, with the size of each bubble corresponding to a specific numerical value (annotations). These annotations provide textual information associated with each data point.

Example

In the example below, we are adding annotations to the bubble plot, displaying the exact size value of each bubble. We are using the plt.annotate() function to place text labels near each bubble −

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 25, 30]
sizes = [50, 100, 75, 120, 90]

# Bubble Plot with Annotations
plt.scatter(x, y, s=sizes, alpha=0.5, edgecolors='w', linewidth=2)
for i, txt in enumerate(sizes):
    # Adding annotations
    plt.annotate(f'{int(txt)}', (x[i], y[i]), textcoords="offset points", xytext=(0, -15), ha='center')

# Adding title and axis labels
plt.title('Bubble Plot with Annotations')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# Displaying the plot
plt.show()

Output

The output obtained is as shown below −

Bubble Plot with Annotations
Advertisements