Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17915 > unrolled thread

Plot seems weird

Started byYigit Turgut <y.turgut@gmail.com>
First post2011-12-25 07:33 -0800
Last post2011-12-27 21:54 +1100
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Plot seems weird Yigit Turgut <y.turgut@gmail.com> - 2011-12-25 07:33 -0800
    Re: Plot seems weird Rick Johnson <rantingrickjohnson@gmail.com> - 2011-12-25 09:06 -0800
      Re: Plot seems weird Yigit Turgut <y.turgut@gmail.com> - 2011-12-25 10:27 -0800
        Re: Plot seems weird Lie Ryan <lie.1296@gmail.com> - 2011-12-26 20:28 +1100
          Re: Plot seems weird Yigit Turgut <y.turgut@gmail.com> - 2011-12-26 09:08 -0800
            Re: Plot seems weird Lie Ryan <lie.1296@gmail.com> - 2011-12-27 05:58 +1100
              Re: Plot seems weird Yigit Turgut <y.turgut@gmail.com> - 2011-12-26 11:14 -0800
                Re: Plot seems weird Lie Ryan <lie.1296@gmail.com> - 2011-12-27 21:54 +1100

#17915 — Plot seems weird

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-25 07:33 -0800
SubjectPlot seems weird
Message-ID<880a2ed3-1a3f-45e7-b439-e87289403fd9@t16g2000vba.googlegroups.com>
Hi all,

I have a text file as following;

0.200047	0.000000
0.200053	0.160000
0.200059	0.000000
0.200065	0.080000
0.200072	0.000000
0.200078	0.160000

And I am trying to plot it with ;

filenames = sys.argv[1:]
if len(filenames) == 0:
    filenames = [sys.stdin]
for filename in filenames:
    t,y1 = numpy.genfromtxt(filename, unpack=True)
    pyplot.plot(t,y1)
    pyplot.show()

But graph seems weird, not as it supposed to be. Any ideas ?

[toc] | [next] | [standalone]


#17921

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2011-12-25 09:06 -0800
Message-ID<c1ae9905-da26-4513-a456-99941ac53036@dp8g2000vbb.googlegroups.com>
In reply to#17915
On Dec 25, 9:33 am, Yigit Turgut <y.tur...@gmail.com> wrote:
> Hi all,
>
> I have a text file as following;
>
> 0.200047        0.000000
> 0.200053        0.160000
> 0.200059        0.000000
> 0.200065        0.080000
> 0.200072        0.000000
> 0.200078        0.160000
>
> And I am trying to plot it with ;
>
> filenames = sys.argv[1:]
> if len(filenames) == 0:
>     filenames = [sys.stdin]
> for filename in filenames:
>     t,y1 = numpy.genfromtxt(filename, unpack=True)
>     pyplot.plot(t,y1)
>     pyplot.show()
>
> But graph seems weird, not as it supposed to be. Any ideas ?


Interesting. Of course "weird" leaves a LOT to be desired. On a scale
of 1-10, how "weird" is the result?

But seriously. Have you tried debugging yet? If not, test these
points:

 * What is the value of "filenames" BEFORE the loop?
 * What is the value of "t" and "y1" for each iteration?

Also observe this wonderful phenomenon:

py> [] or [1,2,3]
[1, 2, 3]
py> [] or None or '' or () or {} or [1,2,3] or "1,2,3"
[1, 2, 3]

[toc] | [prev] | [next] | [standalone]


