Python Forum

Full Version: NumPy Matrix help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, im creating a project that is a sensor mat that shows where a user is stepping. I have successfully created the python code for the mat using matplotlib, and now im trying to create a step counter for this program. i plan on unsiong numPy to look at certain region of the matrix and have conditions for it. For example
if arr[0:7,0:7] > 100:   tsteps = +1
im just having trouble with this. i can give more details if you need it. Any help is appreciated

This is the code i have at the moment, it doesn't compile with the new stuff I added.

from __future__ import print_function
import serial, os
import matplotlib.pyplot as plt
import matplotlib.animation as animation

import numpy as np

ser = serial.Serial('COM4', 500000)
w, h =  138 , 48;
matrix = np.array([[0 for x in range(w)] for y in range(h)])

tsteps = 0
wrongSteps = 0

def generate_data():
	while not ord(ser.read()) == 0:
		pass
	for y in range(h):
		for x in range(w):
			readByte = ser.read()
			if  ord(readByte)==0:
				break
			else:
				matrix[y][x]=ord(readByte)
	print('\n'.join([''.join(['{:4}'.format(item) for item in row]) 
      for row in matrix]))
	return matrix
	   
def update(data):
    mat.set_data(data)
    return mat
	
def data_gen():
    while True:
        yield generate_data()

def steps():
    for arr in data_gen():
       if arr[0:7,0:7] > 100:
       tsteps = +1
    

    
plt.plot(tsteps)
    
ani = animation.FuncAnimation(fig, steps, interval = 1000)




fig, ax = plt.subplots()
mat = ax.matshow(generate_data(),  vmin=0, vmax=120)
ax.autoscale(False)
plt.colorbar(mat)
ani = animation.FuncAnimation(fig, update, data_gen)

def rect(pos):
    r = plt.Rectangle(pos, 8,8, facecolor="none", edgecolor="k", linewidth=2)
    plt.gca().add_patch(r)

for i in range(0, w, 8):
    for j in range(0, h, 8):
        rect((i - 0.5, j - 0.5))
        





matrix[0:7,0:7]
        
matrix[8:15,:0:7]


matrix[16:23,0:7]

matrix[24:31,0:7]


        
plt.show()
I got it to compile but I don't seem to get anything on my graph.

Also I'm confused on what I should call for the array I'm thing it should be data_gen but still confused on that.

from __future__ import print_function
import serial, os
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
import numpy as np

ser = serial.Serial('COM4', 500000)
w, h =  138 , 48;
matrix = np.array([[0 for x in range(w)] for y in range(h)])

tsteps = 1
wrongSteps = 0

def generate_data():
	while not ord(ser.read()) == 0:
		pass
	for y in range(h):
		for x in range(w):
			readByte = ser.read()
			if  ord(readByte)==0:
				break
			else:
				matrix[y][x]=ord(readByte)
	print('\n'.join([''.join(['{:4}'.format(item) for item in row]) 
      for row in matrix]))
	return matrix
	   
def update(data):
    mat.set_data(data)
    return mat
	
def data_gen():
    while True:
        yield generate_data()






def steps():
    for arr in generate_data():
       if (arr[:7,:7] > 0).any():
        tsteps = +1
    

    
fig1, ax = plt.subplots()
    
ani = animation.FuncAnimation(fig1, steps, interval = 1000)

fig, ax = plt.subplots()
mat = ax.matshow(generate_data(),  vmin=0, vmax=120)
ax.autoscale(False)
plt.colorbar(mat)
ani = animation.FuncAnimation(fig, update, data_gen)

def rect(pos):
    r = plt.Rectangle(pos, 8,8, facecolor="none", edgecolor="k", linewidth=2)
    plt.gca().add_patch(r)

for i in range(0, w, 8):
    for j in range(0, h, 8):
        rect((i - 0.5, j - 0.5))