Scatter简介

Scatter(散点图)由两个变量构成,分别作为散点图的横,竖坐标,通过散点图可以看出变量之间的关系。

 如上如,一些基本的相关性,可以分为正相关,负相关等。 

Scatter 相关代码

x = [1, 2, 3, 4, 5]  y = [6, 7, 2, 4, 5] 
 # 画布:尺寸  p = figure(plot_width=400, plot_height=400)  # 画图  p.scatter(x, y,             size=20, # screen units  显示器像素单位  #           radius=1,  # data-space units  坐标轴单位            marker="circle", color="navy", alpha=0.5)    # p.circle(x, y, size=20, color="navy", alpha=0.5)  # 显示  show(p)  

Scatter图 横纵坐标一 一对应 。

关于Scatter的应用 绘制数据并分类

import matplotlib.pyplot as plt # for visualisation
import random # for random number generation
import numpy as np # for numerical libraries

random.seed(242785) # seed the random number generators
np.random.seed(64254)


w0 = 0 # parameter values used to generate data
w1 = -1.5
w2 = 2.5

n = 500 # number of training samples to generate

mean = [0, 0] # mean
cov = [[3, 0.5], [0.5, 3]] # covariance matrix
x1, x2 = np.random.multivariate_normal(mean, cov, n).T # sample from a multivariate normal distribution

z = w1*x1 + w2*x2 + w0 # generate the latent variable z
y = np.sign(z) # generate the output y based on the sign of z

cdict = {1: 'red', 2: 'blue', 3: 'green'} # colour scheme
fig, ax = plt.subplots()
j = 1
for g in np.unique(y):
    ix = np.where(y == g)
    ax.scatter(x1[ix], x2[ix], c = cdict[j], label = "y = " + str(g), s = 16, alpha=0.3)
    j = j + 1
ax.legend()
plt.xlabel("$x_1$")
plt.ylabel("$x_2$")
plt.show()

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