Bottom Page

Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Plot correct values
#1
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 !!
Quote
#2
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.
Quote
#3
(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.
Quote
#4
(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[:]
Quote
#5
(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".
Quote
#6
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?
Quote
#7
(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:
<class 'numpy.ndarray'> <class 'numpy.ndarray'>
Quote
#8
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.
Quote
#9
(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.
Quote
#10
It was a pleasure for me Smile  .

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 Smile .
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Substituting Values in Plot Hotdog1 2 950 Nov-11-2017, 08:07 PM
Last Post: Hotdog1

Forum Jump:


Users browsing this thread: 1 Guest(s)