The Wayback Machine - https://web.archive.org/web/20250305183513/https://www.geeksforgeeks.org/matplotlib-pyplot-scatter-in-python/
Open In App

Matplotlib Scatter

Last Updated : 23 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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:

Python
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:

Screenshot-2024-12-03-124902

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.

Python
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 :

Screenshot-2024-12-03-125850

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:

Python
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 :

Screenshot-2024-12-03-130844

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:

Python
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 :

bubllle

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:

Python
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 :

Screenshot-2024-12-03-132124

Custom scatterplot

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

Frequently Asked Questions (FAQs) on Scatter Plots in Matplotlib

What is a scatter plot?

A scatter plot visualizes the relationship between two numerical variables using dots to represent data points.

How do I create a scatter plot in Matplotlib?

Use plt.scatter(x, y) where x and y are arrays of the same length representing the coordinates.

Can I customize my scatter plot?

Yes, you can customize colors, sizes, transparency, and add titles or labels using various parameters in the scatter() function.

What are common uses for scatter plots?

Scatter plots are used to identify trends, clusters, and outliers in data, making them essential for exploratory data analysis.

How can I plot multiple datasets in one scatter plot?

Call plt.scatter() multiple times with different datasets before calling plt.show() to display them on the same plot


Level up your coding with DSA Python in 90 days! Master key algorithms, solve complex problems, and prepare for top tech interviews. Join the Three 90 Challenge—complete 90% of the course in 90 days and earn a 90% refund. Start your Python DSA journey today!


Next Article
Practice Tags :

Similar Reads

Pandas Scatter Plot – DataFrame.plot.scatter()
A Scatter plot is a type of data visualization technique that shows the relationship between two numerical variables. For plotting to scatter plot using pandas there is DataFrame class and this class has a member called plot. Calling the scatter() method on the plot member draws a plot between two variables or two columns of pandas DataFrame. Synt
2 min read
How to add a legend to a scatter plot in Matplotlib ?
In this article, we are going to add a legend to the depicted images using matplotlib module. We will use the matplotlib.pyplot.legend() method to describe and label the elements of the graph and distinguishing different plots from the same graph. Syntax: matplotlib.pyplot.legend( ["title_1", "Title_2"], ncol = 1 , loc = "upper left" ,bbox_to_anc
2 min read
Use error bars in a Matplotlib scatter plot
Prerequisites: Matplotlib In this article, we will create a scatter plot with error bars using Matplotlib. Error bar charts are a great way to represent the variability in your data. It can be applied to graphs to provide an additional layer of detailed information on the presented data. ApproachImport required python library.Create data.Pass requ
2 min read
How to Annotate Matplotlib Scatter Plots?
A scatter plot uses dots to represent values for two different numeric variables. In Python, we have a library matplotlib in which there is a function called scatter that helps us to create Scatter Plots. Here, we will use matplotlib.pyplot.scatter() method to plot. Syntax : matplotlib.pyplot.scatter(x,y) Parameters: x and y are float values and ar
3 min read
Create Scatter Charts in Matplotlib using Flask
In this article, we will see how to create charts in Matplotlib with Flask. We will discuss two different ways how we can create Matplotlib charts in Flask and present it on an HTML webpage with or without saving the plot using Python. File structure Create and Save the Plot in the Static Directory Here, We first created a get_plot() function whic
4 min read
Animating Scatter Plots in Matplotlib
An animated scatter plot is a dynamic records visualization in Python that makes use of a series of frames or time steps to reveal data points and exchange their positions or attributes over time. Each body represents a second in time, and the scatter plot is up to date for each frame, allowing you to peer traits, fluctuations, or moves in the info
3 min read
Scatter Plot on Polar Axis using Matplotlib
Scatter plots are essential for comprehending the interactions between variables in data visualization. Although scatter plots are frequently created in Cartesian coordinates, we can also make scatter plots on polar axes using Matplotlib. With this feature, one can see circular or angular data in innovative ways, such as cyclical trends or directed
4 min read
Generate a Heatmap in MatPlotLib Using a Scatter Dataset
Heatmaps are a powerful visualization tool that can help you understand the density and distribution of data points in a scatter dataset. They are particularly useful when dealing with large datasets, as they can reveal patterns and trends that might not be immediately apparent from a scatter plot alone. In this article, we will explore how to gene
5 min read
Matplotlib.axes.Axes.scatter() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute. matplotlib.axes.Axes.scatter() Function The Axes.s
4 min read
3D Scatter Plotting in Python using Matplotlib
A 3D Scatter Plot is a mathematical diagram, the most basic version of three-dimensional plotting used to display the properties of data as three variables of a dataset using the cartesian coordinates.To create a 3D Scatter plot, Matplotlib's mplot3d toolkit is used to enable three dimensional plotting.Generally 3D scatter plot is created by using
2 min read