0
votes

I am going to plot the results of a for loop and if statement inside of it.

I want to have plot with lines but once I use plt.plot the plot will be empty but once I try scatter I have plot with dots. What should I do to have plot of Unu vs nu with lines not dots?

if inp==0:
        print('***')
        print('0 Is not acceptable ')
        print('***')
    else:
        for xx in range(1,819):
            ...# lines of code
            if inp<0:
                if lim > 1:
                    pass
                else:
                    nu = dfimppara.iloc[xx, 1] *115
                    plt.scatter(Unu(xx), nu)
            else:
                ...# lines of code
plt.show()
1
To plot lines you need more than one point (of course). Since you are plotting one point at a time, you can't see lines with plt.plot. Instead, what you need is to save your data points in some list/array and then plot them using plt.plot - Sheldore
@Sheldore yes, how can I do it at this code simultaneously with running the code - Ma Y
Initialize some lists after the first else and append them inside the second else with nu and xx. Then once the loop is finished, plot outside - Sheldore

1 Answers

1
votes

You code is not runnable but you can do something along the following lines:

  • Initialize two lists to store nu and Unu(xx)
  • Append them inside the for loop
  • Plot them outside the for loop

if inp==0:
    print('***')
    print('0 Is not acceptable ')
    print('***')
else:
    nu_list = []
    Unu_list = []
    for xx in range(1,819):
        ...# lines of code
        if inp<0:
            if lim > 1:
                pass
            else:
                nu_list.append(dfimppara.iloc[xx, 1] *115)
                Unu_list.append(Unu(xx))
            plt.plot(Unu_list, nu_list)
        else:
            ...
plt.show()