Matplotlib直方图和散点图

 

目录

 

直方图

散点图


直方图

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
data = np.random.normal(0,20,100)
bins = np.arange(-100,100,5)
plt.hist(data,bins=bins)
plt.xlim(min(data)-5,max(data)+5)
(-62.93693386964637, 67.06675295224326)

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 = 'class 1', alpha = 0.6)
plt.hist(data2, bins = bins, label = 'class 2', alpha = 0.6)
(array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
         0.,  1.,  1.,  4.,  7., 22., 47., 85., 93., 94., 71., 41., 22.,
         8.,  3.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),
 array([-50. , -47.5, -45. , -42.5, -40. , -37.5, -35. , -32.5, -30. ,
        -27.5, -25. , -22.5, -20. , -17.5, -15. , -12.5, -10. ,  -7.5,
         -5. ,  -2.5,   0. ,   2.5,   5. ,   7.5,  10. ,  12.5,  15. ,
         17.5,  20. ,  22.5,  25. ,  27.5,  30. ,  32.5,  35. ,  37.5,
         40. ,  42.5,  45. ,  47.5]),
 <a list of 39 Patch objects>)

散点图

mu_vecl = np.array([0,0])
cov_matl = np.array([[2,0],[0,2]])
x1_sample = np.random.multivariate_normal(mu_vecl, cov_matl, 100)
x2_sample = np.random.multivariate_normal(mu_vecl + 0.2, cov_matl + 0.2, 100)
x3_sample = np.random.multivariate_normal(mu_vecl + 0.4, cov_matl + 0.4, 100)
plt.figure(figsize=(8,6))
plt.scatter(x1_sample[:,0], x1_sample[:,1], marker = 'x', color = 'blue', alpha = 0.6, label = 'x1')
plt.scatter(x2_sample[:,0], x2_sample[:,1], marker = 'o', color = 'red', alpha = 0.6, label = 'x2')
plt.scatter(x3_sample[:,0], x3_sample[:,1], marker = 's', color = 'green', alpha = 0.6, label = 'x3')
plt.legend(loc='best')
<matplotlib.legend.Legend at 0x1b1c3d6bb00>

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]

plt.figure(figsize=(8,6))
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,-15), textcoords='offset points', ha='center')

猜你喜欢

转载自blog.csdn.net/xzy53719/article/details/82827533
今日推荐