Friday, December 27, 2024
Google search engine
HomeLanguagesMatplotlib.axes.Axes.scatter() in Python

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.scatter() function in axes module of matplotlib library is used to plot a scatter of y vs. x with varying marker size and/or color.

Syntax: Axes.scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=, edgecolors=None, *, plotnonfinite=False, data=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • x, y: These parameter are the horizontal and vertical coordinates of the data points.
  • s: This parameter is an optional parameter and it contains the marker with size of points**2.
  • c: This parameter is an optional parameter and it contains the sequence of colors.
  • marker: This parameter is also an optional parameter. And it contains the marker style.
  • cmap: This parameter is also an optional parameter which contains the registered colormap name.Its default value is NONE.
  • norm: This parameter is also an optional parameter. And it is used to scale luminance data to 0, 1.Its default value is NONE.
  • vmin, vmax: These parameter are used in conjunction with norm to normalize luminance data with default value None.
  • alpha: This parameter are also an optional parameter. They blending values between 0 (transparent) and 1 (opaque).
  • linewidths: This parameter is also an optional parameter. It is the linewidth of the marker edges.Its default value is None.
  • edgecolors: This parameter is also an optional parameter. It is the sequence of color or {‘face’, ‘none’, None}.
  • plotnonfiniteboolean: This parameter is also an optional parameter. It is the linewidth of the marker edges.Its default value is None.

Returns: This returns the container and it is comprises of the following:

  • plotline:This returns the Line2D instance of x, y plot markers and/or line.
  • caplines:This returns the tuple of Line2D instances of the error bar caps.
  • barlinecols:This returns the tuple of LineCollection with the horizontal and vertical error ranges.

Below examples illustrate the matplotlib.axes.Axes.errorbar() function in matplotlib.axes:

Example-1:




# Implementation of matplotlib function
      
import matplotlib.pyplot as plt
import numpy as np
  
# unit value1 ellipse
rx, ry = 3., 1.
value1 = rx * ry * np.pi
value2 = np.arange(0, 3 * np.pi + 0.01, 0.2)
  
value3 = np.column_stack([rx / value1 * np.cos(value2),
                          ry / value1 * np.sin(value2)])
  
x, y, s, c = np.random.rand(4, 99)
s *= 10**2.
  
fig, ax = plt.subplots()
ax.scatter(x, y, s, c, marker = value3)
ax.set_title("matplotlib.axes.Axes.scatter Example1")
plt.show()


Output:

Example-2:




# Implementation of matplotlib function
      
import numpy as np
import matplotlib.pyplot as plt
  
# first define the ratios
r1 = 0.2
r2 = r1 + 0.3
r3 = r2 + 0.7
  
# define some sizes of the
# scatter marker
sizes = np.array([60, 80, 120, 50])
  
# calculate the points of the
# first pie marker
x1 = np.cos(2 * np.pi * np.linspace(0, r1))
y1 = np.sin(2 * np.pi * np.linspace(0, r1))
  
xy1 = np.row_stack([[0, 0],
                    np.column_stack([x1, y1])])
  
s1 = np.abs(xy1).max()
  
x2 = np.cos(2 * np.pi * np.linspace(r1, r2))
y2 = np.sin(2 * np.pi * np.linspace(r1, r2))
  
xy2 = np.row_stack([[0, 0], 
                    np.column_stack([x2, y2])])
  
s2 = np.abs(xy2).max()
  
x3 = np.cos(2 * np.pi * np.linspace(r2, r3))
y3 = np.sin(2 * np.pi * np.linspace(r2, r3))
xy3 = np.row_stack([[0, 0],
                    np.column_stack([x3, y3])])
  
s3 = np.abs(xy3).max()
  
x4 = np.cos(2 * np.pi * np.linspace(r3, 1))
y4 = np.sin(2 * np.pi * np.linspace(r3, 1))
xy4 = np.row_stack([[0, 0],
                    np.column_stack([x4, y4])])
  
s4 = np.abs(xy4).max()
  
fig, ax = plt.subplots()
ax.scatter(range(3), range(3),
           marker = xy1, s = s1**2 * sizes, 
           facecolor ='blue')
  
ax.scatter(range(3), range(3),
           marker = xy2, s = s2**2 * sizes,
           facecolor ='green')
  
ax.scatter(range(3), range(3),
           marker = xy3, s = s3**2 * sizes, 
           facecolor ='red')
  
ax.scatter(range(3), range(3),
           marker = xy4, s = s4**2 * sizes,
           facecolor ='black')
  
ax.set_title("matplotlib.axes.Axes.scatter Example2")
plt.show()


Output:

RELATED ARTICLES

Most Popular

Recent Comments