matplotlib.pyplot模块的用法

基础的用法:

使用import导入模块matplotlib.pyplot,并简写成plt 使用import导入模块numpy,并简写成np

import matplotlib.pyplot as plt
import numpy as np

使用np.linspace定义x:范围是(-1,1);个数是50. 仿真一维数据组(x ,y)表示曲线1.

x = np.linspace(-1, 1, 50)
y = 2*x + 1

使用plt.figure定义一个图像窗口. 使用plt.plot画(x ,y)曲线. 使用plt.show显示图像.

plt.figure()
plt.plot(x, y)
plt.show()

基本用法

 

 

2.plt.figure的用法:

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基本的用法。

注:参考http://blog.csdn.net/u013634684/article/details/49646311

posted @ 2018-02-24 21:11  西园公子zwj  阅读(1110)  评论(0)    收藏  举报