Python Scatter Plot with Multiple Y values for each X

A scatter plot with multiple Y values for each X is useful when you have several data points that share the same X coordinate but have different Y values. This creates vertical clusters of points along specific X positions.

Understanding Multiple Y Values per X

When we say "multiple Y values for each X," we mean having several data points with the same X coordinate but different Y coordinates. This creates vertical groupings in your scatter plot.

Method 1: Using Zip with Random Data

The simplest approach is to create paired X and Y values and plot them individually ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.figure(figsize=(8, 5))

# Create random data points
xs = np.random.rand(50)
ys = np.random.rand(50)

# Plot each point individually
for x, y in zip(xs, ys):
    plt.scatter(x, y, c='blue', alpha=0.6)

plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Random Points')
plt.grid(True, alpha=0.3)
plt.show()

Method 2: Creating True Multiple Y Values per X

To create actual multiple Y values for each X coordinate, we can define specific X positions and generate multiple Y values for each ?

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))

# Define specific X positions
x_positions = [1, 2, 3, 4, 5]

# Create multiple Y values for each X
for x in x_positions:
    # Generate 3-5 random Y values for each X
    num_points = np.random.randint(3, 6)
    y_values = np.random.normal(loc=x, scale=0.5, size=num_points)
    
    # Create array of X values (same X repeated)
    x_values = [x] * num_points
    
    plt.scatter(x_values, y_values, alpha=0.7, s=50)

plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Multiple Y Values for Each X')
plt.grid(True, alpha=0.3)
plt.show()

Method 3: Using Structured Data

For real-world scenarios, you might have structured data where each X has specific multiple Y values ?

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))

# Structured data: each X has multiple Y values
data = {
    1: [2.1, 2.8, 1.9, 2.5],
    2: [3.2, 3.7, 3.1, 3.9, 3.4],
    3: [1.8, 2.2, 1.6],
    4: [4.1, 4.5, 3.9, 4.2, 4.0],
    5: [2.8, 3.1, 2.9, 3.2]
}

# Plot multiple Y values for each X
colors = ['red', 'blue', 'green', 'orange', 'purple']
for i, (x, y_values) in enumerate(data.items()):
    x_values = [x] * len(y_values)
    plt.scatter(x_values, y_values, 
                color=colors[i], 
                label=f'X = {x}',
                alpha=0.7, 
                s=80)

plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Structured Multiple Y Values')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

Comparison of Methods

Method Use Case Data Structure
Random Zip Quick visualization Independent X,Y pairs
Multiple Y per X Clustered data analysis Grouped by X positions
Structured Data Real-world datasets Dictionary or DataFrame

Key Points

  • Use alpha parameter to handle overlapping points

  • Different colors can help distinguish X groups

  • Grid lines improve readability of clustered data

  • Consider using jitter to separate overlapping points

Conclusion

Creating scatter plots with multiple Y values for each X helps visualize data clusters and distributions. Use structured approaches for real data analysis and add visual enhancements like colors and transparency for better clarity.

Updated on: 2026-03-25T21:18:57+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements