【matplotlib】 之 plt.subplot

本文介绍了使用Matplotlib库绘制子图的方法,并通过实例展示了如何设置子图布局、背景颜色等属性。此外还提供了多个子图绘制的例子,帮助读者更好地理解和掌握子图的绘制技巧。

____tz_zs

matplotlib.pyplot.subplot(*args, **kwargs)

 

返回一个指定子图分布的网格位置的 figure 对象。

官网链接matplotlib.pyplot.subplot

 

参数:

subplot(nrows, ncols, index, **kwargs)

分别指定 (行数,列数,位置)

其他参数:

  • facecolor: 背景色
  • polar: 是否用极坐标投影
  • projection: 投影

 

官网示例代码:

import matplotlib.pyplot as plt
# plot a line, implicitly creating a subplot(111)
plt.plot([1,2,3])
# now create a subplot which represents the top plot of a grid
# with 2 rows and 1 column. Since this subplot will overlap the
# first, the plot (and its axes) previously created, will be removed
plt.subplot(211)
plt.plot(range(12))
plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background

.

.

#!/usr/bin/python2.7
# -*- coding:utf-8 -*-

"""
@author:    tz_zs
"""

import numpy as np
import matplotlib.pyplot as plt


x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)

y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)

plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')

plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')

plt.show()

.

·


# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import numpy as np
import matplotlib.pyplot as plt
 
square = np.zeros((32, 32))
 
square[5:10, 5:10] = 1
plt.subplot(221)
plt.imshow(square)
 
square[15:20, 5:10] = 1
plt.subplot(222)
plt.imshow(square)
 
square[5:10, 15:20] = 1
plt.subplot(223)
plt.imshow(square)
 
square[15:20, 15:20] = 1
plt.subplot(224)
plt.imshow(square)

.

.

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值