Python 中用 matplotlib 画散列点 (Scatter)
2017-05-12 17:42
465 查看
http://blog.topspeedsnail.com/archives/830
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.16.00-AM.png)
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.18.33-AM.png)
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.20.41-AM.png)
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.24.24-AM.png)
基本散列点绘制
1234567891011121314151617181920212223242526272829 | from matplotlib import pyplot as pltimport numpy as np # Generating a Gaussion dataset:# creating random vectors from the multivariate normal distribution# given mean and covariancemu_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) # x1_samples.shape -> (100, 2), 100 rows, 2 columns 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() |
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.16.00-AM.png)
带标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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() |
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.18.33-AM.png)
用曲线把样本分成两类
12345678910111213141516171819202122232425262728293031323334353637383940414243 | # 2-category classification with random 2D-sample data# from a multivariate normal distribution import numpy as npfrom 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 covariancemu_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 1-col vector # Main scatter plot and plot annotationf, ax = plt.subplots(figsize=(7, 7))ax.scatter(x1_samples[:,0], x1_samples[:,1], marker='o', color='green', s=40, alpha=0.5)ax.scatter(x2_samples[:,0], x2_samples[:,1], marker='^', color='blue', s=40, alpha=0.5)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 plotx_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() |
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.20.41-AM.png)
按到原点的距离增大点的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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='gray', marker='o', s=32. * R_sum, edgecolor='black', alpha=0.5) plt.show() |
![](http://blog.topspeedsnail.com/wp-content/uploads/2015/11/Screen-Shot-2015-11-29-at-10.24.24-AM.png)
相关文章推荐
- Python 中用 matplotlib 画散列点 (Scatter)
- Python中用matplotlib.pyplot画图总结
- scatter 基本用法 python matplotlib
- matplotlib绘制散列图scatter()参数设置
- Python matplotlib 通过 plt.scatter画空心圆标记出特定的点
- 【python】Matplotlib画图(一)——散点图scatter
- python中用matplotlib 画pie chart时避免标题重叠
- python中scatter()函数用法,matplotlib画图
- Python中使用matPlotlib绘图-曲线图、柱状图、散列点
- Python画图库 matplotlib, 数值计算库 numpy, 科学计算库 scipy 的安装
- python使用matplotlib绘图 -- barChart
- 基于 Python Matplotlib 模块的高质量图形输出
- Python Matplotlib安装方法以及使用
- Python图表绘制:matplotlib绘图库入门
- 纯python的功能齐全的制表制图库-matplotlib
- Python 介绍二 ——Matplotlib
- python使用matplotlib绘图 -- barChart
- Python之路—matplotlib与云图
- 用 python matplotlib 画图 简单记录
- python 中的matplotlib.pylot 模块中的plot 用法