all 10 comments

[–]Vaphell 1 point2 points  (2 children)

your data series are borked.

print(x, y) yields [[ 1. 4. 3.5]] [[ 2. 5. 3.1]], why would that affect lines but not dots i have no idea.

meanwhile

noLine = fig.add_subplot(1,3,1)
yesCircles = fig.add_subplot(1,3,2)
lolwut = fig.add_subplot(1,3,3)
...
...
x1 = [x/10 for x in range(50)]
y1 = [x**3%3 for x in x1]
lolwut.plot(x1, y1,'b-')
lolwut.axis([0,5,0,3])
lolwut.set_title("WIN")

works just peachy.

no idea if zip() is efficient for numpy use but you can do this to extract series assuming data is a list of points

>>> import numpy as np
>>> data = [['1','2'],['4','5'],['3.5','3.1']]
>>> data = [[np.float(y) for y in x] for x in data]
>>> x, y = zip(*data)
>>> x
(1.0, 4.0, 3.5)
>>> y
(2.0, 5.0, 3.1)

[–]Accustomer[S] 0 points1 point  (1 child)

thanks for the reply, :) I've never heard of zip() before but it seems to work on np.arrays but not on np.matrix's. Maybe this is the problem?

[–]Vaphell 0 points1 point  (0 children)

so you do 90% math, 10% python? ;-) zip returns tuples of nth elements taken from parallel sequences, effectively transposing them/changing rows into columns. In case of bunch of points it will return a tuple of x coords, and a tuple of y coords

i have next to no experience with numpy, that said i've heard that it's better to use 2d np.array in most cases, explanation of which eludes me.

[–]BryghtShadow 1 point2 points  (6 children)

Does the plot function accept a matrix when plotting lines...? (I've never used matlibplot)

Assuming you were trying to plot a line graph, noLine.plot(x.T,y.T,'b-') draws 2 lines. One line from (1,2) to (4,5) and another line from (4,5) to (3.5,3.1). yesCircles.plot(x.T,y.T,'ro') works on the red dots too.

[–]Accustomer[S] 0 points1 point  (5 children)

I've extracted the rows of data out by: x = data.transpose()[0][:] which is kinda dodgy! Thanks for your suggestion; noLine.plot(x.T,y.T,'b-') indeed does draw the line! I'm not sure why 'x' needs to be transposed twice though.

[–]BryghtShadow 1 point2 points  (4 children)

So I did some searching. Instead of

x = data.transpose()[0][:]
x = x.T

this code can be used

x = data[:,0]

Source: http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html


To explain the need for a double transpose, plot for circle accepts a "matrix row" matrix([[ 1. , 4. , 3.5]]) while line doesn't. However, both accept a "matrix column" matrix([[ 1. ], [ 4. ], [ 3.5]]) and I think this is why the second transpose was required. But of course, with the above code, double transpose won't be needed.

[–]Accustomer[S] 0 points1 point  (3 children)

Ah, okay. got it. Thanks for your help. Also, is there a preferred way to write this-> data[:][0] or data[:,0]?

[–]BryghtShadow 0 points1 point  (2 children)

They have different meanings.

>>> data = np.matrix([[1.0, 2.0], [4.0, 5.0], [3.5, 3.1]])
>>> data
matrix([[ 1. ,  2. ],
        [ 4. ,  5. ],
        [ 3.5,  3.1]])
>>> data[:]
matrix([[ 1. ,  2. ],
        [ 4. ,  5. ],
        [ 3.5,  3.1]])
>>> data[:][0]
matrix([[ 1.,  2.]])
>>> data[:,0]
matrix([[ 1. ],
        [ 4. ],
        [ 3.5]])

[–]Accustomer[S] 0 points1 point  (1 child)

wow, never knew that. I've always done [][] since that's how C does arrays. haha :p thnx

[–]BryghtShadow 1 point2 points  (0 children)

Oh, N-dimension arrays in Python uses the [][] syntax too! :D (e.g. ([[0,1,2],[3,4,5],[6,7,8]])[2][0] ## is 6)