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)

 

 

版权声明:本文为yongfuxue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yongfuxue/p/10107282.html