dynamically creating then plotting arrays

hi all,

i am reading a set of tab-separated data from a file and i want to put it into an array, and then plot some of the columns. i know the number of columns ahead of time but not the number of rows. i load the array from the file as follows, which seems to work:

data = []
for line in myfile:
field1, field2, field3 = line.strip().split(’\t’)
data.append([int(field1), int(field2), int(field3)])

i then convert it into an array as follows:

data = array(data)

i am able to reference the first column as follows:

data[:,0]

but if i try to plot the first column against the second as follows:

bar(data[:,0],data[:,1])

then i get the error:

/usr/lib64/python2.5/site-packages/matplotlib/units.pyc in get_converter(self, x)
128 converter = self.get(classx)
129
–> 130 if converter is None and iterable(x):
131 # if this is anything but an object array, we’ll assume
132 # there are no custom units

[repeated many times]

RuntimeError: maximum recursion depth exceeded
WARNING: Failure executing file: <myfile.py>

how can i fix this? i’d like an n-by-m representation of my data as an array which i can reference like a matrix in matlab. some of the columns are floats, other are ints, and others are strings, so i prefer to load the data into an array as a loop where i can cast the strings appropriately, rather than use some built in io function for reading tab-separated data.

thank you very much.

hi all,

please disregard the previous email - i had a mistake in my file that did not do the casting properly when loading the data.

i managed to plot my data, but this time i am having a problem with the ‘bar’ function.

when i plot using:

x = data[:, 0]
y = data[:, 1]
bar(x,y)

i get the attached figure. the bar graphs are way too thin and don’t look like bar graphs at all. i see in the gallery many examples of bars with greater width, e.g. http://matplotlib.sourceforge.net/examples/api/histogram_demo.html

but all of these seem to be made using the ‘hist’ function. i just want the bar width to be greater. my setting of the width= does not make a difference, it treats:

bar(x,y,width=1.5)
bar(x,y,width=10)
etc.

as the same, yielding this line plot. if i remove some data points (and plot x and y’s that are only, say, 3 in length) then the bars look normal.

how can i make the bar widths greater in this case?

image.png

···

On Sun, Mar 1, 2009 at 11:41 AM, per freem <perfreem@…985…> wrote:

hi all,

i am reading a set of tab-separated data from a file and i want to put it into an array, and then plot some of the columns. i know the number of columns ahead of time but not the number of rows. i load the array from the file as follows, which seems to work:

data = []
for line in myfile:
field1, field2, field3 = line.strip().split(’\t’)
data.append([int(field1), int(field2), int(field3)])

i then convert it into an array as follows:

data = array(data)

i am able to reference the first column as follows:

data[:,0]

but if i try to plot the first column against the second as follows:

bar(data[:,0],data[:,1])

then i get the error:

/usr/lib64/python2.5/site-packages/matplotlib/units.pyc in get_converter(self, x)

128             converter = self.get(classx)
129

–> 130 if converter is None and iterable(x):
131 # if this is anything but an object array, we’ll assume
132 # there are no custom units

[repeated many times]

RuntimeError: maximum recursion depth exceeded
WARNING: Failure executing file: <myfile.py>

how can i fix this? i’d like an n-by-m representation of my data as an array which i can reference like a matrix in matlab. some of the columns are floats, other are ints, and others are strings, so i prefer to load the data into an array as a loop where i can cast the strings appropriately, rather than use some built in io function for reading tab-separated data.

thank you very much.

per freem wrote:

hi all,

please disregard the previous email - i had a mistake in my file that did not do the casting properly when loading the data.

i managed to plot my data, but this time i am having a problem with the 'bar' function.

when i plot using:

x = data[:, 0]
y = data[:, 1]
bar(x,y)

i get the attached figure. the bar graphs are way too thin and don't look like bar graphs at all. i see in the gallery many examples of bars with greater width, e.g. http://matplotlib.sourceforge.net/examples/api/histogram_demo.html

but all of these seem to be made using the 'hist' function. i just want the bar width to be greater. my setting of the width= does not make a difference, it treats:

bar(x,y,width=1.5)
bar(x,y,width=10)
etc.

Width is in the same units as x, and it looks like your range of x values is 1e8, so maybe your width needs to be something like 1e7, not 10.

Eric

···

as the same, yielding this line plot. if i remove some data points (and plot x and y's that are only, say, 3 in length) then the bars look normal.

how can i make the bar widths greater in this case?

On Sun, Mar 1, 2009 at 11:41 AM, per freem <perfreem@…287… > <mailto:perfreem@…287…>> wrote:

    hi all,

    i am reading a set of tab-separated data from a file and i want to
    put it into an array, and then plot some of the columns. i know the
    number of columns ahead of time but not the number of rows. i load
    the array from the file as follows, which seems to work:

    data = []
    for line in myfile:
      field1, field2, field3 = line.strip().split('\t')
      data.append([int(field1), int(field2), int(field3)])

    i then convert it into an array as follows:

    data = array(data)

    i am able to reference the first column as follows:

    data[:,0]

    but if i try to plot the first column against the second as follows:

    bar(data[:,0],data[:,1])

    then i get the error:

    /usr/lib64/python2.5/site-packages/matplotlib/units.pyc in
    get_converter(self, x)
        128 converter = self.get(classx)
        129
    --> 130 if converter is None and iterable(x):
        131 # if this is anything but an object array, we'll
    assume
        132 # there are no custom units

    [repeated many times]

    RuntimeError: maximum recursion depth exceeded
    WARNING: Failure executing file: <myfile.py>

    how can i fix this? i'd like an n-by-m representation of my data as
    an array which i can reference like a matrix in matlab. some of the
    columns are floats, other are ints, and others are strings, so i
    prefer to load the data into an array as a loop where i can cast the
    strings appropriately, rather than use some built in io function for
    reading tab-separated data.

    thank you very much.

------------------------------------------------------------------------

------------------------------------------------------------------------

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H

------------------------------------------------------------------------

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users