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

matplotlib.pyplot.scatter() in Python

Last Updated : 29 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Matplotlib stands as an extensive library in Python, offering the capability to generate static, animated, and interactive visualizations. The Matplotlib.pyplot.scatter() in Python extends to creating diverse plots such as scatter plots, bar charts, pie charts, line plots, histograms, 3-D plots, and more.

For a more in-depth understanding, additional information can be found in the guide titled “Python Matplotlib – An Overview.”

What is Matplotlib.pyplot.scatter()?

The matplotlib.pyplot.scatter() plots serve as a visual tool to explore and analyze the relationships between variables, utilizing dots to depict the connection between them. The matplotlib library provides the scatter() method, specifically designed for creating scatter plots. These plots are instrumental in illustrating the interdependencies among variables and how alterations in one variable can impact another

Syntax: matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)

Parameters:

  • x_axis_data: An array containing data for the x-axis.matplotlib
  • s: Marker size, which can be a scalar or an array of size equal to the size of x or y.
  • c: Color of the sequence of colors for markers.
  • marker: Marker style.
  • cmap: Colormap name.
  • linewidths: Width of the marker border.
  • edgecolor: Marker border color.
  • alpha: Blending value, ranging between 0 (transparent) and 1 (opaque).

Except for x_axis_data and y_axis_data, all other parameters are optional, with their default values set to None. The scatter plot examples below demonstrate the versatility of the scatter() method by showcasing various combinations of these optional parameters.

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:

  • Basic Scatter Plot
  • Scatter Plot With Multiple Datasets
  • Bubble Chart Plot
  • Customized Scatter Plot

Scatter Plot in Matplotlib

By importing matpltlib. plot () we created a scatter plot. It defines x and y coordinates, then plots the points in blue and displays the plot. 

Python3




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, c ="blue")
 
# To show the plot
plt.show()


Output :

first

Basic Scatter Plot

Plot Multiple Datasets on a Scatterplot

The below code generates a scatter plot showcasing two distinct datasets, each with its set of x and y coordinates. The code employs different markers, colors, and styling options for enhanced visualization.

Python3




import matplotlib.pyplot as plt
 
# dataset-1
x1 = [89, 43, 36, 36, 95, 10,
      66, 34, 38, 20]
 
y1 = [21, 46, 3, 35, 67, 95,
      53, 72, 58, 10]
 
# dataset2
x2 = [26, 29, 48, 64, 6, 5,
      36, 66, 72, 40]
 
y2 = [26, 34, 90, 33, 38,
      20, 56, 2, 47, 15]
 
plt.scatter(x1, y1, c ="pink",
            linewidths = 2,
            marker ="s",
            edgecolor ="green",
            s = 50)
 
plt.scatter(x2, y2, c ="yellow",
            linewidths = 2,
            marker ="^",
            edgecolor ="red",
            s = 200)
 
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()


Output :

second-

Scatter Plot with Multiple Datasets

Bubble Plots in Matplotlib

This code generates a bubble chart using Matplotlib. It plots points with specified x and y coordinates, each represented by a bubble with a size determined by the bubble_sizes list. The chart has customization for transparency, edge color, and linewidth. Finally, it displays the plot with a title and axis labels.

Python3




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

Custom a Matplotlib Scatterplot

By importing Matplotlib we create a customized scatter plot using Matplotlib and NumPy. It generates random data for x and y coordinates, colors, and sizes. The scatter plot is then created with customized properties such as color, size, transparency, and colormap. The plot includes a title, axis labels, and a color intensity scale. Finally, the plot is displayed

Python3




import matplotlib.pyplot as plt
import numpy as np
 
# Generate random data
x = np.random.rand(50)
y = np.random.rand(50)
colors = np.random.rand(50)
sizes = 100 * np.random.rand(50)
 
# Create a customized scatter plot
plt.scatter(x, y, c=colors, s=sizes, alpha=0.7, cmap='viridis')
 
