子图subplot

 
1)第一种方式subplot:

plt.figure()

 

plt.subplot(2,3,1)

plt.plot(x, y)

 

plt.subplot(232)

plt.bar(x, y)

 

plt.subplot(233)

plt.barh(x, y)

 

plt.subplot(234)

plt.bar(x, y)

y1 = [7,8,5,3]

plt.bar(x, y1, bottom=y, color = 'r')

 

plt.subplot(235)

plt.boxplot(x)

 

plt.subplot(236)

plt.scatter(x,y)

 

plt.show()

2)第二种方式add_subplot()

fig = plt.figure()

 

ax1 = fig.add_subplot(221)

ax1.plot(x, x)

 

ax2 = fig.add_subplot(222)

ax2.plot(x, -x)

 

ax3 = fig.add_subplot(223)

ax3.plot(x, x ** 2)

 

ax4 = fig.add_subplot(224)

ax4.plot(x, np.log(x))

 

plt.show()

3)第三种方式

fig, axes = plt.subplots(2, 2)

axes[0,0].hist(np.random.randn(500), bins=50, color='k', alpha=0.5)

axes[0,1].hist(np.random.randn(500), bins=50, color='r', alpha=0.5)

4)subplot的一些参数的设置

nrows            subplot的行数

ncols             subplot的列数

sharex            sharex=True使得所有subplot使用同一个X轴刻度(调节xlim将会影响所有的subplot)

sharey            sharey=True使得所有subplot使用同一个Y轴刻度(调节ylim将会影响所有的subplot)

subplot_kw    用于创建subplot的关键字字典

**fig_kw       创建figure时的其他关键字

 

plt.subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None, hspace=None)

 

fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)

for i in range(2):

    for j in range(2):

        axes[i, j].hist(randn(500), bins=50, color='k', alpha=0.5)

 

plt.subplots_adjust(wspace=0, hspace=0)

 

 

转载于:https://www.cnblogs.com/yongfuxue/p/10107282.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值