Scatter Plot components in matplotlib

 Scatter Plot components in matplotlib

  • A scatter plot in Matplotlib is a type of plot that displays individual data points on a two-dimensional graph. 
  • Each point on the graph represents a single observation.
  • Components of a scatter plot in matplotlib:

1. Figure and Axes:

   - Figure (`plt.figure()`): The top-level container for the entire plot.

   - Axes (`plt.subplot()` or `plt.subplots()`): The area within the figure where the data points are plotted.

 

2. Data Plotting:

   - Scatter Plot (`plt. scatter()`): This is the main function for creating scatter plots. It allows you to specify the x and y coordinates of each point, along with various visual properties.

   plt.scatter(x, y, label='Data Points', color='red', marker='o', s=50)

  

   - `x` and `y`: The data to be plotted along the x and y axes.
   - `label`: A label for the data series.
   - `Color`: The color of the markers.
   - `marker`: The marker style (e.g., 'o' for circles).
   - `s`: The size of the markers.

 

3. Title and Labels:

   - Title (`plt. title()`): Adds a title to the plot.

   plt.title('Scatter Plot')

   - Axis Labels (`plt.xlabel()` and `plt.ylabel()`): Adds labels to the x and y axes.

   plt.xlabel('x')

   plt.ylabel('y')

  

4. Legend:

  •    - Legend (`plt.legend()`): Displays a legend that describes the elements in the plot. 
  • The `label` parameter in the plotting function is used to associate a label with each data series.

   plt.legend()

  

5. Grid Lines:

   - Grid (`plt.grid()`): Adds grid lines to the plot.

   plt.grid(True)

  

6. Customizing Marker Appearance:

   - Color (`color`): Specifies the color of the markers.
   - Marker (`marker`): Specifies the marker style for data points.
   - Markersize (`s`): Sets the size of markers.
    These properties are specified in the `plt.scatter()` function.

 

7. Saving and Displaying the Plot:

   - Save (`plt.savefig()`): Saves the plot to a file.
   - Show (`plt.show()`): Displays the plot on the screen.

   plt.show()

 

example:- a scatter plot with these components:

 import matplotlib.pyplot as plt

import numpy as np

 

np.random.seed(42)

x = np.random.rand(50)

y = 2 * x + 1 + 0.1 * np.random.randn(50)

 

plt.figure(figsize=(8, 4))

plt.scatter(x, y, label='Data Points', color='red', marker='o', s=50)

plt.title('Scatter Plot')

plt.xlabel('x')

plt.ylabel('y')

plt.legend()

plt.grid(True)

plt.show()





Post a Comment

0 Comments