WikiGalaxy

Personalize

Scatter Plots and Bubble Charts in Matplotlib

Introduction to Scatter Plots:

Scatter plots are a type of data visualization that uses Cartesian coordinates to display values for two variables for a set of data. The data is displayed as a collection of points, each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.

Introduction to Bubble Charts:

Bubble charts are an extension of scatter plots where each point has a third dimension represented by the size of the bubbles. This allows for the visualization of three variables at once, providing a more comprehensive data representation.

Basic Scatter Plot

Creating a Basic Scatter Plot:

A basic scatter plot can be created using the scatter() function from the matplotlib library. It requires the x and y coordinates of the data points.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]

plt.scatter(x, y)
plt.title('Basic Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
    

Explanation:

This code creates a simple scatter plot with x-coordinates [1, 2, 3, 4, 5] and y-coordinates [2, 3, 5, 7, 11]. The scatter() function plots the data points, and show() displays the plot.

Customizing Scatter Plot

Adding Colors and Sizes:

Scatter plots can be customized by adding colors and varying the size of the points. This is done using the c and s parameters in the scatter() function.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
colors = ['red', 'green', 'blue', 'orange', 'purple']
sizes = [100, 200, 300, 400, 500]

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5)
plt.title('Custom Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
    

Explanation:

This example demonstrates a scatter plot with customized colors and sizes. The c parameter assigns different colors to each point, while the s parameter controls the size of the points. The alpha parameter sets the transparency level of the points.

Bubble Chart Example

Creating a Bubble Chart:

A bubble chart is essentially a scatter plot with a third dimension of data represented by the size of the bubbles. It's useful for visualizing three variables at once.


import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
sizes = [100, 200, 300, 400, 500]

plt.scatter(x, y, s=sizes, alpha=0.5, c='skyblue', edgecolors='w', linewidth=2)
plt.title('Bubble Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
    

Explanation:

In this bubble chart, the size of each bubble represents a third variable, allowing for the visualization of three dimensions of data. The edgecolors and linewidth parameters enhance the appearance of the bubbles.

Scatter Plot with Colormap

Using a Colormap:

Colormaps can be used to represent data values as colors, providing an additional dimension of information. This is particularly useful for continuous data.


import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)

plt.scatter(x, y, c=colors, cmap='viridis', s=100, alpha=0.5)
plt.colorbar()
plt.title('Scatter Plot with Colormap')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
    

Explanation:

This scatter plot uses the viridis colormap to represent the color of each point based on its value. The colorbar() function adds a color scale to the plot, providing a key for interpreting the colors.

3D Scatter Plot

Visualizing in 3D:

3D scatter plots allow for the visualization of three variables in a spatial context, providing a more interactive and comprehensive view of the data.


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

x = np.random.rand(50)
y = np.random.rand(50)
z = np.random.rand(50)

ax.scatter(x, y, z, c='r', marker='o')
ax.set_title('3D Scatter Plot')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')

plt.show()
    

Explanation:

This example demonstrates a 3D scatter plot using the mpl_toolkits.mplot3d module. The scatter() method plots the data points in three dimensions, providing a spatial representation of the data.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025