想要改变世界,就得先改变自己。 ------ 博客首页

4-6 直方图和散点图

 

1.直方图

 

1-1.简单的直方图绘制

  • 一个histogram,通常可以用一个列向量表示(例子中的a,b),列向量里面的每一个值就是一个bin(a,b),比如说列向量有个50个元素,那么就代表有50个bin。简单的说就是有多少条条状图
In [1]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data=np.random.normal(0,20,1000)
bins=np.arange(-100,100,5)

plt.hist(data,bins=bins)
plt.xlim([min(data)-5,max(data)+5])#限制范围
Out[1]:
(-64.75170330553308, 71.47776989236957)
 
 

1-2.直方图常见的参数属性

属性说明类型
x 数据 数值类型
bins 条形数 int
label 设置直方图的标签,可通过legend展示其图例 字符串
color 颜色 "r","g","y","c"
density 是否以密度的形式显示 bool
range x轴的范围 数值元组(起,终)
bottom y轴的起始位置 数值类型
histtype 线条的类型 "bar":方形,"barstacked":柱形,"step":"未填充线条","stepfilled":"填充线条"
align 对齐方式 " left":左,"mid":中间,"right":右
orientation orientation "horizontal":水平,"vertical":垂直
log 单位是否以科学计术法 bool
In [2]:
import random
data1=[random.gauss(15,10)for i in range(500)]
data2=[random.gauss(5,5)for i in range(500)]
bins=np.arange(-50,50,2.5)

plt.hist(data1,bins=bins,label='class1')
plt.hist(data2,bins=bins,label='class2')

plt.legend()
Out[2]:
<matplotlib.legend.Legend at 0x8310710>
 
 

2.散点图

 

2-1.简单的散点图 multivariate_normal(mean, cov, size=None, check_valid=None, tol=None)参数解释如下:

参数解释
mean mean是多维分布的均值维度为1;
cov 协方差矩阵(协方差基本概念戳这里),注意:协方差矩阵必须是对称的且需为半正定矩阵;
size 指定生成的正态分布矩阵的维度(例:若size=(1, 1, 2),则输出的矩阵的shape即形状为 1X1X2XN(N为mean的长度))。
check_valid 这个参数用于决定当cov即协方差矩阵不是半正定矩阵时程序的处理方式,它一共有三个值:warn,raise以及ignore。当使用warn作为传入的参数时,如果cov不是半正定的程序会输出警告但仍旧会得到结果;当使用raise作为传入的参数时,如果cov不是半正定的程序会报错且不会计算出结果;当使用ignore时忽略这个问题即无论cov是否为半正定的都会计算出结果。
In [3]:
#准备数据
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.5,label='x1')
plt.scatter(x2_samples[:,0],x2_samples[:,1],marker='o',color='red',alpha=0.5,label='x2')
plt.scatter(x3_samples[:,0],x3_samples[:,1],marker='^',color='green',alpha=0.5,label='x3')
plt.legend()
Out[3]:
<matplotlib.legend.Legend at 0x84752b0>
 
 

2.散点图加标注:annotate

annotate语法说明 :annotate(s='str' ,xy=(x,y) ,xytext=(l1,l2) ,..)

s 为注释文本内容

xy 为被注释的坐标点

xytext 为注释文字的坐标位置

xycoords 参数如下:

参数解释
figure points points from the lower left of the figure 点在图左下方
figure pixels pixels from the lower left of the figure 图左下角的像素
figure fraction fraction of figure from lower left 左下角数字部分
axes points points from lower left corner of axes 从左下角点的坐标
axes pixels pixels from lower left corner of axes 从左下角的像素坐标
axes fraction fraction of axes from lower left 左下角部分
data use the coordinate system of the object being annotated(default) 使用的坐标系统被注释的对象(默认)
polar(theta,r) if not native ‘data’ coordinates t

extcoords 设置注释文字偏移量

参数 | 坐标系 --|-- 'figure points' | 距离图形左下角的点数量 'figure pixels' | 距离图形左下角的像素数量 'figure fraction' | 0,0 是图形左下角,1,1 是右上角 'axes points' | 距离轴域左下角的点数量 'axes pixels' | 距离轴域左下角的像素数量 'axes fraction' | 0,0 是轴域左下角,1,1 是右上角 'data' | 使用轴域数据坐标系

arrowprops #箭头参数,参数类型为字典dict

参数解释
width 点箭头的宽度
headwidth 在点的箭头底座的宽度
headlength 点箭头的长度
shrink 总长度为分数“缩水”从两端
facecolor 箭头颜色

bbox给标题增加外框 ,常用参数如下:

参数解释
boxstyle 方框外形
facecolor (简写fc)背景颜色
edgecolor (简写ec)边框线条颜色
edgewidth 边框线条大小

bbox=dict(boxstyle='round,pad=0.5', fc='yellow', ec='k',lw=1 ,alpha=0.5) #fc为facecolor,ec为edgecolor,lw为lineweight

In [4]:
x_coords=[0.13,0.22,0.39,0.59,0.68,0.76,0.45]
y_coords=[0.25,0.55,0.91,0.41,0.62,0.81,0.31]

plt.figure(figsize=(8,6))
plt.scatter(x_coords,y_coords,marker='s',s=50)#marker='s'是方形
#每个点坐标加标注annotate
for x,y in zip(x_coords,y_coords):
    plt.annotate('(%s,%s)'%(x,y),xy=(x,y),xytext=(0,-15),textcoords='offset points',ha='center')#设置标注的位置和对齐方式
 
 

2-2 画离定点距离越远圆越大的散点图

In [5]:
mul_vecl=np.array([0,0])
cov_matl=np.array([[1,0],[0,1]])
X=np.random.multivariate_normal(mu_vecl,cov_matl,500)
fig=plt.figure(figsize=(8,6))

R=X**2
R_sum=R.sum(axis=1)
plt.scatter(X[:,0],X[:,1],color='grey',marker='o',s=20*R_sum,alpha=0.5)
Out[5]:
<matplotlib.collections.PathCollection at 0x8548198>
 
posted @ 2019-10-25 19:48  karina512  阅读(819)  评论(0编辑  收藏  举报