matplotlib mysql,Python Matplotlib和MySQL和Ginput

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()

EDIT: 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 - 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?

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.

Also It's giving this 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

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 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.

I hope that makes sense

解决方案

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)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值