Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NumPy Matrix help
#1
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()
Reply
#2
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))
        
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Numpy error while filling up matrix with Characters august 4 1,799 Apr-13-2022, 10:28 PM
Last Post: august
  Create a 2-channel numpy file from two cvs file containing a 9x9 matrix silvialecc 1 1,616 Oct-26-2021, 07:59 AM
Last Post: Gribouillis
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,303 May-03-2021, 06:30 AM
Last Post: Gribouillis
  Matrix Operations Without Numpy or Incorporating Python into Webpage ebryski 1 2,847 Nov-26-2020, 12:50 PM
Last Post: jefsummers
  Transposing a Matrix WITHOUT numpy TreasureDragon 5 11,106 Mar-02-2019, 10:48 PM
Last Post: ichabod801
  matrix from matrix python numpy array shei7141 1 3,642 Jan-16-2017, 06:10 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020