Warum zeichnet Matplotlib DatetimeIndex falsch ausgerichtete Datumsangaben auf?Python

Python-Programme
Anonymous
 Warum zeichnet Matplotlib DatetimeIndex falsch ausgerichtete Datumsangaben auf?

Post by Anonymous »

Warum rundet matplotlib manchmal auf den nächsten Monat und zu anderen Zeiten nicht, abhängig von den Auftragszeilen, die dargestellt werden, und den verwendeten Daten? ax3 und ax4 sind besonders interessant, da sie nicht am Rand einer neuen Monatsgrenze liegen. Wie kann ich das Verhalten von ax2 erzwingen, ohne von der Reihenfolge der Darstellung abhängig zu sein?

Code: Select all

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure()
gs = gridspec.GridSpec(4, 1, height_ratios=[1, 1, 1, 1])
ax1 = fig.add_subplot(gs[0])
ax2 = fig.add_subplot(gs[1])
ax3 = fig.add_subplot(gs[2])
ax4 = fig.add_subplot(gs[3])

ax1.set_title("Month ends first")
pd.Series([0, 2, 0], pd.DatetimeIndex(["2025-11-30", "2025-12-31", "2026-01-31"])).plot(ax=ax1, c="b")
pd.Series([0, 1, 0], pd.DatetimeIndex(["2025-11-30", "2025-12-15", "2026-01-31"])).plot(ax=ax1, c="r")

ax2.set_title("Middle of month first")
pd.Series([0, 1, 0], pd.DatetimeIndex(["2025-11-30", "2025-12-15", "2026-01-31"])).plot(ax=ax2, c="r")
pd.Series([0, 2, 0], pd.DatetimeIndex(["2025-11-30", "2025-12-31", "2026-01-31"])).plot(ax=ax2, c="b")

ax3.set_title("Not end of month first")
pd.Series([0, 2, 0], pd.DatetimeIndex(["2025-11-28", "2025-12-31", "2026-01-31"])).plot(ax=ax3, c="b")
pd.Series([0, 1, 0], pd.DatetimeIndex(["2025-11-30", "2025-12-15", "2026-01-31"])).plot(ax=ax3, c="r")

ax4.set_title("Not end of month first fence post")
pd.Series([0, 2, 0], pd.DatetimeIndex(["2025-11-27", "2025-12-31", "2026-01-31"])).plot(ax=ax4, c="b")
pd.Series([0, 1, 0], pd.DatetimeIndex(["2025-11-30", "2025-12-15", "2026-01-31"])).plot(ax=ax4, c="r")

plt.show()
Image

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post