 Plot correct values - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding) +--- Forum: Data Science (https://python-forum.io/Forum-Data-Science) +--- Thread: Plot correct values (/Thread-Plot-correct-values) Plot correct values - Felipe - Feb-16-2017 Hi guys, I wrote a code to make corrections in array data and plot the after/before for that. Here's my code: while ctr2 == 'R':                 cte = float(input('Type the constant: '))                 plt.close()                 originB = B                 originT = T                 B = B + cte                 dx = len(T)                 for i in range(dx):                     if T[i] >= 0:                         print(T[i])                         T[i] = T[i] + cte                         print(T[i])                     else:                         T[i] = T[i] - cte                 print(T)                 f, axarr = plt.subplots(2, sharex=True)                 axarr.plot(X,B,'-',X,originB,'--')                 axarr.set_title('Modulo')                 axarr.plot(X,T,'-',X,originT,'--')                 axarr.set_title('Anomalia')                 plt.show(block=False)                 ctr2 = input('Type R to remove a constant or E to exit: ') I store the the values before changes in "originB" and "originT" arrays. So I use the value of the constant "cte" to performe the corrections. For the "B" array it's a simple sum, but for the "T" array I need to check the elements to decide if I sum or subtract the value of "cte". When I plot, the graph for "B" it's ok, but for "T" it plots in the same position of the values stored in "originT". Look that I put some prints to check if the values of "T" are changing correctly and they are. Here's my output. The values for B are plotted in the subplot called "Modulo" and for T in the subplot called "Anomalia".  For this example I used the values for "originB" and "originT" equals to zero and for "cte" equal to one: Output: [img]https://drive.google.com/open?id=0B-HZ-2g-lyoUX19IeG4yR19WQU0[/img] I appreciate the help !! RE: Plot correct values - merlem - Feb-16-2017 I'm not sure whether I understand...Shall the values of OriginT and T be always the same, both changing during code execution? Or shall one change and the other remain untouched? (In that case, think about the meaning oforiginT = T.) And, by the way, I have no idea to which library I should assign 'plt'. For code testing that could be helpful to know. RE: Plot correct values - Felipe - Feb-16-2017 (Feb-16-2017, 04:03 PM)merlem Wrote: I'm not sure whether I understand...Shall the values of OriginT and T be always the same, both changing during code execution? Or shall one change and the other remain untouched? (In that case, think about the meaning oforiginT = T.) And, by the way, I have no idea to which library I should assign 'plt'. For code testing that could be helpful to know. So sorry, my mistake. The library is matplotlib.pyplot. I used import matplotlib.pyplot as plt. The idea is use "originT" to store the value of "T" every time that enter in the while. So I can change the value of "T" and plot the previous and posterior values on the end of every while. After that, I can decide if I enter again on the while, typing R or E. Every time that I enter in the while, "originT" stores a new value. RE: Plot correct values - merlem - Feb-16-2017 (Feb-16-2017, 04:45 PM)Felipe Wrote: The idea is use "originT" to store the value of "T" every time that enter in the while. So I can change the value of "T"... And what happens to the value of "originT" in that moment? Maybe try a copy instead, it can be done with a minor code change: originT = T[:] RE: Plot correct values - Felipe - Feb-16-2017 (Feb-16-2017, 05:13 PM)merlem Wrote: (Feb-16-2017, 04:45 PM)Felipe Wrote: The idea is use "originT" to store the value of "T" every time that enter in the while. So I can change the value of "T"... And what happens to the value of "originT" in that moment? Maybe try a copy instead, it can be done with a minor code change: originT = T[:] I tried that and doesn't work. I really don't understand the problem. I use: originB = B B = B + cte Them, when I print "originB" and "B" that's ok. These arrays has different values. But if I use: originT = T dx = len(T) for i in range(dx):     if T[i] >= 0:         T[i] = T[i] + cte     else:         T[i] = T[i] - cte When I print "originT" and "T", they have the same value. Why? If change "T" means change "originT", the same would be true for "originB" and "B". RE: Plot correct values - merlem - Feb-16-2017 There's a difference between a value (as B is) and a list (as T is). If my understanding of python is correct (I'm not absolute sure in this point), then originB = B makes originB being the same as B, that is, whatever number B is at that time. If I am not wrong, it's kind of a link to a number, and as this number itself is immutable, B = B + cte cannot change originB's target. Therefore, only B is changed. However, T links to a list. And you will not get a copy of this list, but another link to this list. And the list items are not immutable. Therefore, when you change them, the change will apply to both links, originT and T.  Therefore, even if the expressions look excactly the same, different things happen, because one of the starting items is a number and the other one is not. I only wonder why the "fast copy" does not work. Unfortunately, I can't do experiments with your code by myself, as I don't have mathplotlib. Maybe try other ways to get a copy? RE: Plot correct values - Felipe - Feb-16-2017 (Feb-16-2017, 07:05 PM)merlem Wrote: There's a difference between a value (as B is) and a list (as T is). If my understanding of python is correct (I'm not absolute sure in this point), then originB = B makes originB being the same as B, that is, whatever number B is at that time. If I am not wrong, it's kind of a link to a number, and as this number itself is immutable, B = B + cte cannot change originB's target. Therefore, only B is changed. However, T links to a list. And you will not get a copy of this list, but another link to this list. And the list items are not immutable. Therefore, when you change them, the change will apply to both links, originT and T.  Therefore, even if the expressions look excactly the same, different things happen, because one of the starting items is a number and the other one is not. I only wonder why the "fast copy" does not work. Unfortunately, I can't do experiments with your code by myself, as I don't have mathplotlib. Maybe try other ways to get a copy? Thanks very much for the explanation !! I'll search for other ways to get a copy as you suggest. I came from C /MatLab and that doesn't happen in those languages . I need more attention for this question of list, cause I used numpy.array to create "B" and "T". So, I thought that originB = B and originT = T creates arrays too. Well the most strange is that when I use: print(type(originT)) print(type(T)) The output is: Output: RE: Plot correct values - merlem - Feb-17-2017 I'm not familiar with numpy, so I can't say what that type is. A short check of some numpy docs makes me assume that it has something to do with the 'relationship' of numpy to C++. However, it has it's own copy method. originT = t.copy() should provide an independend ndarray. RE: Plot correct values - Felipe - Feb-17-2017 (Feb-17-2017, 08:29 AM)merlem Wrote: I'm not familiar with numpy, so I can't say what that type is. A short check of some numpy docs makes me assume that it has something to do with the 'relationship' of numpy to C++. However, it has it's own copy method. originT = t.copy() should provide an independend ndarray. Wow, thanks, thanks, thanks !!! That worked perfectly for me. I just add T.copy() and B.copy() to my code and this minor correction solved the problem. Thanks for the patience and help with the solution. RE: Plot correct values - merlem - Feb-18-2017 It was a pleasure for me . I still need tons of help for myself, so it's nice to see that bit by bit I become experienced enough to be helpful to others, too .