python scatter函数_Matplotlib之scatter()函数

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)

参数(Parameters)说明:

x,y:array_like,shape(n,)

输入数据

s:标量或array_like,shape(n,),可选

大小以点数^ 2。默认是`rcParams ['lines.markersize'] ** 2`。

c:颜色,顺序或颜色顺序,可选,默认:'b'

`c`可以是单个颜色格式的字符串,也可以是一系列颜色

规范的长度为`N`,或一系列`N`数字

使用通过kwargs指定的`cmap`和`norm`映射到颜色

(见下文)。请注意,`c`不应该是单个数字RGB或

RGBA序列,因为这与数组无法区分

值将被彩色映射。 `c`可以是一个二维数组,其中的

行是RGB或RGBA,但是,包括单个的情况

行为所有点指定相同的颜色。

marker:`〜matplotlib.markers.MarkerStyle`,可选,默认值:'o'

请参阅`〜matplotlib.markers`以获取有关不同的更多信息

标记分散支持的样式。 `marker`可以是

该类的实例或特定文本的简写

标记。

cmap:`〜matplotlib.colors.Colormap`,可选,默认:无

一个`〜matplotlib.colors.Colormap`实例或注册名称。

`cmap`仅在`c`是浮点数组时使用。如果没有,

默认为rc`image.cmap`。

norm:`〜matplotlib.colors.Normalize`,可选,默认:无

`〜matplotlib.colors.Normalize`实例用于缩放

亮度数据为0,1。`norm`只有在`c`是一个数组时才被使用

彩车。如果`None',则使用默认值:func:`normalize`。

vmin,vmax:标量,可选,默认值:无

`vmin`和`vmax`与`norm`结合使用来标准化

亮度数据。如果其中任何一个都是`无',那么最小和最大的

使用颜色数组。请注意,如果你通过一个“规范”实例,你的

`vmin`和`vmax`的设置将被忽略。

alpha:标量,可选,默认值:无

alpha混合值,介于0(透明)和1(不透明)之间,

linewidths:标量或array_like,可选,默认值:无

如果无,则默认为(lines.linewidth,)。

verts:(x,y)的序列,可选

如果`marker`为None,这些顶点将用于

构建标记。标记的中心位于

在(0,0)为标准化单位。整体标记重新调整

由``s``完成。

edgecolors :颜色或颜色顺序,可选,默认值:无

如果无,则默认为'face'

如果'face',边缘颜色将永远是相同的

脸色。

如果它是'none',补丁边界不会

被画下来。

对于未填充的标记,“edgecolors”kwarg

被忽视并被迫在内部“面对”。

简单点绘制

按到原点的距离增大点的大小

x = [0,2,4,6,8,10]

y = [0]*len(x)

s = [20*4**n for n in range(len(x))]

plt.scatter(x,y,s=s)

plt.show()

image.png

x = [0,2,4,6,8,10]

y = [0]*len(x)

s = [20*2**n for n in range(len(x))]

plt.scatter(x,y,s=s)

plt.show()

image.png

import numpy as np

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(8,6))

#Generating a Gaussion dataset:

#creating random vectors from the multivariate normal distribution

#given mean and covariance

mu_vec1=np.array([0,0])

cov_mat1=np.array([[1,0],[0,1]])

X=np.random.multivariate_normal(mu_vec1,cov_mat1,500)

R=X**2

R_sum=R.sum(axis=1)

plt.scatter(X[:,0],X[:,1],color='green',marker='o',

s=32.*R_sum,edgecolor='black',alpha=0.5)

plt.show()

image.png

散点绘制

from matplotlib import pyplot as plt

import numpy as np

# Generating a Gaussion dTset:

#Creating random vectors from the multivaritate normal distribution

#givem mean and covariance

mu_vecl = np.array([0, 0])

cov_matl = np.array([[2,0],[0,2]])

x1_samples = np.random.multivariate_normal(mu_vecl, cov_matl,100)

