Matplotlib绘制子图

Matplotlib中绘制子图需要subplot函数,该函数的定义如下:

image

基本的使用方法如下

    
    
    
    
import numpy as np import matplotlib.pyplot as plt plt.subplot( 3 , 1 , 1 ) plt.plot([ 1 , 2 , 3 ]) plt.subplot( 3 , 1 , 2 ) plt.plot([ 1 , 4 , 5 ]) plt.subplot( 3 , 1 , 3 ) plt.plot([ 1 , 3 , 10 ]) plt.show()

上述代码的执行效果如下:

image

subplot方法还有一种简写格式,如果三个参数都是小于10的,可以用一个参数代替,直接把逗号删除,如下面的代码和上面的代码执行效果是一样的。

    
    
    
    
import matplotlib.pyplot as plt plt.subplot( 311 ) plt.plot([ 1 , 2 , 3 ]) plt.subplot( 312 ) plt.plot([ 1 , 4 , 5 ]) plt.subplot( 313 ) plt.plot([ 1 , 3 , 10 ]) plt.show()

你可能感兴趣的:(Matplotlib绘制子图)