在python&matplotlib中是否可以使用sqlite数据库中的数据绘制datetime而不使用pandas?
我是编程新手:。我有一个sqlite数据库,我从中插入和查询数据。它有两列,datetimetype文本和一个0或1整数。我想使用python和matplotlib中的绘图来获取这些数据 我试过了,但似乎日期时间格式及其转换给我带来了问题。我不想使用pandas,但未能在matplotlib中获得函数在python&matplotlib中是否可以使用sqlite数据库中的数据绘制datetime而不使用pandas?,python,pandas,matplotlib,Python,Pandas,Matplotlib,我是编程新手:。我有一个sqlite数据库,我从中插入和查询数据。它有两列,datetimetype文本和一个0或1整数。我想使用python和matplotlib中的绘图来获取这些数据 我试过了,但似乎日期时间格式及其转换给我带来了问题。我不想使用pandas,但未能在matplotlib中获得函数 import numpy as np import matplotlib.pyplot as plt import sqlite3 from datetime import datetime,da
import numpy as np
import matplotlib.pyplot as plt
import sqlite3
from datetime import datetime,date
#define datetimes
strt_timestamp=1234567878
end_timestamp=1234568980
#create and connect database and tables
conn=sqlite3.connect('table1.db')
cur=conn.cursor()
#cur.execute('CREATE TABLE machine1(dt_tim TEXT,workingcnd INT)')
conn=sqlite3.connect('table1.db')
cur.execute('DELETE FROM machine1')
#code to create table1 database
while strt_timestamp<=(end_timestamp-54):
strt_timestamp+=55
b=datetime.fromtimestamp(strt_timestamp)
a=b.strftime("%m/%d/%Y,%H:%M:%S");
c=strt_timestamp%2
cur.execute('INSERT INTO machine1(dt_tim,workingcnd)VALUES(?,?)',
(a,c));
cur.execute('SELECT dt_tim,workingcnd FROM machine1');
result=cur.fetchall()
print(result)
cur.execute('SELECT dt_tim FROM machine1')
dt_tim=cur.fetchall()
cur.execute('SELECT workingcnd FROM machine1')
cnd=cur.fetchall()
plt.plot_date(x,y)
plt.show()
似乎有两个问题。首先,cur.execute'SELECT dt_tim from machine1'的结果是一个元组列表。您需要将其解包以获得实际值的列表。 其次,您需要将日期字符串转换为datetime,以便能够使用matplotlib打印它们
import matplotlib.pyplot as plt
import sqlite3
from datetime import datetime
#define datetimes
strt_timestamp=1234567878
end_timestamp=1234568980
#create and connect database and tables
conn=sqlite3.connect('table1.db')
cur=conn.cursor()
#cur.execute('CREATE TABLE machine1(dt_tim TEXT,workingcnd INT)')
conn=sqlite3.connect('table1.db')
#cur.execute('DELETE FROM machine1')
#code to create table1 database
while strt_timestamp<=(end_timestamp-54):
strt_timestamp+=55
b=datetime.fromtimestamp(strt_timestamp)
a=b.strftime("%m/%d/%Y,%H:%M:%S");
c=strt_timestamp%2
cur.execute('INSERT INTO machine1(dt_tim,workingcnd)VALUES(?,?)',
(a,c));
cur.execute('SELECT dt_tim FROM machine1')
dt_tim=[datetime.strptime(d, "%m/%d/%Y,%H:%M:%S") for d, in cur.fetchall()]
cur.execute('SELECT workingcnd FROM machine1')
cnd=[int(d) for d, in cur.fetchall()]
conn.close()
print(dt_tim)
print(cnd)
plt.plot(dt_tim,cnd)
plt.show()
没有必要使用熊猫。除此之外,我可能不理解这个问题,代码中的x是什么?哦…我很抱歉…绘图函数应该是…'plt.plot_datedt_tim,cnd'very thanking@importanceofbeingernest,让我知道了很多事情。大部分问题已经解决。如果有更多的干扰,我会先搔搔头,万一再也不行了,我会回来的。感谢所有的方式:…schönes wockhenende。。。。