1、最基本的直方图
#最基本的直方图 import numpy as np import matplotlib.pyplot as plt data = np.random.normal(0,20,1000) bins = np.arange(-100,100,5) plt.hist(data,bins=bins,color='#0504aa',alpha=0.7, rwidth=0.85) plt.xlim(min(data)-5,max(data)+5) plt.show()结果: 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,rwidth=0.85,label='class1',alpha = 0.5) plt.hist(data2,bins=bins,rwidth=0.85,label='class2',alpha = 0.5) plt.legend(loc='best') plt.show()结果:
1、将不同类型的散点画在图上
#散点图 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) x2_samples = np.random.multivariate_normal(mu_vec1+0.2,cov_mat1+0.2,100) x3_samples = np.random.multivariate_normal(mu_vec1+0.4,cov_mat1+0.4,100) plt.figure(figsize=(8,6)) plt.scatter(x1_samples[:,0],x1_samples[:,1],marker='x',color='blue',alpha=0.6,label='x1') plt.scatter(x2_samples[:,0],x2_samples[:,1],marker='o',color='red',alpha=0.6,label='x2') plt.scatter(x3_samples[:,0],x3_samples[:,1],marker='^',color='green',alpha=0.6,label='x3') plt.legend(loc='best') plt.show()结果: 2、在散点图加上标注坐标
#在散点图加上标注坐标 x_coords = [0.13,0.22,0.39,0.68,0.74,0.93,0.41] y_coords = [0.75,0.32,0.45,0.56,0.78,0.29,0.64] plt.figure(figsize=(8,6)) plt.scatter(x_coords,y_coords,marker='^',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') plt.show()结果: 3、散点图设置散点在一定范围是不断变小,一定范围不断变大
mu_vec1 = np.array([0,0]) cov_mat1 = np.array([[1,0],[0,1]]) X = np.random.multivariate_normal(mu_vec1,cov_mat1,500) fig = plt.figure(figsize=(8,6)) R = X**2 R_sum = R.sum(axis = 1) plt.scatter(X[:,0],X[:,1],color = 'green',marker='o',s=20*R_sum,alpha=0.5) plt.show()结果: