当前位置: 首页 > 图灵资讯 > 行业资讯> python中subplot函数怎么画图?

python中subplot函数怎么画图?

来源:图灵python
时间: 2024-07-30 10:04:01

说明

1、可以通过调用subplot()函数创建子图,可以在子图上绘制程序。subplot(nrows、ncols、index、**kwargs)函数的nrows参数指定数据图区域分为多少行,ncols参数指定数据图区域分为多少列,index参数指定获得多少区域。

2、subplot()函数还支持直接输入三位数参数,第一位数为nrows参数;第二位数为ncols参数;第三位数为index参数。

参数

nrows: subplot的行数

ncols: subplot列数

sharex :所有subplot都应使用相同的x轴刻度(调整xlim会影响所有subplot)

sharey: 所有subplot应使用相同的Y轴刻度(调整ylim会影响所有subplot)

subplot_kw: 用于创建每个subplot的关键字典

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

实例

importnumpyasnp
importmatplotlib.pyplotasplt
x=np.arange(0,100)
#作图1
plt.subplot(2,2,1)#等效于plt.subplot(221)
plt.plot(x,x)
#作图2
plt.subplot(2,2,2)
plt.plot(x,-x)
#作图3
plt.subplot(2,2,3)
plt.plot(x,x**2)
plt.grid(color='r',linestyle='--',linewidth=1,alpha=0.3)
#作图4
#plt.subplot(224)
#plt.plot(x,np.log(x))
plt.show()

以上是python中subplot函数画图的方法,希望对大家有所帮助。更多Python学习指导:python基础教程

本文教程操作环境:windows7系统Python 3.9.1,DELL G3电脑。