小试牛刀

在上一节已经安装好matplotlib模块,下面使用几个例子熟悉一下。

对应的一些文档说明:

http://matplotlib.org/1.3.1/api/pyplot_summary.html

例子1:二维坐标——整数

[root@typhoeus79 20131113]# ipython 
In [1]: import matplotlib.pyplot as plt In [2]: x = range(6) In [3]: plt.plot(x,[xi*xi for xi in x])
Out[3]: [<matplotlib.lines.Line2D at 0x1cf4050>] In [4]: plt.savefig('test1.png')

输出结果:

上图的例子可以看到直线不平滑,原因在于样本点太少的缘故。

例子2:二维坐标——浮点数

[root@typhoeus79 20131113]# ipython 
In [1]: import matplotlib.pyplot as plt
In [2]: import numpy as np
In [3]: x = np.arange(0.0,6.0,0.1)
In [4]: plt.plot(x,[xi * xi for xi in x])
Out[4]: [<matplotlib.lines.Line2D at 0x1cf1f10>]
In [5]: plt.savefig('test2.png')

range以及xrange是python中有的,而arange是numpy特有的。

输出结果:

例子3:二维坐标——多个曲线

[root@typhoeus79 20131113]# ipython 
In [1]: import matplotlib.pyplot as plt
In [2]: import numpy as np
In [4]: x = range(5)
In [5]: x
Out[5]: [0, 1, 2, 3, 4]
In [6]: plt.plot(x,[xi * 1.5 for xi in x])
Out[6]: [<matplotlib.lines.Line2D at 0x1cf2c50>]
In [7]: plt.plot(x,[xi * 3.0 for xi in x])
Out[7]: [<matplotlib.lines.Line2D at 0x1cf2ed0>]
In [8]: plt.plot(x,[xi / 3.0 for xi in x])
Out[8]: [<matplotlib.lines.Line2D at 0x1cf5590>]
In [9]: plt.savefig('test3.png')

输出结果:

例子4:二维坐标——多个曲线,改进版本

In [1]: import matplotlib.pyplot as plt
In [2]: import numpy as np
In [3]: x = range(1,5)
In [4]: plt.plot(x,[xi * 1.5 for xi in x],x,[xi * 3.0 for xi in x],x,[xi / 3.0 for xi in x])
Out[4]:
[<matplotlib.lines.Line2D at 0x1cf3150>,
<matplotlib.lines.Line2D at 0x1cf33d0>,
<matplotlib.lines.Line2D at 0x1cf3a90>] In [5]: plt.savefig('test4.png')

多个数据使用一个plot进行输出

例子5:二维坐标——多个曲线,使用numpy进行改进

In [1]: import matplotlib.pyplot as plt
In [2]: import numpy as np
In [3]: x = np.arange(1,5) In [4]: plt.plot(x,x*1.5,x,x*3.0,x,x/3.0)
Out[4]:
[<matplotlib.lines.Line2D at 0x1cf1fd0>,
<matplotlib.lines.Line2D at 0x1cf4290>,
<matplotlib.lines.Line2D at 0x1cf4950>] In [5]: plt.savefig('test5.png')

《Getting Started with Matplotlib》

最新文章

  1. 【HTML5&amp;CSS3进阶03】Jser与Csser如何一起愉快的翻新老组件
  2. 如何有效使用Project(2)——进度计划的执行与监控
  3. 20160808_Qt570安装
  4. Spring+Struts+Ibatis的配置
  5. android139 360 黑名单 增删改查-数据库操作
  6. js中的等号与非等号
  7. 浅谈js观察者模式
  8. Bluetooth LE(低功耗蓝牙) - 第二部分
  9. poj 3250 Bad Hair Day (单调栈)
  10. linux服务器开发二(系统编程)--线程相关
  11. PHP如何读取json数据
  12. leetCode之旅(14)-Number of 1 Bits
  13. SimpleDateFormat安全的时间格式化
  14. Java进阶篇设计模式之五-----外观模式和装饰器模式
  15. SqlServer 语法
  16. brew install
  17. D3算法编写决策树
  18. vue-cli创建的项目的目录结构及说明
  19. 【原】ActiveMq实现分布式事务一致性
  20. UML和模式应用4:初始阶段(6)--迭代方法中如何使用用例

热门文章

  1. WPF 验证没有通过无法保存数据(非常好)+ 虚似数据库
  2. 【爬虫入门01】我第一只由Reuests和BeautifulSoup4供养的Spider
  3. Android 设备兼容性(1)
  4. NDK Jni 开发(1)
  5. leetcode 697. Degree of an Array
  6. 剖析Prometheus的内部存储机制
  7. 总结HTML5
  8. Java基础-Random类(05)
  9. es6总结
  10. AI翻译离无障碍交流有多远