0

I have a function that returns a matrix with varying rows but constant columns (10) e.g (5, 10), (7, 10) , (20,10) etc.

Plotting the matrix using a constant x-axis matrix yields the following plot 1

Now, trying to use code to add each plot into a separate subplot

#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()

the code also generates this Plot 2

I want to plot each curve in a subplot using a function.

1 Answer 1

1

ax1.plot(myx,row, 'r-') plots all rows to ax1, which is the last of the axes.

Your problem can be simplified by using plt.subplots to create the 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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.