x2_samples = np.random.multivariate_normal(mu_vecl+0.2, cov_matl +0.2, 100)

x3_samples = np.random.multivariate_normal(mu_vecl+0.4, cov_matl +0.4, 100)

plt.figure(figsize = (8, 6))

plt.scatter(x1_samples[:,0], x1_samples[:, 1], marker='x',

color = 'blue', alpha=0.7, label = 'x1 samples')

plt.scatter(x2_samples[:,0], x1_samples[:,1], marker='o',

color ='green', alpha=0.7, label = 'x2 samples')

plt.scatter(x3_samples[:,0], x1_samples[:,1], marker='^',

color ='red', alpha=0.7, label = 'x3 samples')

plt.title('Basic scatter plot')

plt.ylabel('variable X')

plt.xlabel('Variable Y')

plt.legend(loc = 'upper right')

plt.show()

import matplotlib.pyplot as plt

fig,ax = plt.subplots()

ax.plot([0],[0], marker="o", markersize=10)

ax.plot([0.07,0.93],[0,0], linewidth=10)

ax.scatter([1],[0], s=100)

ax.plot([0],[1], marker="o", markersize=22)

ax.plot([0.14,0.86],[1,1], linewidth=22)

ax.scatter([1],[1], s=22**2)

plt.show()

![image.png](http://upload-images.jianshu.io/upload_images/8730384-8d27a5015b37ee97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

import matplotlib.pyplot as plt

for dpi in [72,100,144]:

fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)

ax.set_title("fig.dpi={}".format(dpi))

ax.set_ylim(-3,3)

ax.set_xlim(-2,2)

ax.scatter([0],[1], s=10**2,

marker="s", linewidth=0, label="100 points^2")

ax.scatter([1],[1], s=(10*72./fig.dpi)**2,

marker="s", linewidth=0, label="100 pixels^2")

ax.legend(loc=8,framealpha=1, fontsize=8)

fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")

plt.show()

image.png

import matplotlib.pyplot as plt

for dpi in [72,100,144]:

fig,ax = plt.subplots(figsize=(1.5,2), dpi=dpi)

ax.set_title("fig.dpi={}".format(dpi))

ax.set_ylim(-3,3)

ax.set_xlim(-2,2)

ax.scatter([0],[1], s=10**2,

marker="s", linewidth=0, label="100 points^2")

ax.scatter([1],[1], s=(10*72./fig.dpi)**2,

marker="s", linewidth=0, label="100 pixels^2")

ax.legend(loc=8,framealpha=1, fontsize=8)

fig.savefig("fig{}.png".format(dpi), bbox_inches="tight")

plt.show()

image.png

import numpy as np

import matplotlib.pyplot as plt

x1 = np.random.randn(20)

x2 = np.random.randn(20)

plt.figure(1)

# you can specify the marker size two ways directly:

plt.plot(x1, 'bo', markersize=20) # blue circle with size 10

plt.plot(x2, 'ro', ms=10,) # ms is just an alias for markersize

plt.show()

image.png

plt.scatter(2, 1, s=4000, c='r')

plt.scatter(2, 1, s=1000 ,c='b')

plt.scatter(2, 1, s=10, c='g')

image.png

带标签点绘制

import matplotlib.pyplot as plt

x_coords = [0.13, 0.22, 0.39, 0.59, 0.68, 0.74,0.93]

y_coords = [0.75, 0.34, 0.44, 0.52, 0.80, 0.25,0.55]

fig = plt.figure(figsize = (8,5))

plt.scatter(x_coords, y_coords, marker = 's', s = 50)

for x, y in zip(x_coords, y_coords):

plt.annotate('(%s,%s)'%(x,y), xy=(x,y),xytext = (0, -10), textcoords = 'offset points',ha = 'center', va = 'top')

plt.xlim([0,1])

plt.ylim([0,1])

plt.show()

image.png

用曲线把样本分成两类

# 2-category classfication with random 2D-sample data

# from a multivariate normal distribution

import numpy as np

