Home >Backend Development >Python Tutorial >How to use scatter function in Python
The usage of scatter function in Python requires specific code examples
Python is a widely used programming language with powerful data analysis and visualization capabilities. When it comes to data visualization, the matplotlib library in Python is a very useful and commonly used tool. In matplotlib, the scatter function is a commonly used function for drawing scatter plots. This article will introduce the usage of the scatter function and provide some specific code examples.
The basic usage of the scatter function is very simple and can be used to draw a scatter plot of two-dimensional data. It accepts two parameters, X and Y, which represent the abscissa and ordinate of the scatter plot respectively. The following is a basic usage example of the scatter function:
import matplotlib.pyplot as plt # 生成随机数据 import numpy as np np.random.seed(0) x = np.random.rand(100) y = np.random.rand(100) # 绘制散点图 plt.scatter(x, y) plt.show()
The above code first imports the matplotlib.pyplot module and generates 100 random data as the horizontal and vertical coordinates of the scatter plot. Then, a scatter plot was drawn using the scatter function and the graph was displayed via plt.show(). Running the above code will result in a randomly distributed scatter plot.
The scatter function also provides some additional parameters to control the style, color and size of the scatter points. The following are examples of some commonly used scatter function parameters:
import matplotlib.pyplot as plt # 生成随机数据 import numpy as np np.random.seed(0) x = np.random.rand(100) y = np.random.rand(100) # 绘制散点图,并设置参数 plt.scatter(x, y, color='red', marker='o', s=50) plt.show()
The above code uses the color, marker and s parameters in the scatter function, which are used to set the color, marker type and size of the scatter points respectively. The color parameter accepts a color value or color name, the marker parameter accepts the glyph used to draw scatter points, and the s parameter is used to set the size of the scatter points. Running the above code will result in a red circular scatter plot.
In addition to the above parameters, the scatter function can also accept an additional parameter c, which is used to automatically set the color of the scatter points based on the value of the data. The following is an example of using the c parameter:
import matplotlib.pyplot as plt # 生成随机数据 import numpy as np np.random.seed(0) x = np.random.rand(100) y = np.random.rand(100) colors = np.random.rand(100) # 绘制散点图,并根据数据值设置颜色 plt.scatter(x, y, c=colors) plt.colorbar() plt.show()
The above code uses the c parameter in the scatter function and passes the colors array as a parameter to c. Running the above code will result in a scatter plot with colors automatically set based on data values, and a color bar added via plt.colorbar().
The above are the basic usage of scatter function in Python and some common parameter examples. The scatter function is a very powerful and flexible function that can be used to draw various types of scatter plots. Readers can add appropriate parameters to the scatter function and make customized settings according to their own needs. I hope this article will be helpful to readers in their learning and application of data visualization.
The above is the detailed content of How to use scatter function in Python. For more information, please follow other related articles on the PHP Chinese website!