#17927

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-25 10:27 -0800
Message-ID<4f709686-4b07-4096-a87c-3e4bf8571c73@v13g2000yqc.googlegroups.com>
In reply to#17921
On Dec 25, 7:06 pm, Rick Johnson <rantingrickjohn...@gmail.com> wrote:
> On Dec 25, 9:33 am, Yigit Turgut <y.tur...@gmail.com> wrote:
> > Hi all,
>
> > I have a text file as following;
>
> > 0.200047        0.000000
> > 0.200053        0.160000
> > 0.200059        0.000000
> > 0.200065        0.080000
> > 0.200072        0.000000
> > 0.200078        0.160000
>
> > And I am trying to plot it with ;
>
> > filenames = sys.argv[1:]
> > if len(filenames) == 0:
> >     filenames = [sys.stdin]
> > for filename in filenames:
> >     t,y1 = numpy.genfromtxt(filename, unpack=True)
> >     pyplot.plot(t,y1)
> >     pyplot.show()
>
> > But graph seems weird, not as it supposed to be. Any ideas ?
>
> Interesting. Of course "weird" leaves a LOT to be desired. On a scale
> of 1-10, how "weird" is the result?

I apply a 1Khz test signal just to see if things run smoothly, but I
see spikes at lower and higher ends (logic 0,1) where I should see a
clean rectangle pwm signal. By the look of it I say weirdness is
around 3/10.

>
> But seriously. Have you tried debugging yet? If not, test these
> points:
Yes I double checked it, there seems to be nothing wrong in debug.

>  * What is the value of "filenames" BEFORE the loop?

Filename is argument 1 of the startup action.

>  * What is the value of "t" and "y1" for each iteration?

I test with both low and mid frequency signals (50Hz - 1Khz), same
inconsistency.

>
> Also observe this wonderful phenomenon:
>
> py> [] or [1,2,3]
> [1, 2, 3]
> py> [] or None or '' or () or {} or [1,2,3] or "1,2,3"
> [1, 2, 3]

Beautiful. I convert my arrays to string before writing to file.
Original post contains a fragment of the whole file. Data is
fluctuating, not a linear behavior.

[toc] | [prev] | [next] | [standalone]


#17954

FromLie Ryan <lie.1296@gmail.com>
Date2011-12-26 20:28 +1100
Message-ID<mailman.4099.1324891710.27778.python-list@python.org>
In reply to#17927
On 12/26/2011 05:27 AM, Yigit Turgut wrote:
> On Dec 25, 7:06 pm, Rick Johnson<rantingrickjohn...@gmail.com>  wrote:
>> On Dec 25, 9:33 am, Yigit Turgut<y.tur...@gmail.com>  wrote:
>>> Hi all,
>>
>>> I have a text file as following;
>>
>>> 0.200047        0.000000
>>> 0.200053        0.160000
>>> 0.200059        0.000000
>>> 0.200065        0.080000
>>> 0.200072        0.000000
>>> 0.200078        0.160000
>>
>>> And I am trying to plot it with ;
>>
>>> filenames = sys.argv[1:]
>>> if len(filenames) == 0:
>>>      filenames = [sys.stdin]
>>> for filename in filenames:
>>>      t,y1 = numpy.genfromtxt(filename, unpack=True)
>>>      pyplot.plot(t,y1)
>>>      pyplot.show()
>>
>>> But graph seems weird, not as it supposed to be. Any ideas ?
>>
>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale
>> of 1-10, how "weird" is the result?
>
> I apply a 1Khz test signal just to see if things run smoothly, but I
> see spikes at lower and higher ends (logic 0,1) where I should see a
> clean rectangle pwm signal. By the look of it I say weirdness is
> around 3/10.