from matplotlib import pyplot as plt

def decision_boundary(x_1):

"""Calculates the x_2 value for plotting the decision boundary."""

return 4 - np.sqrt(-x_1**2 + 4*x_1 + 6 + np.log(16))

# Generating a gaussion dataset:

# creating random vectors from the multivariate normal distribution

# given mean and covariance

mu_vec1 = np.array([0,0])

cov_mat1 = np.array([[2,0],[0,2]])

x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1,100)

mu_vec1 = mu_vec1.reshape(1,2).T # TO 1-COL VECTOR

mu_vec2 = np.array([1,2])

cov_mat2 = np.array([[1,0],[0,1]])

x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)

mu_vec2 = mu_vec2.reshape(1,2).T # to 2-col vector

# Main scatter plot and plot annotation

f, ax = plt.subplots(figsize = (7, 7))

ax.scatter(x1_samples[:, 0], x1_samples[:,1], marker = 'o',color = 'green', s=40)

ax.scatter(x2_samples[:, 0], x2_samples[:,1], marker = '^',color = 'blue', s =40)

plt.legend(['Class1 (w1)', 'Class2 (w2)'], loc = 'upper right')

plt.title('Densities of 2 classes with 25 bivariate random patterns each')

plt.ylabel('x2')

plt.xlabel('x1')

ftext = 'p(x|w1) -N(mu1=(0,0)^t, cov1 = I)\np.(x|w2) -N(mu2 = (1, 1)^t), cov2 =I'

plt.figtext(.15,.8, ftext, fontsize = 11, ha ='left')

#Adding decision boundary to plot

x_1 = np.arange(-5, 5, 0.1)

bound = decision_boundary(x_1)

plt.plot(x_1, bound, 'r--', lw = 3)

x_vec = np.linspace(*ax.get_xlim())

x_1 = np.arange(0, 100, 0.05)

plt.show()

image.png

直线划分

# 2-category classfication with random 2D-sample data

# from a multivariate normal distribution

import numpy as np

from matplotlib import pyplot as plt

def decision_boundary(x_1):

"""Calculates the x_2 value for plotting the decision boundary."""

# return 4 - np.sqrt(-x_1**2 + 4*x_1 + 6 + np.log(16))

return -x_1 + 1

# Generating a gaussion dataset:

# creating random vectors from the multivariate normal distribution

# given mean and covariance

mu_vec1 = np.array([0,0])

cov_mat1 = np.array([[2,0],[0,2]])

x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1,100)

mu_vec1 = mu_vec1.reshape(1,2).T # TO 1-COL VECTOR

mu_vec2 = np.array([1,2])

cov_mat2 = np.array([[1,0],[0,1]])

x2_samples = np.random.multivariate_normal(mu_vec2, cov_mat2, 100)

mu_vec2 = mu_vec2.reshape(1,2).T # to 2-col vector

# Main scatter plot and plot annotation

f, ax = plt.subplots(figsize = (7, 7))

ax.scatter(x1_samples[:, 0], x1_samples[:,1], marker = 'o',color = 'green', s=40)

ax.scatter(x2_samples[:, 0], x2_samples[:,1], marker = '^',color = 'blue', s =40)

plt.legend(['Class1 (w1)', 'Class2 (w2)'], loc = 'upper right')

plt.title('Densities of 2 classes with 25 bivariate random patterns each')

plt.ylabel('x2')

plt.xlabel('x1')

ftext = 'p(x|w1) -N(mu1=(0,0)^t, cov1 = I)\np.(x|w2) -N(mu2 = (1, 1)^t), cov2 =I'

plt.figtext(.15,.8, ftext, fontsize = 11, ha ='left')

#Adding decision boundary to plot

x_1 = np.arange(-5, 5, 0.1)

bound = decision_boundary(x_1)

plt.plot(x_1, bound, 'r--', lw = 3)

x_vec = np.linspace(*ax.get_xlim())

x_1 = np.arange(0, 100, 0.05)

plt.show()

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值