.scatter()
Published Mar 6, 2023Updated Mar 28, 2023
Contribute to Docs
The .scatter()
method in the matplotlib library is used to draw a scatter plot, showing a relationship between variables.
Syntax
matplotlib.pyplot.scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite)
Both ‘x’ and ‘y’ parameters are required, and represent float or array-like objects. Other parameters are optional and modify plot features like marker size and/or color.
.scatter()
takes the following arguments:
x
andy
: Positional arguments of type float or array.s
: A float or an array (of size equal to x or y) specifying marker size.c
: An array or list specifying marker color.marker
: Sets the marker style, specified with a shorthand code (e.g. “.”: point, “o”: circle) or an instance of the class.cmap
: A Colormap instance used to map scalar data to colors. (Default: “viridis”)norm
: Normalization method used to scale scalar data to a range of (0 to 1) before mapping. Linear scaling is default.vmin
andvmax
: Sets the data range for the colormap (if norm is not specified).alpha
: Sets the transparency value of the markers - range between 0 (transparent) and 1 (opaque).linewidths
: Sets the linewidth of the marker edge.edgecolors
: Sets the edge color of the marker.plotnonfinite
: Boolean value determining whether to plot nonfinite (inf
,-inf
,nan
) values. Default isFalse
.
Examples
Examples below demonstrate the use of .scatter()
to plot values and vary marker properties.
import matplotlib.pyplot as pltx1 = [5, 13, 21, 28, 31, 34, 39, 44, 49]y1 = [14, 28, 44, 56, 67, 53, 47, 30, 11]plt.scatter(x1, y1)plt.show()
Output:
import matplotlib.pyplot as pltx2 = [11, 22, 33, 44, 55]y2 = [11, 22, 33, 44, 55]plt.scatter(x2, y2, s=150, c='#88c988', linewidth=3, marker='p' , edgecolor='#175E17', alpha=0.75)plt.show()
Output:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Matplotlib on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly90 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours