Matplotlib connect scatterplot points with line - python

I read the question Matplotlib connect scatterplot points with line - Python but it doesn't work for me...
I tried to use plt.scatter(x, y) and after that plt.show(), but all that I see is a set of separated dots from each other.
I think, that problem consists in that I use a loop for generating each point on the plot and after that loop, I use plt.show().
All my code looks like this:
x = 0
y = 0
def eye_gaze1(a,b):
global x,y
image_plot1 = plt.imshow(image1)
for i in range(len(a)):
if stimulus_name[x+y+1] == '5_01.jpg' and a[x+y+1] != '-':
plt.scatter([a[x+y+1]], [b[x+y+1]]) #here I putting new point on the image_plot1 and this process repeats (something like 1000 times) before in my massive of data cycle will find '-' symbol
x += 1
else:
x += 1
break
plt.show() #final image
y += x
x = 0
j = 0
while j < 15: #just repeat this function for another cells in data-massive
eye_gaze1(col_values_X_right,col_values_Y_right)
j += 1
So, question is, how can I connect points?
If I try to use the comment of JohanC, I see this error: TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
x = 0
y = 0
def eye_gaze1(a,b):
global x,y
image_plot1 = plt.imshow(image1)
for i in range(len(a)):
if stimulus_name[x+y+1] == '5_01.jpg' and a[x+y+1] != '-':
X_coordinates = list().append(a[x+y+1])
Y_coordinates = list().append(b[x+y+1])
x += 1
else:
x += 1
break
plt.scatter(X_coordinates, Y_coordinates, '-o')
plt.show()
y += x
x = 0
j = 0
while j < 15:
eye_gaze1(col_values_X_right,col_values_Y_right)
print(x)
print(y)
j += 1

Related

Python: Comparing values in two lists

I've really tried looking all over for solutions to my problem but haven't been successful in finding anything. If someone else has already asked this question, I apologize. Onto the problem.
I have two lists in with values to be compared to each other. I have tried the following option.
list1 = [1,3,5,7,9]
list2 = [200,2]
x = 0
n = 0
y = 0
while x <= 9:
if list1[y] >= list2[n]:
print('TRUE')
x = x + 1
y = y + 1
if y > 4:
y = 0
n = n + 1
else:
print('FALSE')
x = x + 1
y = y + 1
if y > 4:
y = 0
n = n + 1
The only problem is, instead of the variables in place, I need to iterate through a list of values.
So instead, I would like the code to look something like this:
x = 0
n = [0,1]
y = [0,3]
z = len(n) + len(y) - 1
while x <= z:
if list1[y] >= list2[n]:
print('TRUE')
x = x + 1
else:
print('FALSE')
x = x + 1
Where n and y are index values of the numbers that I want to compare.
This does not work for me and I'm really not sure how else to do this.
Edit: I didn't think I had to explain everything by text since I included two sets of code. The first set of code works and it shows exactly what I am trying to do. The second is what I want it to do.
Broken down further I want to know if list1[0]>=list2[0], followed by list1[3]>=list2[0], followed by list1[0]>=list2[1], followed by list1[3]>=list2[1]. The outputs that I expect are in the code I provided.
Apologies if I wasn't clear before. I need to call specific index positions that I will have in a separate list. This is the problem that I tried to outline in the second code.
I think now get what you are trying to do.
First, there are two lists of "raw" data:
list1 = [1,3,5,7,9]
list2 = [200,2]
Then, there are two sets of "indices of interest":
y = [0, 3] # indices of interest for list1
n = [0, 1] # indices of interest for list2
I think the following can achieve what you want:
product = [(index1, index2) for index2 in n for index1 in y] #cartesian product
for index1, index2 in product:
if list1[index1] >= list2[index2]:
print("True")
else:
print("False")
Or if the cartesian product is not wanted, simply do it within nested loops:
for index2 in n:
for index1 in y:
if list1[index1] >= list2[index2]:
print("True")
else:
print("False")

Giving positional arguments for Co-ordinates and Clustering them together

so I have a data file and I'm wanting to find two things:
Whether the coordinates I give are inside or outside an area and returning if its true or not
Put each coordinates of "1" together in each line in its own list. This should return it in a dictionary.
The file has the following:
1 1 0 1 0 1
0 0 1 1 1 0
1 1 1 1 1 0
1 0 0 0 0 1
0 0 1 1 0 1
1 0 0 0 0 1
I've put the above into a list and each with the code:
lines = []
with open('area.dat', 'r') as a:
for line in a:
line = line.strip()
if not line:
continue
lines.append(list(map(int, line.split())))
data.extend(map(int, line.split()))
print(lines)
My attempts at code to get the coordinates and whether it's outside or inside the area (for both x and y)
area is the list of lists
x = int(input('enter x: '))
y = int(input('enter y: '))
def cords(x, y, area):
if x > 6 or y > 6:
print('Outside Area')
return(False)
else:
print('Inside Area')
return(True)
I want to get a coordinate x and y within the list "area" and return whether it is in or out this.
So for example if I put in cords(0,4,area) it will return "True" and if I put cords(3,7,area) it will return "False".
After this I then want to put them together in groups of 1's by each line.
so for example line 1 and 2 would give:
{1: [(0,4), (0,5)], 2: [(1,0), (1,1)]}
All help is appreciated thanks.
For the first part you have two options:
def cords(x, y):
return x >= 0 and x < 6 and y >= 0 and y < 6
This first option is static for an area size of 6x6, note that array indexing starts at 0 so 6 would already be out of bounds.
def cords(x, y, area):
return x >= 0 and x < len(area) and y >= 0 and y < len(area[0])
This second option dynamically checks whether the coordinates are in bounds of the given nested list. You might have to adjust it based on whether x and y relate to rows and columns or vice versa.
Now for your second part, you're creating a dictionary with redundant information, since the index (1 and 2 in your example) directly relates to the first axis (0 and 1 in your example), you might want to reconsider what you actually want to achieve here.
d = {}
for i,row in enumerate(lines):
n = []
for j,v in enumerate(row):
if v == 1:
n.append([i,j])
d[i+1] = n

