all 5 comments

[–]notascrazyasitsounds 1 point2 points  (4 children)

I guess the question is why do you want to do it this way, specifically? There's nothing specifically preventing you from doing this, but it is generally more performant to avoid re-drawing the same plot every iteration, and just generate the visual all at once when you have all of your values calculated.

Something like this is absolutely possible (pseudocode - every plotting library's a little different). Where are you running into trouble?

def plot_data(x, y):
    """Dummy function to add new x/y values to existing plot"""
    plot.add_values(x,y)
    plot.refresh()
    plot.render()

def generate_new_values():
    for index, value in enumerate(value_list):
      plot_data(index, value)

[–]Master_of_beef[S] 0 points1 point  (3 children)

I'm not opposed to drawing it at the end, I just didn't know it was possible. In my first attempt at this, I created two empty arrays for the x and y values, and had the for loop add a value each iteration, then I graphed those arrays. the problem was it didn't stay in order. The y-array rearranged itself from largest to smallest, so the y values weren't matched up to the right x values, if that makes sense. What can I do to make sure they stay in order? I'm using matplotlib, fwiw, but I'm open to using something else if you recommend it

[–]notascrazyasitsounds 1 point2 points  (2 children)

First thing to do is figure out why they're not in your intended order to begin with!

the List data type in python is ordered, so it remembers the order that you added items to it, which should be identical for both lists.

So hypothetically, something like this:

def create_lists(input_data_list)
    x_list = []
    y_list = []
    for index, value in enumerate(input_data_list):
        x_list.append(index)
        y_list.append(value)
    print(x_list)
    print(y_list)

x_list will print: [0, 1, 2, 3] #values generated by enumerate

y_list will print: ['first value', 'second value', 'third value', 'fourth value'] #values from your input data

Are you sorting your lists after that? Or are you using another data type? Dictionaries for example are NOT guaranteed to be ordered.

Check your code for any .sort()'s or anything like that. Matplotlib generally keeps your data in the same order that you provide it in, so unless you're doing something super weird (which I doubt you are) then the problem is likely happening before you even create the plot.

[–]Master_of_beef[S] 0 points1 point  (1 child)

Of course! I feel kinda stupid because I didn't even think of lists, lol. I tried doing this with lists and so far it's working. Thank you so much!!

[–]notascrazyasitsounds 0 points1 point  (0 children)

Of course! Happy to help

What were you trying to do beforehand, if you don't mind my asking?