What are you expecting? Your data produces something that looks like the 
plot on the right of this screenshot 
(http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with 
that; if you are expecting a square-wave-like plot (like on the left), 
then you should use a square-wave-like data, pyplot wouldn't magically 
transform a spiked-plot to squared-plot.

Here's what I use to convert the data on right plot to data on left 
plot, I don't know much about numpy so it might be possible to do it 
more efficiently or numpy might even have something like it already.

from itertools import izip_longest
def to_square(t, y1):
     sq_data = [[], []]
     for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
         sq_data[0].append(x)
         sq_data[1].append(y)
         sq_data[0].append(xn)
         sq_data[1].append(y)
     return numpy.array(sq_data, dtype=float)

[toc] | [prev] | [next] | [standalone]


#17963

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-26 09:08 -0800
Message-ID<74f9b578-ab36-4962-a353-ec52fe5ba12e@cs7g2000vbb.googlegroups.com>
In reply to#17954
On Dec 26, 11:28 am, Lie Ryan <lie.1...@gmail.com> wrote:
> On 12/26/2011 05:27 AM, Yigit Turgut wrote:
>
>
>
>
>
>
>
>
>
> > On Dec 25, 7:06 pm, Rick Johnson<rantingrickjohn...@gmail.com>  wrote:
> >> On Dec 25, 9:33 am, Yigit Turgut<y.tur...@gmail.com>  wrote:
> >>> Hi all,
>
> >>> I have a text file as following;
>
> >>> 0.200047        0.000000
> >>> 0.200053        0.160000
> >>> 0.200059        0.000000
> >>> 0.200065        0.080000
> >>> 0.200072        0.000000
> >>> 0.200078        0.160000
>
> >>> And I am trying to plot it with ;
>
> >>> filenames = sys.argv[1:]
> >>> if len(filenames) == 0:
> >>>      filenames = [sys.stdin]
> >>> for filename in filenames:
> >>>      t,y1 = numpy.genfromtxt(filename, unpack=True)
> >>>      pyplot.plot(t,y1)
> >>>      pyplot.show()
>
> >>> But graph seems weird, not as it supposed to be. Any ideas ?
>
> >> Interesting. Of course "weird" leaves a LOT to be desired. On a scale
> >> of 1-10, how "weird" is the result?
>
> > I apply a 1Khz test signal just to see if things run smoothly, but I
> > see spikes at lower and higher ends (logic 0,1) where I should see a
> > clean rectangle pwm signal. By the look of it I say weirdness is
> > around 3/10.
>
> What are you expecting? Your data produces something that looks like the
> plot on the right of this screenshot
> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with
> that; if you are expecting a square-wave-like plot (like on the left),
> then you should use a square-wave-like data, pyplot wouldn't magically
> transform a spiked-plot to squared-plot.
>
> Here's what I use to convert the data on right plot to data on left
> plot, I don't know much about numpy so it might be possible to do it
> more efficiently or numpy might even have something like it already.
>
> from itertools import izip_longest
> def to_square(t, y1):
>      sq_data = [[], []]
>      for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
>          sq_data[0].append(x)
>          sq_data[1].append(y)
>          sq_data[0].append(xn)
>          sq_data[1].append(y)
>      return numpy.array(sq_data, dtype=float)

Thanks for the tip. I know that I feed a square wave signal and record
this data. Thus I believe the situation can be related to sampling
frequency.
Couldn't get your code working, maybe because I import the data from
file.

[toc] | [prev] | [next] | [standalone]


#17970

FromLie Ryan <lie.1296@gmail.com>
Date2011-12-27 05:58 +1100
Message-ID<mailman.4104.1324925950.27778.python-list@python.org>
In reply to#17963
On 12/27/2011 04:08 AM, Yigit Turgut wrote:
> On Dec 26, 11:28 am, Lie Ryan<lie.1...@gmail.com>  wrote:
>> On 12/26/2011 05:27 AM, Yigit Turgut wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> On Dec 25, 7:06 pm, Rick Johnson<rantingrickjohn...@gmail.com>    wrote:
>>>> On Dec 25, 9:33 am, Yigit Turgut<y.tur...@gmail.com>    wrote:
>>>>> Hi all,
>>
>>>>> I have a text file as following;
>>
>>>>> 0.200047        0.000000
>>>>> 0.200053        0.160000
>>>>> 0.200059        0.000000
>>>>> 0.200065        0.080000
>>>>> 0.200072        0.000000
>>>>> 0.200078        0.160000
>>
>>>>> And I am trying to plot it with ;
>>
>>>>> filenames = sys.argv[1:]
>>>>> if len(filenames) == 0:
>>>>>       filenames = [sys.stdin]
>>>>> for filename in filenames:
>>>>>       t,y1 = numpy.genfromtxt(filename, unpack=True)
>>>>>       pyplot.plot(t,y1)
>>>>>       pyplot.show()
>>
>>>>> But graph seems weird, not as it supposed to be. Any ideas ?
>>
>>>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale
>>>> of 1-10, how "weird" is the result?
>>
>>> I apply a 1Khz test signal just to see if things run smoothly, but I
>>> see spikes at lower and higher ends (logic 0,1) where I should see a
>>> clean rectangle pwm signal. By the look of it I say weirdness is
>>> around 3/10.
>>
>> What are you expecting? Your data produces something that looks like the
>> plot on the right of this screenshot
>> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with
>> that; if you are expecting a square-wave-like plot (like on the left),
>> then you should use a square-wave-like data, pyplot wouldn't magically
>> transform a spiked-plot to squared-plot.
>>
>> Here's what I use to convert the data on right plot to data on left
>> plot, I don't know much about numpy so it might be possible to do it
>> more efficiently or numpy might even have something like it already.
>>
>> from itertools import izip_longest
>> def to_square(t, y1):
>>       sq_data = [[], []]
>>       for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
>>           sq_data[0].append(x)
>>           sq_data[1].append(y)
>>           sq_data[0].append(xn)
>>           sq_data[1].append(y)
>>       return numpy.array(sq_data, dtype=float)
>
> Thanks for the tip. I know that I feed a square wave signal and record
> this data. Thus I believe the situation can be related to sampling
> frequency.

It is due to sampling frequency, but also because you cannot sample a 
square wave perfectly because square wave has infinite steepness at the 
transitions. Although if you know the exact timing of the transitions, 
it may be possible to reconstruct the transitions perfectly.

> Couldn't get your code working, maybe because I import the data from
> file.

not your fault, I made a mistake when copy-pasteing the code, here's the 
fixed code:

from itertools import izip_longest
def to_square(data):
      sq_data = [[], []]
      for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
          sq_data[0].append(x)
          sq_data[1].append(y)
          sq_data[0].append(xn)
          sq_data[1].append(y)
      return numpy.array(sq_data, dtype=float)


use it like this:

t,y1 = to_square(numpy.genfromtxt(filename, unpack=True))
pyplot.plot(t,y1)
pyplot.show()

[toc] | [prev] | [next] | [standalone]


#17971

FromYigit Turgut <y.turgut@gmail.com>
Date2011-12-26 11:14 -0800
Message-ID<96ac870e-dffe-4556-aa3a-fab84333ceda@o9g2000vbc.googlegroups.com>
In reply to#17970
On Dec 26, 8:58 pm, Lie Ryan <lie.1...@gmail.com> wrote:
> On 12/27/2011 04:08 AM, Yigit Turgut wrote:
>
>
>
>
>
>
>
>
>
> > On Dec 26, 11:28 am, Lie Ryan<lie.1...@gmail.com>  wrote:
> >> On 12/26/2011 05:27 AM, Yigit Turgut wrote:
>
> >>> On Dec 25, 7:06 pm, Rick Johnson<rantingrickjohn...@gmail.com>    wrote:
> >>>> On Dec 25, 9:33 am, Yigit Turgut<y.tur...@gmail.com>    wrote:
> >>>>> Hi all,
>
> >>>>> I have a text file as following;
>
> >>>>> 0.200047        0.000000
> >>>>> 0.200053        0.160000
> >>>>> 0.200059        0.000000
> >>>>> 0.200065        0.080000
> >>>>> 0.200072        0.000000
> >>>>> 0.200078        0.160000
>
> >>>>> And I am trying to plot it with ;
>
> >>>>> filenames = sys.argv[1:]
> >>>>> if len(filenames) == 0:
> >>>>>       filenames = [sys.stdin]
> >>>>> for filename in filenames:
> >>>>>       t,y1 = numpy.genfromtxt(filename, unpack=True)
> >>>>>       pyplot.plot(t,y1)
> >>>>>       pyplot.show()
>
> >>>>> But graph seems weird, not as it supposed to be. Any ideas ?
>
> >>>> Interesting. Of course "weird" leaves a LOT to be desired. On a scale
> >>>> of 1-10, how "weird" is the result?
>
> >>> I apply a 1Khz test signal just to see if things run smoothly, but I
> >>> see spikes at lower and higher ends (logic 0,1) where I should see a
> >>> clean rectangle pwm signal. By the look of it I say weirdness is
> >>> around 3/10.
>
> >> What are you expecting? Your data produces something that looks like the
> >> plot on the right of this screenshot
> >> (http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with
> >> that; if you are expecting a square-wave-like plot (like on the left),
> >> then you should use a square-wave-like data, pyplot wouldn't magically
> >> transform a spiked-plot to squared-plot.
>
> >> Here's what I use to convert the data on right plot to data on left
> >> plot, I don't know much about numpy so it might be possible to do it
> >> more efficiently or numpy might even have something like it already.
>
> >> from itertools import izip_longest
> >> def to_square(t, y1):
> >>       sq_data = [[], []]
> >>       for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
> >>           sq_data[0].append(x)
> >>           sq_data[1].append(y)
> >>           sq_data[0].append(xn)
> >>           sq_data[1].append(y)
> >>       return numpy.array(sq_data, dtype=float)
>
> > Thanks for the tip. I know that I feed a square wave signal and record
> > this data. Thus I believe the situation can be related to sampling
> > frequency.
>
> It is due to sampling frequency, but also because you cannot sample a
> square wave perfectly because square wave has infinite steepness at the
> transitions. Although if you know the exact timing of the transitions,
> it may be possible to reconstruct the transitions perfectly.
>
> > Couldn't get your code working, maybe because I import the data from
> > file.
>
> not your fault, I made a mistake when copy-pasteing the code, here's the
> fixed code:
>
> from itertools import izip_longest
> def to_square(data):
>       sq_data = [[], []]
>       for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
>           sq_data[0].append(x)
>           sq_data[1].append(y)
>           sq_data[0].append(xn)
>           sq_data[1].append(y)
>       return numpy.array(sq_data, dtype=float)
>
> use it like this:
>
> t,y1 = to_square(numpy.genfromtxt(filename, unpack=True))
> pyplot.plot(t,y1)
> pyplot.show()

Significant improvement on the plot, pretty interesting. It runs ok
but I need to know how?! (:

[toc] | [prev] | [next] | [standalone]


#18020

FromLie Ryan <lie.1296@gmail.com>
Date2011-12-27 21:54 +1100
Message-ID<mailman.4131.1324983314.27778.python-list@python.org>
In reply to#17971
On 12/27/2011 06:14 AM, Yigit Turgut wrote:
> On Dec 26, 8:58 pm, Lie Ryan<lie.1...@gmail.com>  wrote:
>> On 12/27/2011 04:08 AM, Yigit Turgut wrote:
>>
>> not your fault, I made a mistake when copy-pasteing the code, here's the
>> fixed code:
>>
>> from itertools import izip_longest
>> def to_square(data):
>>        sq_data = [[], []]
>>        for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
>>            sq_data[0].append(x)
>>            sq_data[1].append(y)
>>            sq_data[0].append(xn)
>>            sq_data[1].append(y)
>>        return numpy.array(sq_data, dtype=float)
>>
>> use it like this:
>>
>> t,y1 = to_square(numpy.genfromtxt(filename, unpack=True))
>> pyplot.plot(t,y1)
>> pyplot.show()
>
> Significant improvement on the plot, pretty interesting. It runs ok
> but I need to know how?! (:

it's pretty simple, actually; just observe the numbers before and after 
it's fixed by the function and it should be fairly obvious.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web