1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > python scatter参数详解_python matplotlib.scatter 用法

python scatter参数详解_python matplotlib.scatter 用法

时间:2023-10-28 19:19:39

相关推荐

python scatter参数详解_python matplotlib.scatter 用法

# -*- coding: utf-8 -*-

#导入模块

from matplotlib import pyplot as plt

import numpy as np

import pprint

from math import pi,sin

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

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

#以 A1为均值,B1为协方差矩阵,生成正态分布的随机数 每次生成不一样

C1=np.random.multivariate_normal(A1,B1,200)

C2=np.random.multivariate_normal(A1+0.2,B1+0.2,200)

#画布的大小为长8cm高6cm

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

#绘图,参数s:点的大小,marker:点的形状 alpha:点的亮度,label:标签

plt.scatter(C1[:,0],C1[:,1],s=30,color='red',marker='o',alpha=0.5,label='C1') #[:,0]每列第一个 [:,1]每列第二个

plt.scatter(C2[:,0],C2[:,1],s=30,color='blue',marker='x',alpha=0.5,label='C2')

#图注部分

plt.title('basic scatter plot ')

plt.xlabel('variables x')

plt.ylabel('variables y')

plt.legend(loc='upper right') #这个必须有

plt.show() #打印展示

image.png

# -*- coding: utf-8 -*-

#导入模块

from matplotlib import pyplot as plt

import numpy as np

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,6)) #设置画布大小

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

for x, y in zip(x_coords, y_coords): #接受x,y返回元祖列表[(0.13,0.75),......]

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

# pyplot.annatate是pyplot模块提供的一个注释函数,xy为点的坐标 xytext为注解内容位置坐标

plt.xlim([0,1]) #设置绘图X边界

plt.ylim([0,1]) #设置绘图Y边界

plt.show()

image.png

# -*- coding: utf-8 -*-

#导入模块

from matplotlib import pyplot as plt

import numpy as np

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

mean = np.array([0,0]) #给定均值

covariance = np.array([[1,0],[0,1]]) #协方差

x = np.random.multivariate_normal(mean,covariance, 1000) # 生成多元正态分布随机向量,数字表散点数量

plt.grid(True) #网格线挂起

R = x**2 #这里很关键 制造s差异 离原点越远越大

R_sum = R.sum(axis=1) #axis=0的时候,其实是沿着第一个(水平X)轴进行相加;axis=1的时候是按照第二个(Y)轴,由于是平方和 不管正向还是负向都变大

plt.scatter(x[:, 0], x[:, 1],color='gray',marker='o',s=32. * R_sum,edgecolor='black',alpha=0.5)

plt.show()

image.png

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。