Hallo zusammen,
ich habe ein Problem mit der matplotlib und hoffe, ihr könnt mir helfen.
Ich habe eine Datenbank, mache eine Abfrage und möchte das Ergebnis dieser Abfrage als Balkendiagramm mit Hilfe von matplotlib ausgeben.
An sich ist ein solcher Plot (eine x-Achse und eine y-Achse) kein Problem. Ich möchte nun noch eine zweite y-Achse einfügen und auch weitere "Balken" einfügen, die sich auf die zweite y-Achse beziehen. Ich hoffe, dass das einigermaßen verständlich geschrieben ist. Im Internet habe ich auch schon was dazu gefunden (twinx), allerdings hat es bei mir bisher nicht funktioniert.
Mein bisheriger Code sieht so aus:
Code:
import sqlite3
import numpy as np
import matplotlib.pyplot as plt
connection = sqlite3.connect("MeineDatenbank")
cursor = connection.cursor()
cursor.execute("Select * from MeineTabelle")
ThisTable = cursor.fetchall()
Datum = []
Zeit = []
Distanz = []
for entry in ThisTable:
Datum.append(entry[0])
Zeit.append(entry[1])
Distanz.append(entry[2])
print ThisTable
fig = plt.figure()
N = len(Datum)
ind = np.arange(N)
width = 0.35
ax1 = fig.add_subplot(111)
ax1.bar(ind, Zeit, width, color='grey')
ax1.set_ylabel('Zeit', color='b')
ax1.set_xlabel('YYYY/MM', color='black')
ax1.set_xticks(ind + width)
ax1.set_xticklabels(Datum)
ax2 = ax1.twinx()
M = max(Distanz)
ind2 = np.arange(M)
ax2.bar(ind, Distanz, width, color='red')
ax2.set_ylabel('Distanz', color='r')
plt.show()
Im Anhang ist ein Beispielplot, wie ich ihn am liebsten hätte. Allerdings erscheinen bei mir immer nur ein "paar" Balken. Nie die von beiden y-Achsen.
Habt ihr vielleicht eine Idee oder vielleicht sogar eine Lösung für mich?
Gruß
Isa
Lesezeichen