Matplotlib子图函数:使用for循环在子图中的矩阵中绘制每一行

时间:2017-07-23 12:14:30

标签: python matplotlib

我有一个函数返回一个矩阵,该矩阵具有不同的行但是常数列(10),例如(5,10),(7,10),(20,10)等。

使用恒定的x轴矩阵绘制矩阵得到以下结果 plot 1

现在,尝试使用代码将每个绘图添加到单独的子图中

#the constant x-axis is
myx = [  6.89668072, 6.79190465,  6.48075998e+00 ,  5.97270071, 5.28316394 , 4.43310092,   3.44834036,  2.35880373,  1.19759604e+00 ,  4.22299899e-16]
#the y axis for this example is 
finalyy =[np.array([ 0.        ,  0.19801812,  0.32703622,  0.39731833,  0.43205176,
0.44652588,  0.44920819,  0.44348252,  0.430474  ,  0.40601885]), np.array([ 0.        ,  0.18017484,  0.30180713,  0.37321907,  0.41381173,
0.43625179,  0.44750785,  0.44986628,  0.44364735,  0.42256948]), np.array([ 0.        ,  0.16233156,  0.27657803,  0.3491198 ,  0.3955717 ,
0.4259777 ,  0.44580751,  0.45625005,  0.4568207 ,  0.43912011]), np.array([ 0.        ,  0.14448829,  0.25134894,  0.32502053,  0.37733167,
0.41570361,  0.44410717,  0.46263381,  0.46999405,  0.45567074]), np.array([ 0.        ,  0.12664501,  0.22611984,  0.30092126,  0.35909164,
0.40542952,  0.44240682,  0.46901757,  0.4831674 ,  0.47222137])]

#getting the value of rows in finalyy matrix
last = np.array(finalyy).shape
finalval = last[0]
m = 3 #used below to define number of subplots

#creating subplot numbers eg 331 332 333 334 335 etc
empty = []
for x in range (1,finalval+1):
    mat = (str(m)+ str(m)+str(x))
    empty.append(mat)
finalempty = np.asarray(empty)

#trying to plot each row in finalyy matrix whilst using each of the subplot index above
fig = plt.figure()
for row in finalyy:
    for j in finalempty:
      ax1 = fig.add_subplot(j)

    ax1.plot(myx,row,  'r-')

plt.tight_layout()
plt.show()

代码也生成了这个 Plot 2

我想使用函数在子图中绘制每条曲线。

1 个答案:

答案 0 :(得分:0)

ax1.plot(myx,row, 'r-')将所有行绘制到ax1,这是最后一个轴。

使用plt.subplots创建子图可以简化您的问题。

import numpy as np
import matplotlib.pyplot as plt
myx = [  6.89668072, 6.79190465,  6.48075998e+00 ,  5.97270071, 5.28316394 , 4.43310092,  
         3.44834036,  2.35880373,  1.19759604e+00 ,  4.22299899e-16]
#the y axis for this example is 
finalyy =[np.array([ 0.        ,  0.19801812,  0.32703622,  0.39731833,  0.43205176,
                    0.44652588,  0.44920819,  0.44348252,  0.430474  ,  0.40601885]), 
            np.array([ 0.        ,  0.18017484,  0.30180713,  0.37321907,  0.41381173,
                      0.43625179,  0.44750785,  0.44986628,  0.44364735,  0.42256948]), 
            np.array([ 0.        ,  0.16233156,  0.27657803,  0.3491198 ,  0.3955717 ,
                      0.4259777 ,  0.44580751,  0.45625005,  0.4568207 ,  0.43912011]), 
            np.array([ 0.        ,  0.14448829,  0.25134894,  0.32502053,  0.37733167,
                      0.41570361,  0.44410717,  0.46263381,  0.46999405,  0.45567074]),
            np.array([ 0.        ,  0.12664501,  0.22611984,  0.30092126,  0.35909164,
                      0.40542952,  0.44240682,  0.46901757,  0.4831674 ,  0.47222137])]

m = 3 #used below to define number of subplots

fig, axes = plt.subplots(nrows=m, ncols=m)
for ax, row in zip(axes.flatten(), finalyy):
    ax.plot(myx,row,  'r-')

# turn remaining axes off
for i in range(len(finalyy),m**2):
    axes.flatten()[i].axis("off")

plt.tight_layout()
plt.show()