尝试从sqlite3数据库中绘制简单的温度数据。 python的新手,不知道我做错了什么。来自sql的数据如下:
[(70.8,),(70.8,),(70.9,),(71.0,),(71.0,),(71.2,),(71.2,),(71.2,),(71.4,),( 71.7,),(71.7,),(72.0,),(72.0,),(72.0,),(72.2,),(72.2,),(71.9,),(72.0,),(72.0,),( 72.2,),(72.2,),(72.2,),(72.2,),(71.7,),(71.9,),(71.9,),(72.0,),(72.0,),(72.3,),( 72.0,),(72.0,),(72.2,),(72.2,),(72.2,),(72.3,),(72.2,),(72.2,),(72.3,),(72.3,),( 72.3,),(72.3,),(72.4,),(72.5,),(72.4,),(72.5,),(72.5,),(72.6,),(72.7,),(72.7,),( 73.0,),(73.0,),(73.0,),(73.1,),(73.1,),(73.3,),(73.5,),(73.5,),(73.6,),(73.7,),( 73.8,),(73.7,),(73.7,),(73.7,),(73.8,),(73.7,),(73.7,),(73.6,),(73.7,),(73.8,),( 73.8,),(73.8,),(73.8,),(73.7,),(73.6,),(73.6,),(73.5,),(73.6,),(73.5,),(73.5,),( 73.5,),(73.5,),(73.2,),(73.1,),(73.0,),(73.0,),(72.7,),(72.8,),(72.8,),(72.8,),( 72.7,),(72.7,),(72.6,),(72.6,),(72.7,),(72.7,),(72.7,),(72.7,),(72.7,),(72.8,),( 72.7,),(72.8,),(72.8,),(72.8,),(72.8,),(72.8,),(72.8,),(72.7,),(72.7,),(72.7,),( 73.0,),(73.0, ),(73.0,),(73.0,),(73.4,),(73.5,),(73.5,),(73.6,),(73.5,),(73.6,),(73.5,),(73.5, ),(73.6,),(73.6,),(73.5,),(73.5,),(73.6,),(73.5,),(73.6,),(73.6,),(73.6,),(73.6, ),(73.5,)]
我得到的错误信息是:
Traceback (most recent call last):
File "db-test5.py", line 93, in <module>
renderPM.drawToFile(d, 'rework6.png')
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 655, in drawToFile
c = drawToPMCanvas(d, dpi=dpi, bg=bg, configPIL=configPIL,showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 641, in drawToPMCanvas
draw(d, c, 0, 0, showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 50, in draw
R.draw(renderScaledDrawing(drawing), canvas, x, y, showBoundary=showBoundary)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 199, in draw
self.drawNode(drawing)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderPM.py", line 109, in drawNode
self.drawNodeDispatcher(node)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 280, in drawNodeDispatcher
self.drawGroup(node)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 297, in drawGroup
node = _expandUserNode(node,canvas)
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/renderbase.py", line 161, in _expandUserNode
node = node.provideNode()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/widgetbase.py", line 150, in provideNode
return self.draw()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/charts/lineplots.py", line 351, in draw
self.calcPositions()
File "/usr/lib/python2.7/dist-packages/reportlab/graphics/charts/lineplots.py", line 194, in calcPositions
if isinstance(datum[0],str):
TypeError: 'float' object has no attribute '__getitem__'
我的代码是:
#!/usr/bin/python
from reportlab.graphics.charts.lineplots import LinePlot, AreaLinePlot
from reportlab.graphics.shapes import Drawing
from reportlab.lib import colors
from random import randint
from datetime import date, timedelta
from reportlab.graphics import renderPM
import sqlite3, os, datetime, time
##############################################
######## time functions ##################
##############################################
def RecYear():
return datetime.date.today().strftime("%Y")
def RecMonth():
return datetime.date.today().strftime("%m")
def RecFullDateTime():
return time.time()
def RecTime():
return datetime.datetime.now().strftime("%H:%M:%S")
def RecDate():
return datetime.datetime.now().strftime("%m-%d-%Y")
def RecDay():
return datetime.date.today().strftime("%d")
currentdate_ds18b20=int(datetime.date.today().strftime("%Y"))*1000+ int(time.localtime().tm_yday)
os.system('clear')
sqlite_file="2-database_archived/archived_2017-02-27-HMS-ds18b20-v2.2-DATABASE.db"
table_name = "DS18B20temps"
print "filename = ", sqlite_file
print "table = ", table_name
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
print "db open"
### Calculate total length of table
c.execute('SELECT * FROM {tn}'.\
format(tn=table_name))
print "the length of the database is:", len(c.fetchall()), " rows\n"
###Calculate total number of kitchen temp readings
c.execute('SELECT ({coi}) FROM {tn} WHERE {cn}="Kitchen"'.\
format(coi="temp", tn=table_name, cn="sensor_name"))
kitchen_temp_list = c.fetchall()
print "the number of kitchen temp readings = ", len(kitchen_temp_list)
new_kitchen_list = list(sum(kitchen_temp_list, ()))
print "averge temp = ", (sum(new_kitchen_list))/len(new_kitchen_list)
print ""
# 4) Retrieve all IDs of entries between 6am and noon
c.execute("SELECT {idf} FROM {tn} WHERE {cnn}='Kitchen' AND {cn} BETWEEN '18:00:00' AND '23:59:59'".\
format(idf="temp", tn=table_name, cnn="sensor_name", cn="recorded_time"))
all_date_times = c.fetchall()
print "readings from 6 pm to midnight = ", len(all_date_times)
print all_date_times
width=800
height=500
d = Drawing(width, height)
lp = LinePlot()
lp.data=all_date_times
lp.width= 700
lp.height = 400
lp.xValueAxis.valueMin = 0
lp.xValueAxis.valueMax =400
#lp.xValueAxis.valueSteps = [6,8,10,12,14,16,18,20]
lp.yValueAxis.valueMin = 0
lp.yValueAxis.valueMax =200
lp.strokeColor=colors.black
lp.fillColor=colors.grey
#lp.reversePlotOrder = False
lp.joinedLines=1
d.add(lp)
renderPM.drawToFile(d, 'rework6.png')
每当我传递错误的数据时它就会在renderPM中出现,但是我很难理解错误。我的预感是我传递的数据不适合线图?
任何帮助都非常感激......
答案 0 :(得分:0)
在完成报告实验室文档并试验他们的示例后,我确定问题在于我的数据是如何呈现的。使用报告实验室文档中的示例数据,我能够消除调用renderPM时出现的错误。
然后基于Muhammad Haseeb Khan和Paul Rooney的帮助post
我能够将[(70.1,),(71.1,),(71.1,)]到[(70.1,71.1,71.1)]的数据修改为:
c.execute("SELECT {idf} FROM {tn} WHERE {cnn}='Kitchen' AND {cn} BETWEEN '18:00:00' AND '23:59:59'".\
format(idf="temp", time="read_ID", tn=table_name, cnn="sensor_name",
cn="recorded_time"))
all_date_times = c.fetchall()
print "readings from 6 pm to midnight = ", len(all_date_times)
level1 = [list(row) for row in all_date_times]
level2=[i[0] for i in all_date_times]
level3=[tuple(level2)]
#### build report lab chart
drawing = Drawing(600, 400)
data = level3
lc = HorizontalLineChart()
lc.x = 25
lc.y = 25
lc.height = 450
lc.width = 550
lc.data = data
lc.joinedLines = 1
lc.valueAxis.valueMin = 70
lc.valueAxis.valueMax = 75
drawing.add(lc)
renderPM.drawToFile(drawing, 'chartgraphicfile.png')