Seite 1 von 1

Schleife im subplott

Verfasst: Mittwoch 12. Dezember 2018, 12:10
von Mogician
Hallo,

ich hab gerade folgendes Problem.

Ich arbeite viel mit subplotts, und möchte die Befehle nicht unter jeden subplott einzeln schreiben.
Habe es mit i in range() ausprobiert, aber bekomme Fehlermeldungen und Python reagiert nicht mehr.

Code: Alles auswählen

fig,ax=plt.subplots(7,1)

df['TI111'].plot(ax=ax[0])
df['TI112'].plot(ax=ax[0])
df['TI113'].plot(ax=ax[0])
df['TI114'].plot(ax=ax[0])
df['TI115'].plot(ax=ax[0])
df['TI116'].plot(ax=ax[0])
for i in range(6):
    ax[i].legend()
    ax[i].grid(True)
    ax[i].set_ylim([20, 210])
    ax[i].set_xlim(['2018-12-11 12:38:00', '2018-12-11 13:51:00'])
    ax[i].set_title('Temperaturfühler A')
    ax[i].set_xlabel('Time[s]')
    ax[i].set_ylabel('Temperature [°C]')



df['TI117'].plot(ax=ax[1])
df['TI118'].plot(ax=ax[1])
df['TI119'].plot(ax=ax[1])
df['TI120'].plot(ax=ax[1])
df['TI121'].plot(ax=ax[1])
df['TI122'].plot(ax=ax[1])


df['TI123'].plot(ax=ax[2])
df['TI124'].plot(ax=ax[2])
df['TI125'].plot(ax=ax[2])
df['TI126'].plot(ax=ax[2])
df['TI127'].plot(ax=ax[2])
df['TI128'].plot(ax=ax[2])


df['TI129'].plot(ax=ax[3])
df['TI130'].plot(ax=ax[3])
df['TI131'].plot(ax=ax[3])
df['TI132'].plot(ax=ax[3])
df['TI133'].plot(ax=ax[3])
df['TI134'].plot(ax=ax[3])


df['TI135'].plot(ax=ax[4])
df['TI136'].plot(ax=ax[4])
df['TI137'].plot(ax=ax[4])
df['TI138'].plot(ax=ax[4])
df['TI139'].plot(ax=ax[4])
df['TI140'].plot(ax=ax[4])


df['TI141'].plot(ax=ax[5])
df['TI142'].plot(ax=ax[5])
df['TI143'].plot(ax=ax[5])
df['TI144'].plot(ax=ax[5])
df['TI145'].plot(ax=ax[5])
df['TI146'].plot(ax=ax[5])


df['TI101'].plot(ax=ax[6])
df['TI102'].plot(ax=ax[6])
df['TI103'].plot(ax=ax[6])
df['TI104'].plot(ax=ax[6])
df['TI105'].plot(ax=ax[6])
df['TI106'].plot(ax=ax[6])
df['TI107'].plot(ax=ax[6])
df['TI108'].plot(ax=ax[6])
df['TI109'].plot(ax=ax[6])
df['TI110'].plot(ax=ax[6])
df['TI147'].plot(ax=ax[6])
Fehlermeldung ist dann': No handles with labels found to put in legend.


Kann mir einer sagen, wie das mit i in range(0) richtig geht? Das würde dauerhaft viel Arbeit ersparen.
Danke

Re: Schleife im subplott

Verfasst: Mittwoch 12. Dezember 2018, 14:45
von Sirius3
Wenn ich das richtig sehe, willst Du Legenden erzeugen, bevor Du etwas geplottet hast, das sollte danach passieren.
Die Benennung der Felder in Deinem Dataframe ist sehr schlecht. Warum heißt das alles TIxxx, obwohl es sich doch augenscheinlich um verschiedene Daten handelt?

Pack alles in eine Schleife:

Code: Alles auswählen

PLOT_COLUMNS = [
    ['TI111', 'TI112', 'TI113', 'TI114', 'TI115', 'TI116'],
    ['TI117', 'TI118', 'TI119', 'TI120', 'TI121', 'TI122'],
    ...
]

fig, ax = plt.subplots(7, 1)
for axis, columns in zip(ax, PLOT_COLUMNS):
    for col in columns;
        df[col].plot(ax=axis)
    axis.legend()
    axis.grid(True)
    axis.set_ylim([20, 210])
    axis.set_xlim(['2018-12-11 12:38:00', '2018-12-11 13:51:00'])
    axis.set_title('Temperaturfühler A')
    axis.set_xlabel('Time[s]')
    axis.set_ylabel('Temperature [°C]')

Re: Schleife im subplott

Verfasst: Mittwoch 12. Dezember 2018, 16:48
von Mogician
Da bekomme ich einen Syntaxfehler, weiß nicht warum, verstehe den Code nicht so schnell.

Hab aber auf Grundlage von meinem Code noch eine zweite Frage.

Wie kann ich etwas auf der x-Achse verschieben? Also die x-Achse ist bei mir festgelegt als die Zeit, und davon würde ich nun bei manchen Graphen gerne eine bestimmte Zeit abziehen, also zb 2 Stunden bzw 02:00:00

Weiß aber nicht, wo ich das eingeben muss, weil er scheinbar die Zeit als Index standardmäßig nimmt. Hab den Code nicht ganz selbst geschrieben, muss aber damit arbeiten so gut wie möglich. Arbeite zum ersten mal mit Python.

Re: Schleife im subplott

Verfasst: Donnerstag 13. Dezember 2018, 09:20
von Mogician
So, also wie Sirius3 angemerkt hatte, war das Problem, dass die Schleife nach dem ersten Subplot kam. Schreibt man es unter den letzten, funktioniert es.

Danke