![]() |
VOOZH | about |
Scatter plots are one of the most fundamental tools for visualizing relationships between two numerical variables. matplotlib.pyplot.scatter() plots points on a Cartesian plane defined by X and Y coordinates. Each point represents a data observation, allowing us to visually analyze how two variables correlate, cluster or distribute.
For example:
Output
Explanation: plt.scatter(x, y) creates a scatter plot on a 2D plane to visualize the relationship between two variables, with a title and axis labels added for clarity and context.
matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, alpha=None, edgecolors=None, label=None)
Parameters:
Parameter | Description |
|---|---|
x, y | Sequences of data points to plot |
s | Marker size (scalar or array-like) |
c | Marker color |
marker | Shape of the marker |
cmap | Colormap for mapping numeric values to colors |
alpha | Transparency (0 = transparent, 1 = opaque) |
edgecolors | Color of marker edges |
label | Legend label for the dataset |
Returns: This function returns a PathCollection object representing the scatter plot points. This object can be used to further customize the plot or to update it dynamically.
Example 1: In this example, we compare the height and weight of two different groups using different colors for each group.
Output
Explanation: We define NumPy arrays x1, y1 and x2, y2 for height and weight data of two groups. Using plt.scatter(), Group 1 is plotted in blue and Group 2 in red, each with labels. The x-axis and y-axis are labeled "Height (cm)" and "Weight (kg)" for clarity.
Example 2: This example demonstrates how to customize a scatter plot using different marker sizes and colors for each point. Transparency and edge colors are also adjusted.
Output
Explanation: NumPy arrays x and y set point coordinates, a defines marker sizes and b assigns colors. plt.scatter() plots the points with transparency, white edges and linewidth. A title is added before displaying the plot.
Example 3: This example shows how to create a bubble plot where the size of each point (bubble) represents a variable's magnitude. Edge color and alpha transparency are also used.
Output
Explanation: Lists x and y define point coordinates, while sizes sets the marker (bubble) sizes. The plt.scatter() plots the bubbles with 50% transparency (alpha=0.5), blue edges and edge width of 2. Axis labels and a title are added before displaying the plot.
Example 4: In this example, we map data values to colors using a colormap and add a colorbar. This helps in visualizing a third variable via color intensity.
Output
Explanation: Random arrays x and y set 100 points, with colors mapped using 'viridis' and varying sizes. plt.scatter() plots them with 0.7 transparency and plt.colorbar() adds a color legend.
Example 5: This final example illustrates how to change the marker style using the marker parameter. Here, triangle markers are used with magenta color.
Output
Explanation: This code plots points with triangle markers ('^') in magenta color, size 100 and 0.7 transparency. A title is added before displaying the plot.