Saturday, August 27, 2022

How to create scatter plot using matplotlib?

The scatter plot uses dots to represent values for two different numeric variables. The position of each dot on the \(x\) (horizontal) and \(y\) (vertical) axes indicates values for an individual data point. The general form of matplotlib.pyplot.scatter() can be written as:
matplotlib.pyplot.scatter(x,y,s=None, c=None, marker=None, cmap=None, nrom=None, vmin=None,
vmax = None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data = None, **kwargs)
The x and y data are the \(x\) and \(y\) coordinates of data points. The s is the size of markers in scatter plots and is in unit points\(^2\). The c argument is the marker color. The "marker" argument represents the marker style. A marker can be either an instance of the class or the text shorthand for a particular marker. The "cmap" argument is the colormap instance or registered colormap name. Note that cmap is only used if c is an array of floats. The norm argument is used in case c is an array of floats then the norm argument scales the color data "c" in a range from 0 to 1, to map into the colormap cmap. If None, use the default colors. Normalize. The vmin and vmax are used in conjunction with the default norm to map the color array c to the colormap cmap. If None, the respective min and max of the color array are used. The alpha argument is a blending value between transparent (value: 0) and opaque (value:1). The linewidths argument is used to specify the marker edges. The default edgecolors is "face". The edgecolors argument can be 'face' (the edge color will always be the same as the face color), 'none' (no patch boundary will be drawn), and color or sequence of colors. The plotnonfinite argument is used whether to plot points with nonfinite c. If set to True the points are drawn with a bad colormap color.
Example Create scatter plot for points with coordinates
x = np.arange(0,102,2)
y = np.arange(0,10,10)
. Solution: To plot these points we have to generate the data. This will be done using the numpy library.
import numpy as np
x = np.arange(0,102,2)
y = np.arange(0,10,10)
The data will be plotted using matplotlib.pyplot.scatter function. For this scatter plot the figsize will be set to \(12\times8 [\mathrm{in}]\), and the grid will be shown. The xlabel and ylabel will be set to 'x' and 'y', respectively.
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
plt.scatter(x,y)
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
The entire code is given below.
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0,102,2)
y = np.arange(0,510,10)
plt.figure(figsize=(12,8))
plt.scatter(x,y)
plt.grid(True)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
Figure 1Scatter plot of coordinates from Example 1

No comments:

Post a Comment