Matplotlib Scatter
matplotlib.pyplot.scatter() is used to create scatter plots, which are essential for visualizing relationships between numerical variables. Scatter plots help illustrate how changes in one variable can influence another, making them invaluable for data analysis.
A basic scatter plot can be created using matplotlib.pyplot.scatter() by plotting two sets of data points on the x and y axes:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([12, 45, 7, 32, 89, 54, 23, 67, 14, 91])
y = np.array([99, 31, 72, 56, 19, 88, 43, 61, 35, 77])
plt.scatter(x, y)
plt.show()
Output:

Simple Scatter plot
What is Matplotlib.pyplot.scatter()?
The matplotlib.pyplot.scatter()
method creates scatter plots to visualize relationships between variables, illustrating how changes in one variable can impact another. These plots are essential for analyzing interdependencies among variables.
Syntax: matplotlib.pyplot.scatter(x_axis_data, y_axis_data)
Matplotlib.pyplot.scatter() in Python
There are various ways of creating plots using matplotlib.pyplot.scatter() in Python, There are some examples that illustrate the matplotlib. pyplot.scatter() function in matplotlib.plot:
Scatter Plot with Multiple Datasets
In this example there seems to be a relationship between the height and weight data of two distinct groups (Group 1 and Group 2) by plotting them on a scatter plot, using different colors to differentiate between the groups.
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([160, 165, 170, 175, 180, 185, 190, 195, 200, 205])
y1 = np.array([55, 58, 60, 62, 64, 66, 68, 70, 72, 74])
x2 = np.array([150, 155, 160, 165, 170, 175, 180, 195, 200, 205])
y2 = np.array([50, 52, 54, 56, 58, 64, 66, 68, 70, 72])
plt.scatter(x1, y1, color='blue', label='Group 1')
plt.scatter(x2, y2, color='red', label='Group 2')
plt.xlabel('Height (cm)')
plt.ylabel('Weight (kg)')
plt.title('Comparison of Height vs Weight between two groups')
plt.legend()
plt.show()
Output :

Comparison of height vs weight
The plot compares height vs. weight for two different groups, showing the relationship between the two variables.
Customizing Scatter Plots: Color and Size
Matplotlib allows full customization of scatter plots, including selecting the color and size of data points.
Example Code for Color Customization:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([3, 12, 9, 20, 5, 18, 22, 11, 27, 16, 8, 24, 15])
y = np.array([95, 55, 63, 77, 89, 50, 41, 70, 58, 83, 61, 52, 68])
plt.scatter(x, y, color='#23d4e8')
x = np.array([1, 6, 14, 17, 3, 13, 10, 8, 19, 21, 2, 16, 23, 25])
y = np.array([80, 60, 72, 90, 68, 55, 74, 79, 66, 81, 58, 67, 62, 85])
plt.scatter(x, y, color='#32cd68')
plt.show()
Output :

color Scatter plots
Bubble Plots in Matplotlib
Bubble charts add a dimension of data by using the size of the data points to represent additional information.
Example Code for Bubble Plot:
import matplotlib.pyplot as plt
# Data
x_values = [1, 2, 3, 4, 5]
y_values = [2, 3, 5, 7, 11]
bubble_sizes = [30, 80, 150, 200, 300]
# Create a bubble chart with customization
plt.scatter(x_values, y_values, s=bubble_sizes, alpha=0.6, edgecolors='b', linewidths=2)
# Add title and axis labels
plt.title("Bubble Chart with Transparency")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Display the plot
plt.show()
Output :

Bubble Chart
A bubble chart with varying bubble sizes and transparency is generated, visualizing how the size of data points relates to their position.
Advanced Customization in Scatter Plot: Size, Color, and Transparency
Matplotlib offers even more flexibility by allowing you to customize scatter plots using random data, color maps, and alpha (transparency) for more complex visualizations.
Example Code for Advanced Customization:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randint(50, 150, size=(100))
y = np.random.randint(50, 150, size=(100))
colors = np.random.randint(50, 150, size=(100))
sizes = 10 * np.random.randint(50, 150, size=(100))
plt.scatter(x, y, c=colors, s=sizes, alpha=0.7, cmap='plasma')
plt.colorbar()
plt.show()
Output :

Custom scatterplot
A customized scatter plot with varying colors, sizes, and transparency based on random data.