Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Matplotlib Scatter



Creating scatter plots with Matplotlib's pyplot module is straightforward and offers a variety of customization options. Here’s a comprehensive guide on how to create and customize scatter plots using Matplotlib.

Basic Scatter Plot

To create a basic scatter plot, use the plt.scatter() function:



        import matplotlib.pyplot as plt

        # Example data
        x = [1, 2, 3, 4, 5]
        y = [2, 3, 5, 7, 11]
        
        # Create a scatter plot
        plt.scatter(x, y)
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.title('Basic Scatter Plot')
        plt.show()
      

Customizing Markers

You can customize the markers by changing their color, size, and shape:



        # Example data
        x = [1, 2, 3, 4, 5]
        y = [2, 3, 5, 7, 11]
        
        # Create a scatter plot with customized markers
        plt.scatter(x, y, color='red', s=100, marker='^')  # s is the marker size
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.title('Customized Scatter Plot')
        plt.show()
      

Adding Colors Based on a Third Variable

You can use a third variable to change the color of each point:


        import numpy as np

        # Example data
        x = np.random.rand(50)
        y = np.random.rand(50)
        colors = np.random.rand(50)  # Third variable for color
        
        # Create a scatter plot with colors based on a third variable
        plt.scatter(x, y, c=colors, cmap='viridis')
        plt.colorbar()  # Show color scale
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.title('Scatter Plot with Color Mapping')
        plt.show()
      

Adding Sizes Based on a Third Variable

You can also use a third variable to change the size of each point:



        # Example data
        x = np.random.rand(50)
        y = np.random.rand(50)
        sizes = 1000 * np.random.rand(50)  # Third variable for size
        
        # Create a scatter plot with sizes based on a third variable
        plt.scatter(x, y, s=sizes, alpha=0.5)  # alpha controls the transparency
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.title('Scatter Plot with Size Mapping')
        plt.show()
      

Combining Color and Size

You can combine both color and size customizations:



        # Example data
        x = np.random.rand(50)
        y = np.random.rand(50)
        colors = np.random.rand(50)
        sizes = 1000 * np.random.rand(50)
        
        # Create a scatter plot with both color and size mappings
        plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')
        plt.colorbar()  # Show color scale
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.title('Scatter Plot with Color and Size Mapping')
        plt.show()
      

Adding Annotations

You can add text annotations to individual points:



        # Example data
        x = [1, 2, 3, 4, 5]
        y = [2, 3, 5, 7, 11]
        labels = ['A', 'B', 'C', 'D', 'E']
        
        # Create a scatter plot
        plt.scatter(x, y)
        
        # Add annotations
        for i, label in enumerate(labels):
            plt.annotate(label, (x[i], y[i]), textcoords="offset points", xytext=(0, 10), ha='center')
        
        plt.xlabel('X-axis')
        plt.ylabel('Y-axis')
        plt.title('Scatter Plot with Annotations')
        plt.show()
      

Full Example

Here's a more comprehensive example combining various customizations:



        import matplotlib.pyplot as plt
        import numpy as np
        
        # Example data
        x = np.random.rand(50)
        y = np.random.rand(50)
        colors = np.random.rand(50)
        sizes = 1000 * np.random.rand(50)
        
        # Create subplots
        fig, axs = plt.subplots(1, 2, figsize=(14, 7))
        
        # First subplot: Basic scatter plot
        axs[0].scatter(x, y, color='blue', s=100, marker='o', alpha=0.6)
        axs[0].set_title('Basic Scatter Plot')
        axs[0].set_xlabel('X-axis')
        axs[0].set_ylabel('Y-axis')
        
        # Second subplot: Scatter plot with color and size mapping
        scatter = axs[1].scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')
        fig.colorbar(scatter, ax=axs[1])  # Show color scale
        axs[1].set_title('Scatter Plot with Color and Size Mapping')
        axs[1].set_xlabel('X-axis')
        axs[1].set_ylabel('Y-axis')
        
        # Adjust layout
        plt.tight_layout()
        plt.show()
      




Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

Zava

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

Zava