scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, edgecolors=None, hold=None, data=None, **kwargs)
参数详解:
参数详解:
常用参数:
# legend()方式1
plt.scatter(x1,y1, label='point1')
plt.scatter(x2,y2, label='point2')
plt.legend()
# legend()方式2
plt.scatter(x1,y1)
plt.scatter(x2,y2)
plt.legend(['point1','point2'])
如下,现将其可视化
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14)
data_path0 = './数据集/data0.txt'
data_path8 = './数据集/data8.txt'
def read_data(data_path):
with open(data_path,'r',encoding='utf-8') as f:
lines = f.readlines()
x_list = []
y_list = []
for line in lines:
seg = line.split()
if seg !=[]:
x_list.append(float(seg[0]))
y_list.append(float(seg[1]))
f.close()
return x_list, y_list
x_list0, y_list0 = read_data(data_path0)
x_list8, y_list8 = read_data(data_path8)
plt.title(u'data0/8散点图',FontProperties=font,loc='center')
plt.scatter(x_list0, y_list0, s=10, c='r', marker='o',label='point1')
plt.scatter(x_list8, y_list8, s=30, c='b', marker='+',label='point2')
plt.xlabel(u'x值',FontProperties=font)
plt.ylabel(u'y值',FontProperties=font)
plt.legend()
plt.show()
plt.savefig('0.png')
如果数据点精度过高,比如精确到小数点后6位,可能此时所有的数据点就会重复在一起,图上只能显示一个总的数据点,这种情况下,可以通过设置x轴和y轴的刻度范围,来使得所有点都能分别显示出来。
plt.xlim(min(x_list)-0.000001,max(x_list)+0.000001)#确定横轴坐标范围
plt.ylim(min(y_list)-0.000001,max(y_list)+0.000001)#确定纵轴坐标范围
参考:
https://www.cnblogs.com/shuaishuaidefeizhu/p/11359826.html
https://blog.csdn.net/xiaobaicai4552/article/details/79065990
https://www.cnblogs.com/linblogs/p/9642031.html
https://blog.csdn.net/TeFuirnever/article/details/88945563