1. 系统环境
系 统 :CentOS 6.7 64位
python : 2.6.6 (系统自带)
IDE :pycharm 社区版
2. 搭建绘图环境
#yum install python-matplotlib
3. 画图
第一个例子,很经典,画点线图
代码如下:
#!/usr/bin/python
import numpy as np
import pylab as pl
x = [1, 2, 3, 4, 5] # Make an array of x values
y = [1, 4, 9, 16, 25] # Make an array of y values for each x value
pl.plot(x, y) # use pylab to plot x and y
pl.show() # show the plot on the screen
效果如图:
将pl.plot(x, y) 改为 pl.plot(x, y, 'o') ,可得到下面的图:
-------------------------------------------------
第二个例子,代码如下:
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
xData = np.arange(0, 10, 1)
yData1 = xData.__pow__(2.0)
yData2 = np.arange(15, 61, 5)
plt.figure(num=1, figsize=(8, 6))
plt.title('Plot 1', size=14)
plt.xlabel('x-axis', size=14)
plt.ylabel('y-axis', size=14)
plt.plot(xData, yData1, color='b', linestyle='--', marker='o', label='y1 data')
plt.plot(xData, yData2, color='r', linestyle='-', label='y2 data')
plt.legend(loc='upper left')
plt.savefig('/home/plot1.png', format='png')
效果如下图:
----------------------------------------------------------
第三个例子,代码如下
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 1000)
y = np.sin(x)
z = np.cos(x**2)
plt.figure(figsize=(8,4))
plt.plot(x,y,label="sin(x)",color="red",linewidth=2)
plt.plot(x,z,"b--",label="cos(x^2)")
plt.xlabel("Time(s)")
plt.ylabel("Volt")
plt.title("PyPlot First Example")
plt.ylim(-1.2,1.2)
plt.legend()
plt.show()
效果如下图:
4. 参考文献
[1] http://www.jb51.net/article/67626.htm
[2] http://old.sebug.net/paper/books/scipydoc/matplotlib_intro.html
[3] http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html
---