python画图之散点图sactter函数详解,基本散列点绘制

原文地址:http://blog.csdn.net/u013634684/article/details/49646311

http://blog.csdn.net/abcjennifer/article/details/19848269

http://blog.csdn.net/dataningwei/article/details/53619534

最近开始学习Python编程,遇到scatter函数,感觉里面的参数不知道什么意思于是查资料,最后总结如下:

1、scatter函数原型

2、其中散点的形状参数marker如下:

3、其中颜色参数c如下:

4、基本的使用方法如下:

[python]  view plain  copy
  1. #导入必要的模块  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. #产生测试数据  
  5. x = np.arange(1,10)  
  6. y = x  
  7. fig = plt.figure()  
  8. ax1 = fig.add_subplot(111)  
  9. #设置标题  
  10. ax1.set_title('Scatter Plot')  
  11. #设置X轴标签  
  12. plt.xlabel('X')  
  13. #设置Y轴标签  
  14. plt.ylabel('Y')  
  15. #画散点图  
  16. ax1.scatter(x,y,c = 'r',marker = 'o')  
  17. #设置图标  
  18. plt.legend('x1')  
  19. #显示所画的图  
  20. plt.show()  

结果如下:

5、当scatter后面参数中数组的使用方法,如s,当s是同x大小的数组,表示x中的每个点对应s中一个大小,其他如c,等用法一样,如下:

(1)、不同大小

[python]  view plain  copy
  1. #导入必要的模块  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. #产生测试数据  
  5. x = np.arange(1,10)  
  6. y = x  
  7. fig = plt.figure()  
  8. ax1 = fig.add_subplot(111)  
  9. #设置标题  
  10. ax1.set_title('Scatter Plot')  
  11. #设置X轴标签  
  12. plt.xlabel('X')  
  13. #设置Y轴标签  
  14. plt.ylabel('Y')  
  15. #画散点图  
  16. sValue = x*10  
  17. ax1.scatter(x,y,s=sValue,c='r',marker='x')  
  18. #设置图标  
  19. plt.legend('x1')  
  20. #显示所画的图  
  21. plt.show()  

(2)、不同颜色

[python]  view plain  copy
  1. #导入必要的模块  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. #产生测试数据  
  5. x = np.arange(1,10)  
  6. y = x  
  7. fig = plt.figure()  
  8. ax1 = fig.add_subplot(111)  
  9. #设置标题  
  10. ax1.set_title('Scatter Plot')  
  11. #设置X轴标签  
  12. plt.xlabel('X')  
  13. #设置Y轴标签  
  14. plt.ylabel('Y')  
  15. #画散点图  
  16. cValue = ['r','y','g','b','r','y','g','b','r']  
  17. ax1.scatter(x,y,c=cValue,marker='s')  
  18. #设置图标  
  19. plt.legend('x1')  
  20. #显示所画的图  
  21. plt.show()  

结果:

(3)、线宽linewidths

[python]  view plain  copy
  1. #导入必要的模块  
  2. import numpy as np  
  3. import matplotlib.pyplot as plt  
  4. #产生测试数据  
  5. x = np.arange(1,10)  
  6. y = x  
  7. fig = plt.figure()  
  8. ax1 = fig.add_subplot(111)  
  9. #设置标题  
  10. ax1.set_title('Scatter Plot')  
  11. #设置X轴标签  
  12. plt.xlabel('X')  
  13. #设置Y轴标签  
  14. plt.ylabel('Y')  
  15. #画散点图  
  16. lValue = x  
  17. ax1.scatter(x,y,c='r',s= 100,linewidths=lValue,marker='o')  
  18. #设置图标  
  19. plt.legend('x1')  
  20. #显示所画的图  
  21. plt.show()  

                     注:  这就是scatter基本的用法。


Python 中常用画图工具matplotlib.pyplot工具使用实验。

代码:

[python]  view plain  copy
 print ?
  1. from sklearn.datasets.samples_generator import make_blobs  
  2. import matplotlib.pyplot as plt  
  3.   
  4. X, y = make_blobs(n_samples=100, centers=3, n_features=2,random_state=0)  
  5. y=y+1;  
  6.   
  7. # label  
  8. plt.figure(1)  
  9. ax=plt.subplot(121)  
  10. plt.scatter(X[:,0],X[:,1])  
  11. ax.set_title('No lable')  
  12. ax=plt.subplot(122)  
  13. plt.scatter(X[:,0],X[:,1],y*30,y*30)  
  14. ax.set_title('Have lable')  
  15.   
  16. #  
  17. plt.figure(2)  
  18. ax=plt.subplot(111)  
  19. id=(y==1)  
  20. plt.scatter(X[id,0],X[id,1],s=20,color='b')  
  21. id=(y==2)  
  22. plt.scatter(X[id,0],X[id,1],s=50,color='r')  
  23. id=(y==3)  
  24. plt.scatter(X[id,0],X[id,1],s=70,color='g')  
  25. plt.show()  

显示结果:

fiugre1


figure2


用matplotlib的scatter绘制散点图,legend和matlab中稍有不同,详见代码。

[python]  view plain  copy
  1. x = rand(50,30)  
  2. from numpy import *  
  3. import matplotlib  
  4. import matplotlib.pyplot as plt  
  5.   
  6. #basic  
  7. f1 = plt.figure(1)  
  8. plt.subplot(211)  
  9. plt.scatter(x[:,1],x[:,0])  
  10.   
  11. # with label  
  12. plt.subplot(212)  
  13. label = list(ones(20))+list(2*ones(15))+list(3*ones(15))  
  14. label = array(label)  
  15. plt.scatter(x[:,1],x[:,0],15.0*label,15.0*label)  
  16.   
  17. # with legend  
  18. f2 = plt.figure(2)  
  19. idx_1 = find(label==1)  
  20. p1 = plt.scatter(x[idx_1,1], x[idx_1,0], marker = 'x', color = 'm', label='1', s = 30)  
  21. idx_2 = find(label==2)  
  22. p2 = plt.scatter(x[idx_2,1], x[idx_2,0], marker = '+', color = 'c', label='2', s = 50)  
  23. idx_3 = find(label==3)  
  24. p3 = plt.scatter(x[idx_3,1], x[idx_3,0], marker = 'o', color = 'r', label='3', s = 15)  
  25. plt.legend(loc = 'upper right')  

result:

figure(1):


figure(2):

基本散列点绘制
from matplotlib import pyplot as plt
import numpy as np

# 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([[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()


Screen Shot 2015-11-29 at 10.16.00 AM

带标签
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()


Screen Shot 2015-11-29 at 10.18.33 AM




  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值