MatPlotLib Python-颜色传感器中用于更改线点颜色的RGB值
所以我正在使用TCS3200颜色传感器和Arduino Mega 2560来生成特定的RGB值。然后,通过串行电缆,我将数据发送到VIDLE for Python,拆分3个数据点,并将它们存储在一个数组中(每50个数据点(每个RGB)更新MatPlotLib绘图) 最初我在三条独立的线上绘制R、G、B值……现在我在绘制一条不同的线,基于(255255255)坐标系(y极限为255*sqrt(3)) 我想做的是:如果我的RGB值是(220、60、140),我希望能够基于这些值更改数据点的颜色 图形点为sqrt(pow(220,2.0)+pow(60,2.0)+pow(140,2.0)),但颜色需要反映RGB值 我该怎么做 以下是我当前的绘图设置:MatPlotLib Python-颜色传感器中用于更改线点颜色的RGB值,python,matplotlib,linestyle,drawnow,Python,Matplotlib,Linestyle,Drawnow,所以我正在使用TCS3200颜色传感器和Arduino Mega 2560来生成特定的RGB值。然后,通过串行电缆,我将数据发送到VIDLE for Python,拆分3个数据点,并将它们存储在一个数组中(每50个数据点(每个RGB)更新MatPlotLib绘图) 最初我在三条独立的线上绘制R、G、B值……现在我在绘制一条不同的线,基于(255255255)坐标系(y极限为255*sqrt(3)) 我想做的是:如果我的RGB值是(220、60、140),我希望能够基于这些值更改数据点的颜色 图形
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()
将numpy导入为np
将matplotlib.pyplot作为plt导入
#模型系列
类Serial():
n=0
定义初始(自我,**kwargs):
self.maxN=kwargs.get(“maxN”,1000)
self.cols=np.arange(0240,1)
def inWaiting(自我):
self.n+=1
返回(self.n 50):
距离.pop(0)
颜色。流行音乐(0)
plt.draw()
请参阅这篇关于创建可复制的示例的文章:只要这个示例依赖于这个神秘的
drawnow
包,就没有人能够帮助您。(同样,依赖串行端口获取数据也是如此。用CSV文件的StringIO
实例模拟它)drawnow
是我见过的最无用的软件包。它由plt.clf()
,后跟函数调用和plt.draw()
组成。但我同意在问这样的问题时应该提供a。我可以生成上面的图表,它看起来非常好。但是,假设y轴为255*sqrt(3)(这是RGB(255255255)立方体的最大距离/对角线长度),我是否能够在每次绘制一个红点时根据RGB值使这些红点变为不同的颜色?是否希望在绘制一个新点后,所有点都改变颜色?不太可能。假设绘制的第一个点是基于RGB(120,60100)…它的y轴值是~167,然后下一个点是(120,80110)…y轴~181…..点创建的线是正斜率,但点本身是由其RGB值表示的颜色。如果这是愚蠢的,那么我可以考虑在图的右上方放置一个正方形大小的圆,作为彩色显示。因此,每个测量实际上会产生一个点,而不仅仅是其中的一些。点的颜色取决于测量本身?没有愚蠢的事情;我们只需要精确地说明要求。看起来可能很愚蠢,在某种意义上,想象一个难以阅读和观察的图形,其主焦线可以是任何可以想象的颜色……看起来可能会很奇怪。但现在这正是我所追求的。从技术上讲,我有一个按钮,可以在捕获数据时触发…但我把它拿出来只是为了确保事先绘图工作正常。所以是的,现在它每300毫秒绘制一个点。点的颜色取决于测量,是的。