可以使用matplotlib中的pyplot.scatter函数绘制散点图,scatter函数与plot函数的用法是类似的,只是plot函数是用来绘制点与点之间连续的曲线的,scatter函数中的参数如下:
函数中主要使用的参数有:x, y, color, marker,x为x轴中坐标x的值,y为y轴中坐标y的值,x与y都是长度相同的数组序列,color为点的颜色,marker为散点的形状,
官方的文档说明网址:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter,下面是一个简单的一个例子:
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
x = np.arange(-10, 11)
y = x ** 2
# print(x, y)
# axis设置坐标轴的范围
plt.axis([-20, 20, 0, 200])
# x为x轴中坐标x的值,y为y轴中坐标y的值,x与y都是长度相同的数组序列,color为点的颜色,marker为散点的形状,
# linewidth为点的大小
plt.scatter(x, y, color="c", marker=",", linewidth=1)
plt.show()