Python Forum
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 !!
Reply
#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.
Reply
#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.
Reply
#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[:]
Reply
#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".
Reply
#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?
Reply
#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'>
Reply
#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.
Reply
#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.
Reply
#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 .
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matplotlib scatter plot in loop with None values ivan_sc 1 2,236 Nov-04-2021, 11:25 PM
Last Post: jefsummers
  How to create correct scatter plot for PCA? LK91 0 2,069 Dec-11-2019, 07:53 PM
Last Post: LK91
  Substituting Values in Plot Hotdog1 2 3,029 Nov-11-2017, 08:07 PM
Last Post: Hotdog1

Forum Jump:

User Panel Messages

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