Python Matplotlib和MySQL和Ginput [英] Python Matplotlib and MySQL and Ginput
问题描述
我正在研究将Python与MySQLdb和Matplotlib结合使用.我正在寻找在基于ginput图的matplotlib散点图中使用查询的值.我的工作如下:
I'm looking into using Python with MySQLdb and Matplotlib. I'm looking to use the values of a query within a matplotlib scatter plot based on a ginput plot. I have the following working:
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得到一个有效的数字),然后将其放置在绘图中.
I got the previous part of the question working (how to use a query values as a cmap value) as shown in the new code. I have taken the temperature (divided by 100 to get a valid number) and then placed it in the plot.
我现在想要一些帮助/代码/起点的问题是:
The questions I would now like some help/code/starting points for are:
1-如何从查询中为传感器ID分配ginput点?图上将放置3个传感器,因此我想将ID和温度分配给单个点. 我遇到的问题是,它将t的第一个值分配给了第一点-将t的第二个值分配给了第二点.如何设置将哪个温度值分配给特定点?
1 - How can I assign a ginput point to a sensor Id from the query? There will be 3 sensors that are placed on the plot and so I would like to assign the id and temperature to a single point. The problem I have is that it assigned the first value of t to the first point - and the second value of t to the second point. How can I set which temperature value is assigned to a specific point?
如果我要说1个小时全部取一次,它将为同一个传感器提供多个值.我想要某种时间控制,可以在其中绘制每个传感器的第一组结果-然后按一个按钮,然后绘制每个传感器的下一个结果.它们将同时运行,因此将始终为每个传感器ID绘制一个值.
If I do fetch all for say 1 hour it's going to give multiple values for the same sensor. I would like some kind of time control where I can plot the first set of results for each sensor - and then press a button and the next result for each sensor is plotted. They will all be running at the same time so there will always be a value to plot for each sensor id.
也正在给出此错误-
C:\Python27\lib\site-packages\matplotlib\colorbar.py:808: RuntimeWarning: invali
d value encountered in divide
z = np.take(y, i0) + (xn-np.take(b,i0))*dy/db
Traceback (most recent call last):
File "heatmap2.py", line 51, in <module>
show()
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 143, in show
_show(*args, **kw)
File "C:\Python27\lib\site-packages\matplotlib\backend_bases.py", line 108, in
__call__
self.mainloop()
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", lin
e 69, in mainloop
Tk.mainloop()
File "C:\Python27\lib\lib-tk\Tkinter.py", line 325, in mainloop
_default_root.tk.mainloop(n)
KeyboardInterrupt
是因为两个值都相同,所以cmap仅具有1个值吗?如果我在查询中将温度之一设置为0.56,它就消失了.
Is that because both values are the same and so the cmap only has 1 value? It goes away if I set one of the temperatures in the query to say 0.56.
我希望这是有道理的
推荐答案
您正在遇到ScalarMappables
的特殊之处.他们负责将数据规范化为[0,1]范围,并将该值传递给颜色图.默认情况下,它将范围的底部设置为min(values_you_are_mapping)
,将顶部设置为最大值,如果所有值都相同,则会导致范围的宽度为零,并且映射(v - max_v) / (max_v - min_v)
会爆炸.解决方法是告诉它范围应该是什么
You are running into a peculiarity of ScalarMappables
. They take care of normalizing the data to be in the range [0, 1] and passing that value to the color map. By default it sets the bottom of the range to min(values_you_are_mapping)
and the top to the max, which if all your values are identical results in the width of the range being zero, and the mapping (v - max_v) / (max_v - min_v)
blows up. The solution is to tell it what the range should be by
imshow(..., vmin=min_t, vmax=max_t)
scatter(..., vmin=min_t, vmax=max_t)
其中,max_t
和min_t
是您可能获得的最高和最低温度.这还将使颜色映射在所有图形中保持一致.
where max_t
and min_t
are the maximum and minimum temperatures you could ever get. This will also make the color mapping consistent across all of your figures.
这篇关于Python Matplotlib和MySQL和Ginput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!