Home > Backend Development > Python Tutorial > How to draw a scatter plot with matplotlib

How to draw a scatter plot with matplotlib

DDD
Release: 2023-12-04 14:28:07
Original
1937 people have browsed it

The steps for matplotlib to draw a scatter plot: 1. Import the necessary libraries; 2. Create data, which can generate some random data; 3. Use the "plt.scatter()" function to create a scatter plot and set the color , size, transparency and other attributes; 4. Use "plt.xlabel()" and "plt.ylabel()" to add x-axis and y-axis labels, and use "plt.title()" to add a title; 5. Use "plt .show()” function displays charts, etc.

How to draw a scatter plot with matplotlib

Operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.

Matplotlib is a Python data visualization library that can be used to draw various types of charts, including scatter plots. Below is a simple example to show how to use Matplotlib to draw a scatter plot.

1. Import necessary libraries:

import matplotlib.pyplot as plt  
import numpy as np
Copy after login

2. Create data:

Generate some random data as an example.

x = np.random.rand(50)  # 生成50个随机x值  
y = np.random.rand(50)  # 生成50个随机y值
Copy after login

3. Create a scatter plot:

Use the plt.scatter() function to create a scatter plot. Properties such as color, size, transparency, etc. can be set.

plt.scatter(x, y, c='b', alpha=0.5)  # c设置颜色,alpha设置透明度
Copy after login

4. Add labels and titles:

Use plt.xlabel() and plt.ylabel() to add x-axis and y-axis labels, use plt. title() adds a title.

plt.xlabel('X values')  
plt.ylabel('Y values')  
plt.title('Scatter plot')
Copy after login

5. Display the chart:

Finally, use the plt.show() function to display the chart.

plt.show()
Copy after login

Matplotlib also provides other properties to customize scatter plots, such as setting the shape, size, color, etc. of points. You can use the marker parameter to set the shape of the point, such as 'o' for a circle, '*' for a star, etc. The s parameter can be used to set the size of the points, and the c parameter can be used to set the color of the points. For example: plt.scatter(x, y, marker='o', s=100, c='red') will draw circular points with size 100 on a red background.

You can also use different color maps or color lists to give different colors to different points in the scatter plot. For example, you can use the cmap parameter to set the color map and the c parameter to set the color value of each point. For example: plt.scatter(x, y, c=z, cmap='viridis') will use the viridis color map to color points based on their z values.

The above is the detailed content of How to draw a scatter plot with matplotlib. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template