# Add title and axis labels
plt.title("Customized Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
 
# Display color intensity scale
plt.colorbar(label='Color Intensity')
 
# Show the plot
plt.show()


Output :

final

Customized Scatter Plot

Conclusion

In conclusion, matplotlib.pyplot.scatter() Python is a versatile and powerful tool for visualizing relationships between variables through scatter plots. Its flexibility allows for the customization of markers, colors, sizes, and other properties, providing a dynamic means of representing complex data patterns. Whether for basic exploratory analysis or detailed data interpretation, this function plays a crucial role in creating informative and visually appealing scatter plots within the Python programming environment.
 



Previous Article
Next Article

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. Synta
2 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
Matplotlib.pyplot.broken_barh() in Python
Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. Pyplot is a collection of command style functions that make matplotlib work like MATLAB. matplotlib.pyplot.broken_barh() The function broken_barh() is used to Plot a horizontal sequence of rectan
2 min read
Matplotlib.pyplot.axis() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source. matplotlib.pyplot.axis() This function is us
1 min read
Matplotlib.pyplot.get_fignums() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.get_fignums() method The ge
2 min read
Python | Matplotlib.pyplot ticks
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2003.One of the greatest benefits of visualization is that it allows us visual access to huge
3 min read
Matplotlib.pyplot.waitforbuttonpress() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.waitforbuttonpress() method
2 min read
Matplotlib.pyplot.streamplot() in Python
Stream plot is basically a type of 2D plot used majorly by physicists to show fluid flow and 2D field gradients .The basic function to create a stream plot in Matplotlib is: ax.streamplot(x_grid, y_grid, x_vec, y_vec, density=spacing) Here x_grid and y_grid are arrays of the x and y points.The x_vec and y_vec represent the stream velocity of each p
7 min read
Matplotlib.pyplot.ylim() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.ylim() Function The ylim() function in pyplot module of matplotlib library is used to get or set the y-limits of the current axes. Syntax
2 min read
Matplotlib.pyplot.figimage() function in Python
Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage() is a method for adding a non-resampled image to
2 min read
matplotlib.pyplot.arrow() in Python
Matplotlib is a very powerful plotting library useful for those working with Python and NumPy. And for making statistical interference, it becomes very necessary to visualize our data and Matplotlib is the tool that can be very helpful for this purpose. It provides MATLAB like interface only difference is that it uses Python and is open source. mat
2 min read
Matplotlib.pyplot.ylabel() in Python
Matplotlib is a very powerful plotting library useful for those working with Python and NumPy. And for making statistical interference, it becomes very necessary to visualize our data and Matplotlib is the tool that can be very helpful for this purpose. matplotlib.pyplot.ylabel() This function sets the label for the y-axis of the plot. Syntax: matp
2 min read
Matplotlib.pyplot.acorr() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.acorr() Function The acorr() function in pyplot module of matplotlib library is used to plot the autocorrelation of x (array-like). Synta
2 min read
matplotlib.pyplot.angle_spectrum() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.acorr() Function: The angle_spectrum() function in pyplot module of matplotlib library is used to plot the angle spectrum. Generally, it
2 min read
Matplotlib.pyplot.thetagrids() in Python
Matplotlib is a plotting library of Python programming language and its numerical mathematics module is NumPy. matplotlib.pyplot is a collection of command style functions that make matplotlib work like the MATLAB Tool. Each of the pyplot functions makes certain changes to a figure: e.g., creating a figure, creating a plot area in a figure, plots s
2 min read
matplotlib.pyplot.axhline() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.axhline() Function The axhline() function in pyplot module of matplotlib library is used to add a horizontal line across the axis. Syntax
2 min read
Matplotlib.pyplot.vlines() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Matplotlib.pyplot.vlines() matplotlib.pyplot.vlines() is a function used in the plotting of a dataset. In matplotlib.pyplot.vlines(), vlines is the abbreviation for vertical lines.what this function does is very much clear from the expanded for
2 min read
Matplotlib.pyplot.yticks() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. Matplotlib.pyplot.yticks() Function The annotate() function in pyplot module of matplotlib library is used to get and set the current tick locations and la
2 min read
Matplotlib.pyplot.annotate() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.annotate() Function The annotate() function in pyplot module of matplotlib library is used to annotate the point xy with text s. Syntax:
3 min read
Matplotlib.pyplot.xticks() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.xticks() Function The annotate() function in pyplot module of matplotlib library is used to get and set the current tick locations and la
2 min read
Matplotlib.pyplot.xlabels() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.xlabel() Function The xlabel() function in pyplot module of matplotlib library is used to set the label for the x-axis.. Syntax: matplotl
2 min read
Matplotlib.pyplot.ylabels() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.ylabel() Function The ylabel() function in pyplot module of matplotlib library is used to set the label for the x-axis.. Syntax: matplotl
2 min read
Matplotlib.pyplot.barbs() in Python
Matplotlib is a library of Python bindings which provides the user with a MATLAB-like plotting framework. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Note: For more information, refer to Python Matplotlib – An Overview Ma
3 min read
Matplotlib.pyplot.axvspan() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python.\ Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source. matplotlib.pyplot.axvspan() This function s
2 min read
Matplotlib.pyplot.locator_params() in Python
Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays.Pyplot is a collection of command style functions that make matplotlib work like MATLAB. Note: For more information, refer to Python Matplotlib – An Overview locator_params() is used for controlli
2 min read
Matplotlib.pyplot.stackplot() in Python
Matplotlib is a visualization library available in Python. Pyplot contains various functions that help matplotlib behave like MATLAB. It is used as matplotlib.pyplot for plotting figures, creating areas, lines, etc. Stackplot Among so many functions provided by pyplot one is stackplot which will be discussed in this article. Stackplot is used to dr
3 min read
Matplotlib.pyplot.xkcd() in Python
One of the main process in Data Science is Data Visualization. Data Visualization refers to present a dataset in the form of graphs and pictures. We can identify upcoming trend by observing these Graphs. Python provides us with an amazing Data Visualization library in it which is Matplotlib which was developed by John Hunter (1968-2012). Matplotlib
2 min read
Matplotlib.pyplot.axvline() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Matplotlib can be used in Python scripts, the Python and IPython shell, web application servers, and various graphical user interface toolkits like Tkinter, awxPython, etc. Note: For more information, refer to Python Matplotlib – An Overview Py
2 min read
Matplotlib.pyplot.stem() in Python
Matplotlib is a visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.pyplot.stem() matplotlib.pyplot.stem() creates stem plots. A Stem plot plots vertical lines at each x position covered under the graph f
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg