如何以可以与matplotlib一起使用的形式从烧瓶 - 塞拉尔奇米查询中提取数据列



我正在使用烧瓶 - 塞格勒查询的结果来绘制matplotlib中的数据。我有一些工作代码,但它不高,效率低下。我希望能够从单个查询中获取多个数据列,以我可以在matplotlib中使用的形式。我是Python的新手(使用v3.4(,但我还不是"得到"它。目前,我正在查询每一列数据的数据库。

所有人都非常感谢。

# views.py
import matplotlib.pyplot as plt
from .models import Sensor
def DrawChart():
    #  Draw a chart
    x = Sensor.query.with_entities(Sensor.UnixTime).filter(Sensor.UnixTime >= config.startDateTime).filter(
        Sensor.UnixTime <= config.endDateTime).all()
    y1 = Sensor.query.with_entities(Sensor.SupplyVoltage).filter(Sensor.UnixTime >= config.startDateTime).filter(
        Sensor.UnixTime <= config.endDateTime).all()
    y2 = Sensor.query.with_entities(Sensor.TotalCurrent).filter(Sensor.UnixTime >= config.startDateTime).filter(
        Sensor.UnixTime <= config.endDateTime).all()
    # Draw the chart
    fig, ax = plt.subplots()
    ax.plot(x, y1, 'k--')
    ax.plot(x, y2, 'ro')
# models.py
from . import db
db.Model.metadata.reflect(db.engine)
class Sensor(db.Model):
    """Sensor model links to sensor data table for displaying data and feeding MatPlotLib"""
    __table__ = db.Model.metadata.tables['sensorReadings']
    def __repr__(self):
        return 'Sensor model'

# __INIT__.py
from flask import Flask
from flask_sslify import SSLify
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_bcrypt import Bcrypt
import matplotlib
matplotlib.use('Agg')  # Stops errors when maplotlob tries to use X-windows backend
app = Flask(__name__, instance_relative_config=True)  # Use Flask instance folders to keep some settings out of GIT.
app.config.from_object('config')  # Main config file
app.config.from_pyfile('config.py')  # Instance config file
sslify = SSLify(app)  # Force Flask to use SSL i.e. HTTPS
bcrypt = Bcrypt(app)  # Initialize encryption for hashing passwords
db = SQLAlchemy(app)  # Initialize SQLite database

我认为您不必担心有多个查询的效率低下。但是,您确实可以使它变得更加优雅,例如照顾这个问题:如果其中一个查询中断怎么办?

我建议您将所有3个单独的查询保留,但要在try/除外,将它们包裹起来,以便如果一个查询失败,您将知道哪一个。它将帮助您进行调试,您还可以提示用户提供适当的反馈,从而改善用户体验。

这是一个样本:

    def DrawChart():
        #  Draw a chart
        try:
            x = Sensor.query.with_entities(Sensor.UnixTime).filter(
                Sensor.UnixTime >= config.startDateTime 
                and Sensor.UnixTime <= config.endDateTime
            ).all()
        except:
            # Throw some error
            print "error1"
        try:
            y1 = Sensor.query.with_entities(Sensor.SupplyVoltage).filter(
                Sensor.UnixTime >= config.startDateTime 
                and Sensor.UnixTime <= config.endDateTime
            ).all()
        except:
            print "error2"
        try:
            y2 = Sensor.query.with_entities(Sensor.TotalCurrent).filter(
                Sensor.UnixTime >= config.startDateTime 
                and Sensor.UnixTime <= config.endDateTime
            ).all()
        except:
            print "error3"
        # Draw the chart
        fig, ax = plt.subplots()
        ax.plot(x, y1, 'k--')
        ax.plot(x, y2, 'ro')

最新更新