Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loop, graph
#1
Hi, everyone
I have list with data for graph (image below) like this [['1.9078244', '0.49999094'], ...] where the first argument is x, and the second one is y.
I need to find a y, where the x is less then 0.5 and in the previous 20,000 values of x the difference between the minimum and maximum values is less than 0.1
Unfortunately i can't add file with data to forum, it's too big. So i delete bigger part from alpha.txt, because of that you can change 20,000 values to something smaller
I wrote code, but loop (for m in range(len(results)):) works only with first m where x < 5, but doesn't continue and doesn't try another m

import matplotlib.pyplot as plt

with open('alpha.txt', mode='r') as soubor:       # get data from file
    results = []
    for k, i in enumerate(soubor):
        if k > 3:
            line = i.strip().split(" ")
            line = list(filter(None, line))
            #if float(radka[1]) < 0.5:
            results.append(line)

    last_res = []                                            #here i have all data in the list like this [['1.9078244', '0.49999094'], ...]
    for m in range(len(results)):
        if float(results[m][1]) < 0.5:                 # find m where x < 0.5
            m_list = []
            for num in range((m-20000), m+1):            # create a list with previous 20,000 values of x
                m_list.append(float(results[num][1]))
            if (max(m_list) - min(m_list)) < 0.5:         # [b]check difference between the minimum and maximum of list with 20,000 values, it's works only when <0.5, but i need 0.1[/b]
                last_res.append(results[m])                 #if it's find correct x, stop the loop
                break                                                
            else:
                continue



    print(m_list)                                        # print data for control
    print((max(m_list) - min(m_list)))
    print(last_res)
    x_1, y_1 = float(last_res[0][0]), float(last_res[0][1])        #create staff fo graph
    x, y = [], []
    for item in results:
        y.append(float(item[1]))
        x.append(float(item[0]))

# plotting the points
plt.plot(x, y)
plt.scatter(x_1, y_1, label= "stars", color= "green",
            marker= "*", s=30)

# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')

# giving a title to my graph
plt.title('My first graph!')

# function to show the plot
plt.show()

Attached Files

.txt   alpha.txt (Size: 190.91 KB / Downloads: 82)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020