Python Matplotlib and MySQL and Ginput -
i'm looking using python mysqldb , matplotlib. i'm looking use values of query within matplotlib scatter plot based on ginput plot. have following working:
import matplotlib.pyplot plt import matplotlib.image mpimg import numpy np pylab import * import random import mysqldb mdb import sys collections import defaultdict ##### start query #### db = mdb.connect('localhost', 'root', 'password', 'xbee') start = raw_input("enter start date: ") part_1 = "select xbee_address_al, xbee_temperature xbeereadings 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) k, v in s: d[k].append(v) = 0 temp = [item[i] item in d.values()] figure(figsize=(15, 8)) img = mpimg.imread('floor.png') imgplot = plt.imshow(img, cmap=cm.hot) print "left click plot sensors point on image - middle click remove last point - right click end plotting" # pts used ginput collect place sensor located. returns 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 values 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()
edit: got previous part of question working (how use query values cmap value) shown in new code. have taken temperature (divided 100 valid number) , placed in plot.
the questions help/code/starting points are:
1 - how can assign ginput point sensor id query? there 3 sensors placed on plot , assign id , temperature single point. problem have assigned first value of t first point - , second value of t second point. how can set temperature value assigned specific point?
if fetch 1 hour it's going give multiple values same sensor. kind of time control can plot first set of results each sensor - , press button , next result each sensor plotted. running @ same time there value plot each sensor id.
also it's giving error -
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
is because both values same , cmap has 1 value? goes away if set 1 of temperatures in query 0.56.
i hope makes sense
you running peculiarity of scalarmappables
. take care of normalizing data in range [0, 1] , passing value color map. default sets bottom of range min(values_you_are_mapping)
, top max, if values identical results in width of range being zero, , mapping (v - max_v) / (max_v - min_v)
blows up. solution tell range should by
imshow(..., vmin=min_t, vmax=max_t) scatter(..., vmin=min_t, vmax=max_t)
where max_t
, min_t
maximum , minimum temperatures ever get. make color mapping consistent across of figures.
Comments
Post a Comment