matplotlib的散点图生成图片
时间: 2023-10-11 20:09:12 浏览: 39
要生成matplotlib的散点图,可以按照以下步骤进行操作:
1. 首先,导入所需的库:
```python
import matplotlib.pyplot as plt
import numpy as np
```
2. 创建一个新的Figure对象,并添加一个subplot:
```python
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
```
3. 准备散点图的数据:
```python
x = np.random.randn(100) # x坐标的数据
y = np.random.randn(100) # y坐标的数据
```
4. 使用scatter()函数创建散点图:
```python
ax.scatter(x, y, c='red', marker='o') # c参数指定颜色,marker参数指定标记形状
```
5. 可选:添加标题、坐标轴标签等:
```python
ax.set_title('Scatter Plot') # 设置标题
ax.set_xlabel('X') # 设置x轴标签
ax.set_ylabel('Y') # 设置y轴标签
```
6. 可选:显示网格线、设置图例等:
```python
ax.grid(True, linestyle='--', alpha=0.5) # 显示网格线
ax.legend(['Data Points']) # 设置图例
```
7. 最后,使用plt.show()显示散点图:
```python
plt.show()
```
这样就可以生成一个matplotlib的散点图。