YouTip LogoYouTip

Matplotlib Scatter

We can use the scatter() method in pyplot to draw a scatter plot. The scatter() method syntax is as follows: matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, *, edgecolors=None, plotnonfinite=False, data=None, **kwargs) **Parameter Description:** **x, y**: Arrays of the same length, which are the data points we are about to plot, input data. **s**: The size of the points, default 20, can also be an array, each parameter in the array corresponds to the size of the point. **c**: The color of the points, default blue 'b', can also be a 2D row array of RGB or RGBA. **marker**: The style of the points, default small circle 'o'. **cmap**: Colormap, default None, scalar or the name of a colormap, only used when c is an array of floating point numbers. If not specified, it is image.cmap. **norm**: Normalize, default None, data brightness between 0-1, only used when c is an array of floating point numbers. **vmin, vmax**: Brightness setting, ignored when norm parameter exists. **alpha**: Transparency setting, between 0-1, default None, which means opaque. **linewidths**: The length of the marker points. **edgecolors**: Color or color sequence, default 'face', optional values are 'face', 'none', None. **plotnonfinite**: Boolean value, set whether to use non-finite c (inf, -inf or nan) to draw points. ****kwargs**: Other parameters. The following example shows the scatter() function receiving array parameters of the same length, one for the x-axis values and another for the y-axis values: ## Example import matplotlib.pyplot as plt import numpy as np x = np.array([1,2,3,4,5,6,7,8]) y = np.array([1,4,9,16,7,11,23,18]) plt.scatter(x, y) plt.show() The result is as follows: !(#) Set the icon size: ## Example import matplotlib.pyplot as plt import numpy as np x = np.array([1,2,3,4,5,6,7,8]) y = np.array([1,4,9,16,7,11,23,18]) sizes = np.array([20,50,100,200,500,1000,60,90]) plt.scatter(x, y, s=sizes) plt.show() The result is as follows: !(#) Customize point colors: ## Example import matplotlib.pyplot as plt import numpy as np x = np.array([1,2,3,4,5,6,7,8]) y = np.array([1,4,9,16,7,11,23,18]) colors = np.array(["red","green","black","orange","purple","beige","cyan","magenta"]) plt.scatter(x, y, c=colors) plt.show() The result is as follows: !(#) Set two groups of scatter plots: ## Example import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6]) y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86]) plt.scatter(x, y, color ='hotpink') x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12]) y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85]) plt.scatter(x, y, color ='#88c999') plt.show() The result is as follows: !(#) Use random numbers to set the scatter plot: ## Example import numpy as np import matplotlib.pyplot as plt # Seed for random number generator np.random.seed(19680801) N =50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) area =(30 * np.random.rand(N))**2# 0 to 15 point radii plt.scatter(x, y, s=area, c=colors, alpha=0.5)# Set color and transparency plt.title("TUTORIAL Scatter Test")# Set title plt.show() The result is as follows: !(#) ### Colorbar Colormap The Matplotlib module provides many available colorbars. A colorbar is like a list of colors, where each color has a value ranging from 0 to 100. Here is an example of a colorbar: !(#) To set the colorbar, you need to use the cmap parameter, the default value is 'viridis', then the color value is set to an array from 0 to 100. ## Example import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6]) y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86]) colors = np.array([0,10,20,30,40,45,50,55,60,70,80,90,100]) plt.scatter(x, y, c=colors, cmap='viridis') plt.show() The result is as follows: !(#) To display the colorbar, you need to use the plt.colorbar() method: ## Example import matplotlib.pyplot as plt import numpy as np x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6]) y = np.array([99,
← Matplotlib PieMatplotlib Grid β†’