画图功能总结(1):http://blog.csdn.net/mmc2015/article/details/47746195
1、回忆之前,快速入门:
# -*- coding: utf-8 -*- 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()
载入matplotlib中的绘图函数子库pyplot:
import matplotlib.pyplot as plt调用figure创建一个绘图对象,并且使它成为当前的绘图对象:
plt.figure(figsize=(8,4))(figsize参数可以指定绘图对象的宽度和高度,单位为英寸)
调用plot函数在当前绘图对象中画图:
plt.figure(figsize=(8,4)) plt.plot(x,y,label="$sin(x)$",color="red",linewidth=2)其他说明:
2、绘制多轴图(多个子图):
一个绘图对象(figure)可以包含多个轴(axis),在Matplotlib中用轴表示一个绘图区域,可以将其理解为子图。可以使用subplot函数快速绘制有多个轴的图表。subplot函数的调用形式如下:
subplot(numRows, numCols, plotNum)
subplot将整个绘图区域等分为numRows行 * numCols列个子区域,然后按照从左到右,从上到下的顺序对每个子区域进行编号,左上的子区域的编号为1。如果numRows,numCols和plotNum这三个数都小于10的话,可以把它们缩写为一个整数,例如subplot(323)和subplot(3,2,3)是相同的。subplot在plotNum指定的区域中创建一个轴对象。如果新创建的轴和之前创建的轴重叠的话,之前的轴将被删除。
创建3行2列共6个轴,通过axisbg参数给每个轴设置不同的背景颜色。
for idx, color in enumerate("rgbyck"): plt.subplot(320+idx+1, axisbg=color) plt.show()
plt.subplot(221) # 第一行的左图 plt.subplot(222) # 第一行的右图 plt.subplot(212) # 第二整行 plt.show()