2

In my code I have multiple plots I want to automate, I use a for loop to run through code and create 1 plot per loop. However the code seems to only create the final plot. This seems to work if I call the function in a loop but I don't understand why it wouldn't create all the plots the way I do it here.

for i in range(num_x):
        curr = usehdr[xcols[i]]

        for j in range(num_y):
            domain = data[:,xcols[i] - 1]
            image  = data[:,ycols[j] - 1]
            plt.plot(domain,image,'.', color = 'black')
            plt.xlabel(curr + ' NN distances (Mpc)')
            plt.ylabel(usehdr[ycols[j]])
            plt.title('D4000_N vs NN distances')

the i variable is the amount of plots where the x data set is changed the j variable is the amount of plots where the y data set is change. Usually i goes from 0 to 9 inclusive and j executes only once.

1 Answer 1

2

I fixed it, figured out it is very similar to MATLAB and I needed to implement subplot to retain my other plots. Now this code works:

for i in range(num_x):
    curr = usehdr[xcols[i]]
    
    for j in range(num_y):
        domain = data[:,xcols[i] - 1]
        image  = data[:,ycols[j] - 1]
        plt.subplot(num_x,num_y,i+1)
        plt.plot(domain,image,'.', color = 'black')
        plt.xlabel(curr + ' NN distances (Mpc)')
        plt.ylabel(usehdr[ycols[j]])
        plt.title('D4000_N vs NN distances')

or alternatively, using plt.show() for several separate plots.

for i in range(num_x):
    curr = usehdr[xcols[i]]
    
    for j in range(num_y):
        domain = data[:,xcols[i] - 1]
        image  = data[:,ycols[j] - 1]
        plt.plot(domain,image,'.', color = 'black')
        plt.xlabel(curr + ' NN distances (Mpc)')
        plt.ylabel(usehdr[ycols[j]])
        plt.title('D4000_N vs NN distances')
        plt.show()
Sign up to request clarification or add additional context in comments.

1 Comment

or alternatively the code works with plt.show() in the loop as well if you want several separate plots.

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.