我正在研究在MySQLdb和Matplotlib中使用Python。我希望在基于ginput图的matplotlib散点图中使用查询的值。我有以下工作:import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from pylab import *
import random
import MySQLdb as mdb
import sys
from collections import defaultdict
##### Start the query ####
db = mdb.connect('localhost', 'root', 'password', 'xbee')
start = raw_input("Enter Start Date: ")
part_1 = "SELECT XBEE_ADDRESS_AL, XBEE_TEMPERATURE FROM xbeereadings WHERE Date='"
part_2 = start
part_3 = "'"
query_1 = part_1 + part_2 + part_3
cur = db.cursor()
cur.execute(query_1)
s = cur.fetchall()
print s
d = defaultdict(list)
for k, v in s:
d[k].append(v)
i = 0
temp = [item[i] for item in d.values()]
figure(figsize=(15, 8))
img = mpimg.imread('floor.png')
imgplot = plt.imshow(img, cmap=cm.hot)
print "Left click to plot the sensors point on the image - Middle Click to remove the last point - Right click to End plotting"
# pts would be used with ginput to collect the place the sensor would be located. It returns the example array below
pts = ginput(n=0, timeout=0, mouse_add=1, mouse_pop=2, mouse_stop=3)
x = map(lambda x: x[0],pts) # Extract the values from pts
y = map(lambda x: x[1],pts)
t = temp
result = zip(x,y,t)
img = mpimg.imread('floor.png')
imgplot = plt.imshow(img, cmap=cm.hot, vmin=-20, vmax=40)
scatter(x, y, marker='h', c=t, s=150, vmin=-20, vmax=40) #add colour c=?
print t
# Add cmap
colorbar()
show()
编辑:问题的前一部分(如何使用查询值作为cmap值)如新代码中所示。我测量了温度(除以100得到一个有效的数字),然后把它放在绘图中。在
我现在想要一些帮助/代码/起点的问题是:
1-如何将输入点分配给查询中的传感器Id?将有3个传感器,是放置在绘图,所以我想分配id和温度到一个单一的点。
我遇到的问题是它把第一个t值赋给第一个点,第二个t值赋给第二个点。如何设置指定给特定点的温度值?在
如果我在一个小时内获取所有数据,那么同一个传感器会有多个值。我想要某种时间控制,我可以绘制每个传感器的第一组结果-然后按一个按钮,每个传感器的下一个结果就会绘制出来。它们将同时运行,因此每个传感器id都会有一个要绘制的值
它也给出了这个错误-
^{pr2}$
这是因为两个值相同,所以cmap只有1个值吗?如果我将查询中的一个温度设置为0.56,它就会消失。在
我希望这有道理