Matplotlib Scatter

A scatter plot is used to display values for two variables in a set of data. Each data point is represented by a dot, where the position on the horizontal axis corresponds to one variable and the position on the vertical axis corresponds to another variable. Scatter plots are commonly used to visualize the relationship or correlation between these two variables.

Creating a Basic Scatter Plot

You can create a scatter plot using the plt.scatter() function. Here’s a basic example:

Python
import matplotlib.pyplot as plt

x = [5, 7, 8, 7, 2, 17, 2, 9, 4, 11, 12, 9, 6]
y = [99, 86, 87, 88, 100, 86, 103, 87, 94, 78, 77, 85, 86]

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

This code creates a simple scatter plot with x and y as the coordinates of the points. The plot also includes titles and labels for clarity.

Customizing Scatter Plots

Matplotlib allows you to customize scatter plots by changing the size, color, and shape of the points. Here’s how you can do it:

Python
plt.scatter(x, y, s=100, c="red", marker="^")
plt.title("Customized Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

In this example, s=100 changes the size of the points, c="red" changes their color, and marker="^" changes the marker shape to a triangle. This allows for a high degree of customization, making your plots more informative and visually appealing.

Adding Transparency

To make your scatter plot points semi-transparent, you can use the alpha parameter. This is useful when dealing with overlapping points:

Python
plt.scatter(x, y, alpha=0.5)
plt.title("Scatter Plot with Transparency")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

The alpha parameter controls the opacity of the points, where alpha=1 is fully opaque and alpha=0 is fully transparent.

Using Color Maps

You can also use color maps to add another dimension to your scatter plot, such as representing the magnitude of a third variable:

Python
import numpy as np

colors = np.random.rand(len(x))

plt.scatter(x, y, c=colors, cmap="viridis", s=100)
plt.colorbar()  # Show color scale
plt.title("Scatter Plot with Color Map")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

This example assigns a random color to each point using a colormap (in this case, "viridis"), and the colorbar() function adds a color scale to the plot.

Conclusion

Scatter plots are powerful tools for visualizing the relationships between two variables. By customizing the size, color, and transparency of the points, and by using color maps, you can enhance your scatter plots to convey more information and provide clearer insights into your data.