PostgreSQL Datenbank teile auslesen und bearbeiten

Installation und Anwendung von Datenbankschnittstellen wie SQLite, PostgreSQL, MariaDB/MySQL, der DB-API 2.0 und sonstigen Datenbanksystemen.
Antworten
Hypec
User
Beiträge: 183
Registriert: Mittwoch 1. August 2018, 16:11

Ich hab das ganze jetzt gelöst das einzige Problem was ich bisher nicht lösen konnte is das mir bei dem ax.plot mit 2 Messwerten 4 Sachen im Label angezeigt werden anstatt 2 wenn ich den 2. Wert rausnehme werden mir im Label wie gewollt 2 Sachen angezeigt hat jemand vlt ne idee wie ich das ändern kann?

Code: Alles auswählen

PLOTS = {
    'luftfeuchtigkeit': {
        'title': 'Luftfeuchtigkeit',
        'ylabel': 'Luftfeuchtigkeit in %',
        'plots': [('Luftfeuchtigkeit innen'), ('Luftfeuchtigkeit ausen')]
    },
    'temperatur': {
        'title': 'Temperatur',
        'ylabel': 'Temperatur in °C',
        'plots': [("Temperatur innen"), ("Temperatur ausen")]
    },
    'erdfeuchtigkeit': {
        'title': 'Erdfeuchtigkeit',
        'ylabel': 'Erdfeuchtigkeit in %',
        'plots': [('Erdfeuchtigkeit')]
    },
    'lux': {
        'title': 'Lichtitensität',
        'ylabel': 'Lichtitensität in Lux',
        'plots': [('Lichtitensität(lux)')]
    },
}

@app.route('/image/<filename>', methods=['GET', 'POST'])
def data(filename):
    data_one = filename.split('.')[0]
    
    conn = psycopg2.connect()
    print ("Opened database successfully")

    cur = conn.cursor()

    cur.execute("SELECT TIMESTAMP, LUFTFEUCHTIGKEITDRIN, TEMPERATURDRIN, LUFTFEUCHTIGKEITAUSEN, TEMPERATURAUSEN, ERDFEUCHTIGKEIT, LUX  from MESSDATEN")

    rows = cur.fetchall()

    time = []
    wertone = []
    werttwo = []
    rowone = 1
    rowtwo = 2
    plot = PLOTS[data_one]

    if data_one == 'luftfeuchtigkeit':
        rowone = 1
        rowtwo = 3
    elif data_one == 'temperatur' :
        rowone = 2
        rowtwo = 4
    elif data_one == 'erdfeuchtigkeit' :
        rowone = 5
    elif data_one == 'lux' :
        rowone = 6
    else :
        print ("error")

    for row in rows:
        time.append(row[0])
        wertone.append(row[rowone])
        if data_one == 'luftfeuchtigkeit' or data_one == 'temperatur':
            werttwo.append(row[rowtwo])

    fig, ax = plt.subplots()
    if data_one == 'luftfeuchtigkeit' or data_one == 'temperatur':
        for label in plot['plots']:
            ax.plot(time, wertone, werttwo, label=label)
    else:
        for label in plot['plots']:
            ax.plot(time, wertone, label=label ) 
    ax.grid(True)
    fig.legend()
    ax.set_title(plot['title'])
    ax.set_ylabel(plot['ylabel'])
    fig.autofmt_xdate()

    buffer = io.BytesIO()
    plt.savefig(buffer, format = 'png')
    plot_data = buffer.getvalue()

    response = make_response(plot_data)
    response.headers['Content-type'] = 'image/png'
    return response
Antworten