Bottom Page

• 2 Vote(s) - 3 Average
• 1
• 2
• 3
• 4
• 5
 Plot correct values Felipe Silly Frenchman Posts: 44 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 2 #1 Feb-16-2017, 01:38 PM (This post was last modified: Feb-16-2017, 01:41 PM by Felipe. Edited 1 time in total.) 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[0].plot(X,B,'-',X,originB,'--')                 axarr[0].set_title('Modulo')                 axarr[1].plot(X,T,'-',X,originT,'--')                 axarr[1].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 !! merlem Wafer-Thin Wafer Posts: 82 Threads: 11 Joined: Sep 2016 Reputation: 4 Likes received: 4 #2 Feb-16-2017, 04:03 PM 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 of`originT = 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. Felipe Silly Frenchman Posts: 44 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 2 #3 Feb-16-2017, 04:45 PM (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 of`originT = 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. merlem Wafer-Thin Wafer Posts: 82 Threads: 11 Joined: Sep 2016 Reputation: 4 Likes received: 4 #4 Feb-16-2017, 05:13 PM (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[:]` Felipe Silly Frenchman Posts: 44 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 2 #5 Feb-16-2017, 06:17 PM (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". merlem Wafer-Thin Wafer Posts: 82 Threads: 11 Joined: Sep 2016 Reputation: 4 Likes received: 4 #6 Feb-16-2017, 07:05 PM 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? Felipe Silly Frenchman Posts: 44 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 2 #7 Feb-16-2017, 10:43 PM (This post was last modified: Feb-16-2017, 10:48 PM by Felipe.) (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: `````` merlem Wafer-Thin Wafer Posts: 82 Threads: 11 Joined: Sep 2016 Reputation: 4 Likes received: 4 #8 Feb-17-2017, 08:29 AM 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. Felipe Silly Frenchman Posts: 44 Threads: 17 Joined: Jan 2017 Reputation: 0 Likes received: 2 #9 Feb-17-2017, 11:57 AM (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. merlem Wafer-Thin Wafer Posts: 82 Threads: 11 Joined: Sep 2016 Reputation: 4 Likes received: 4 #10 Feb-18-2017, 09:21 AM 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 . « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post How to create correct scatter plot for PCA? LK91 0 187 Dec-11-2019, 07:53 PM Last Post: LK91 Substituting Values in Plot Hotdog1 2 1,220 Nov-11-2017, 08:07 PM Last Post: Hotdog1

Forum Jump:

Users browsing this thread: 1 Guest(s)