Seite 1 von 1

Uhrzeit auf x-achse in matplotlib anpassen

Verfasst: Donnerstag 28. Januar 2021, 01:15
von geckocode
Hallo,
ich will eine Grafik mit matplotlib erstellen und nutze Daten aus einer *.csv datei im format: ('%y%m%d%H%M%S') alternativ habe ich die Uhrzeit auch in (H:M) also [10:00, 11:01,...]
bisher habe ich einfach die Uhrzeit als String genommen aber da ich jede minute einen Wert habe, dürfen später auch nicht alle angezeigt werden, nur so jede halbe Stunde....
ich dachte es ist besser nicht mit string zu arbeiten, komme aber nicht weiter.
hier ein Beispiel, was ich bisher habe. vielleicht hat jemand eine gute Idee?

Code: Alles auswählen

from matplotlib import pyplot as plt
import matplotlib.dates as mdt

x = ['201206000007', '201206000107', '201206000207', '201206000307', '201206000407', '201206000507', '201206000607', '201206000707', '201206000807', '201206000907', '201206001007', '201206001107', '201206001207', '201206001407'] 
y = [51.56, 51.5, 51.44, 51.5, 51.56, 51.69, 51.81, 51.5, 51.38, 50.25, 50.81, 51.75, 52.56, 53.56]

fig, ax = plt.subplots(1)
plt.plot(x,y)

fig.autofmt_xdate()
xfmt = mdt.DateFormatter('%y%m%d%H%M%S')
ax.xaxis.set_major_formatter(xfmt)
plt.show()

Re: Uhrzeit auf x-achse in matplotlib anpassen

Verfasst: Samstag 30. Januar 2021, 01:06
von geckocode
ich habs geschafft. :-)
zeitstring umwandeln:
zeit = [datetime.strptime(date, '%y%m%d%H%M%S') for date in dtime]

und so in matplotlib für x-achse einbinden:
fig.autofmt_xdate() # setzt Uhrzeit auf x-achse schräg
xfmt = mdt.DateFormatter('%H:%M')
ax2.xaxis.set_major_formatter(xfmt)