MatPlotLib Python - 来自颜色传感器的 RGB 值用于改变线点的颜色
MatPlotLib Python - RGB values from color sensor used to change color of line point
所以我正在使用 TCS3200 颜色传感器和 Arduino Mega 2560 来生成特定的 RGB 值。然后,通过串行电缆,我将 Python 的数据发送到 VIDLE,拆分 3 个数据点,并将它们存储在一个数组中(每 50 个数据点(每个 RGB)更新 MatPlotLib 图。)
最初我在三个单独的线上绘制 R、G、B 值...现在我绘制了一条不同的线,基于 (255,255,255) 坐标系(y 限制为 255*sqrt(3 )).
我想做的是:如果我的 RGB 值为 (220, 60, 140),我希望能够根据这些值更改数据点的颜色。
图形点为 sqrt(pow(220,2.0)+pow(60,2.0)+pow(140,2.0)),但颜色需要反映 RGB 值。
我该怎么做?
这是我当前的情节设置:
import serial
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
distance = []
s = serial.Serial(port='/dev/cu.usbmodem1421', baudrate=115200)
plt.ion()
cnt = 0
limit = 255*sqrt(3);
r = 0
g = 0
b = 0
def makeFig():
plt.ylim(0,limit)
plt.title('My Live Streaming Sensor Data')
plt.grid(True)
plt.ylabel('RGB Values')
plt.xlabel('Time')
# somewhere in the line below I think the RGB dynamics should be reflected
plt.plot(distance, '-', label='Distance')
plt.ticklabel_format(useOffset=True)
plt.legend(loc='upper left')
while True:
while (s.inWaiting()):
myDataString = s.readline()
try:
dataArray = myDataString.split(',')
print (dataArray)
r = float(dataArray[0])
g = float(dataArray[1])
b = float(dataArray[2])
d = float(dataArray[3].strip('\r\n')
distance.append(d)
# before this 'drawnow' gets called, should the RGB values be incorporated into the plot?
drawnow(makeFig)
plt.pause(0.000001)
cnt = cnt + 1
if (cnt > 50):
distance.pop(0)
except ValueError:
print (myDataString)
这是一种在 RGB 立方体中与原点距离相对应的位置绘制一些点的方法。它们的颜色将设置为 rgb 值元组。
import numpy as np
import matplotlib.pyplot as plt
# Mockup Serial
class Serial():
n = 0
def __init__(self, **kwargs):
self.maxN = kwargs.get("maxN", 1000)
self.cols = np.arange(0,240,1)
def inWaiting(self):
self.n+=1
return (self.n<self.maxN)
def readline(self):
a = np.random.choice(self.cols,size=3)
a = list(map(str, a))
b = str(np.random.randint(0,10))
return ",".join(a)+","+b+'\r\n'
distance = []
colors = []
s = Serial(port='/dev/cu.usbmodem1421', baudrate=115200)
plt.ion()
cnt = 0
limit = 255.*np.sqrt(3)
r = 0
g = 0
b = 0
plt.ylim(0,limit)
plt.title('My Live Streaming Sensor Data')
plt.grid(True)
plt.ylabel('RGB Values')
plt.xlabel('Time')
line, = plt.plot([],[], '-', color="gray",label='Distance')
scatter = plt.scatter([],[], s=40, marker='o', label='Hit', zorder=3)
plt.ticklabel_format(useOffset=True)
plt.legend(loc='upper left')
while (s.inWaiting()):
myDataString = s.readline()
dataArray = myDataString.split(',')
r = int(dataArray[0])
g = int(dataArray[1])
b = int(dataArray[2])
d = int(dataArray[3].strip('\r\n'))
distance.append(np.sqrt(r**2+b**2+g**2))
color = (r/255.,g/255.,b/255.)
colors.append(color)
x = range(len(distance))
line.set_data(x, distance)
scatter.set_offsets(np.c_[x,distance])
scatter.set_color(colors)
plt.xlim(min(x), max(x))
plt.pause(0.01)
cnt = cnt + 1
if (cnt > 50):
distance.pop(0)
colors.pop(0)
plt.draw()
所以我正在使用 TCS3200 颜色传感器和 Arduino Mega 2560 来生成特定的 RGB 值。然后,通过串行电缆,我将 Python 的数据发送到 VIDLE,拆分 3 个数据点,并将它们存储在一个数组中(每 50 个数据点(每个 RGB)更新 MatPlotLib 图。)
最初我在三个单独的线上绘制 R、G、B 值...现在我绘制了一条不同的线,基于 (255,255,255) 坐标系(y 限制为 255*sqrt(3 )).
我想做的是:如果我的 RGB 值为 (220, 60, 140),我希望能够根据这些值更改数据点的颜色。
图形点为 sqrt(pow(220,2.0)+pow(60,2.0)+pow(140,2.0)),但颜色需要反映 RGB 值。
我该怎么做?
这是我当前的情节设置:
import serial
import numpy as np
import matplotlib.pyplot as plt
from drawnow import *
distance = []
s = serial.Serial(port='/dev/cu.usbmodem1421', baudrate=115200)
plt.ion()
cnt = 0
limit = 255*sqrt(3);
r = 0
g = 0
b = 0
def makeFig():
plt.ylim(0,limit)
plt.title('My Live Streaming Sensor Data')
plt.grid(True)
plt.ylabel('RGB Values')
plt.xlabel('Time')
# somewhere in the line below I think the RGB dynamics should be reflected
plt.plot(distance, '-', label='Distance')
plt.ticklabel_format(useOffset=True)
plt.legend(loc='upper left')
while True:
while (s.inWaiting()):
myDataString = s.readline()
try:
dataArray = myDataString.split(',')
print (dataArray)
r = float(dataArray[0])
g = float(dataArray[1])
b = float(dataArray[2])
d = float(dataArray[3].strip('\r\n')
distance.append(d)
# before this 'drawnow' gets called, should the RGB values be incorporated into the plot?
drawnow(makeFig)
plt.pause(0.000001)
cnt = cnt + 1
if (cnt > 50):
distance.pop(0)
except ValueError:
print (myDataString)
这是一种在 RGB 立方体中与原点距离相对应的位置绘制一些点的方法。它们的颜色将设置为 rgb 值元组。
import numpy as np
import matplotlib.pyplot as plt
# Mockup Serial
class Serial():
n = 0
def __init__(self, **kwargs):
self.maxN = kwargs.get("maxN", 1000)
self.cols = np.arange(0,240,1)
def inWaiting(self):
self.n+=1
return (self.n<self.maxN)
def readline(self):
a = np.random.choice(self.cols,size=3)
a = list(map(str, a))
b = str(np.random.randint(0,10))
return ",".join(a)+","+b+'\r\n'
distance = []
colors = []
s = Serial(port='/dev/cu.usbmodem1421', baudrate=115200)
plt.ion()
cnt = 0
limit = 255.*np.sqrt(3)
r = 0
g = 0
b = 0
plt.ylim(0,limit)
plt.title('My Live Streaming Sensor Data')
plt.grid(True)
plt.ylabel('RGB Values')
plt.xlabel('Time')
line, = plt.plot([],[], '-', color="gray",label='Distance')
scatter = plt.scatter([],[], s=40, marker='o', label='Hit', zorder=3)
plt.ticklabel_format(useOffset=True)
plt.legend(loc='upper left')
while (s.inWaiting()):
myDataString = s.readline()
dataArray = myDataString.split(',')
r = int(dataArray[0])
g = int(dataArray[1])
b = int(dataArray[2])
d = int(dataArray[3].strip('\r\n'))
distance.append(np.sqrt(r**2+b**2+g**2))
color = (r/255.,g/255.,b/255.)
colors.append(color)
x = range(len(distance))
line.set_data(x, distance)
scatter.set_offsets(np.c_[x,distance])
scatter.set_color(colors)
plt.xlim(min(x), max(x))
plt.pause(0.01)
cnt = cnt + 1
if (cnt > 50):
distance.pop(0)
colors.pop(0)
plt.draw()