Python single line while loop

In Python, I am trying to make a variable increment in value while it is less than another number. I know that it is possible to do a for loop in the form (print(x) for x in range(1, 5)). My question is, is there a similar way to do a while loop in this form, such as x += 1 while x < y?
x = 0
y = 10
while x < y:
x +=1
>>> x
10
Well you can do it in a single line because Python allows that:
x = 0
while x < y: x +=1
It is not as readable, and it doesn't conform to PEP 8, but it is doable.
You can separate each statement with a semi-colon like so...
x = 0; y = 5
while(x < y): print(x); x=x+1
You could do something like this
n = 0
while n < 1000: rn += n if not (n % 3 and n % 5) else 0
What you are seeing is a conditional expression but it comes at the price of some reduced readability

Map for text based game

this is supposed to make a map for a text based game which works great until I add more than one point to the map when I add a second point to it, it makes the length of the map 2x bigger than needed
this is example with 2 points
mapS = [5,5] ##Size of the map
line = ""
objects = dict()
numx = 0
numy = 0
def mapSize(x,y):
mapS = [x,y]
def addObject(name, x, y, symbol):
globals()[name] = [x,y, symbol]
objects[name] = [x,y, symbol]
addObject("House", 2,3, "H") ##############FIRST POINT
addObject("Cabin", 3,4, "C") ##############SECOND POINT
for y in range(mapS[1]):
numy += 1
for x in range(mapS[0]):
numx += 1
for place in objects:
if objects[place][0] == numx:
if objects[place][1] == numy:
line += objects[place][2]
else:
line += "*"
else:
line += "*"
print(line)
line =""
numx = 0
numy = 0
The actual problem is that, for each y and each x, you're printing one character for each object:
for y in range(mapS[1]):
numy += 1
for x in range(mapS[0]):
numx += 1
for place in objects:
if objects[place][0] == numx:
if objects[place][0] == numy:
line += objects[place][2]
else:
line += "*"
else:
line += "*"
So, if you have 2 objects, you'll print 2 characters at each x, y point; if you have 6 objects, you'll print 6 characters at each x, y point.
What you want to do is only print 1 character at each x, y point. For example:
for y in range(mapS[1]):
numy += 1
for x in range(mapS[0]):
numx += 1
for place in objects:
if objects[place][0] == numx and objects[place][0] == numy:
line += objects[place][2]
break
else:
line += "*"
This goes through the objects until it finds the first one that matches that position, and adds only that one object's character, and stops searching with break. Then, only if it gets to the end of the loop without breaking (that's what an else clause on a for statement means), it adds a * instead.
You have a number of other problems here:
mapSize doesn't actually change mapS; you need a global mapS statement.
You're comparing both numx and numy to objects[place][0]. You almost certainly wanted to compare numy to objects[place][1]. One nice way to do this all at once is: if objects[place][:2] == [numx, numy]:.
Your numx and numy are 1-based. Did you want that? If so, it would make your intention more explicit—and a whole lot simpler—to just use numx == x+1 instead of keeping track of it separately from x but in such a way that it always ends up the same as x+1. If not, just use x itself.
The globals()[name] = is a bad idea. Since you don't make any use of it, the simplest way to fix that is to just remove that line.
It's a lot simpler to create things like line at the top of the loop, instead of creating them outside the loop and then re-creating them at the bottom of the loop.
So, putting it all together:
mapS = [5, 5]
objects = {}
def mapSize(x, y):
global mapS
mapS = [x, y]
def addObject(name, x, y, symbol):
objects[name] = [x, y, symbol]
addObject("House", 2, 3, "H") ##############FIRST POINT
addObject("Cabin", 3, 4, "C") ##############SECOND POINT
for y in range(mapS[1]):
line = ""
numy = y+1
for x in range(mapS[0]):
numx = x + 1
for place in objects:
if objects[place][:2] == [numx, numy]:
line += objects[place][2]
break
else:
line += "*"
print(line)

screen output of tuples

I got a question about displaying my output data. Here is my code:
coordinate = []
z=0
while z <= 10:
y = 0
while y < 10:
x = 0
while x < 10:
coordinate.append((x,y,z))
x += 1
coordinate.append((x,y,z))
y += 1
coordinate.append((x,y,z))
z += 1
for point in coordinate:
print(point)
My output data contains commas and parenthesis which I want to get rid of. I want my output to look like this:
0 0 0
1 0 0
2 0 0
etc. No comma and parenthesis, just the values.
Write the last two lines like this:
for x, y, z in coordinate:
print(x, y, z)
In addition to the answer by #flornquake, you can do something with those while
import itertools
# If you just want to print this thing, forget about building a list
# and just use the output of itertools.product
coordinate = list(itertools.product(range(0, 10), range(0, 10), range(0, 11)))
for point in coordinate:
print('{} {} {}'.format(*point))
Presuming you are using Python 3, you could do this:
for point in coordinate:
print(*point)
The "star" notation unpacks the tuple into its individual elements. The print function then displays the elements using the default separator, which is a single space character.

Categories