matplotlib.pyplot.scatter
时间: 2023-04-27 21:06:54 浏览: 62
matplotlib.pyplot.scatter 是 matplotlib 库中用来绘制散点图的函数。它接受两个数组作为参数,分别表示 x 和 y 轴上的数据。用户可以通过其他参数来指定散点图的样式,如颜色、大小等。
相关问题
使用 matplotlib.pyplot.scatter 函数绘制经纬度散点图的具体代码
以下是使用 matplotlib.pyplot.scatter 函数绘制经纬度散点图的示例代码:
```python
import matplotlib.pyplot as plt
# 经度和纬度数据
longitude = [116.4074, 121.4737, 113.2644, 113.5575, 104.0657]
latitude = [39.9042, 31.2304, 23.1291, 22.3964, 30.6595]
# 绘制散点图
plt.scatter(longitude, latitude)
# 设置坐标轴范围
plt.xlim(100, 130)
plt.ylim(20, 50)
# 设置坐标轴标签
plt.xlabel('Longitude')
plt.ylabel('Latitude')
# 显示图形
plt.show()
```
这段代码可以绘制出经度和纬度数据对应的散点图,并设置坐标轴范围和标签。
matplotlib.pyplot用途
Matplotlib.pyplot是Matplotlib的一个子模块,它提供了一组类似于MATLAB的命令,使得Matplotlib在交互式命令行中工作更方便。它可以用于创建各种类型的图表,包括线图,散点图,条形图,直方图等等。此外,它还可以用于自定义图表的各个方面,例如轴标签,标题,图例等等。Matplotlib.pyplot还可以与NumPy一起使用,以便更轻松地处理和可视化NumPy数组。
以下是Matplotlib.pyplot的一些用途:
1.绘制线图
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.show()
```
2.绘制散点图
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.scatter(x, y)
plt.show()
```
3.绘制条形图
```python
import matplotlib.pyplot as plt
x = ['A', 'B', 'C', 'D']
y = [10, 20, 25, 30]
plt.bar(x, y)
plt.show()
```
4.绘制直方图
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
plt.hist(x, bins=50)
plt.show()
```