Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import matplotlib.patches as mpatches
- import matplotlib.ticker as ticker
- import numpy as np
- SECONDS_PER_DAY = 86_400
- SECONDS_PER_MONTH = 365.25 * 86_400 / 12
- SECONDS_PER_YEAR = 365.25 * 86_400
- D1 = SECONDS_PER_DAY
- D10 = 10 * SECONDS_PER_DAY
- M1 = SECONDS_PER_MONTH
- M10 = 10 * SECONDS_PER_MONTH
- Y1 = SECONDS_PER_YEAR
- Y10 = 10 * SECONDS_PER_YEAR
- Y100 = 100 * SECONDS_PER_YEAR
- Y1000 = 1000 * SECONDS_PER_YEAR
- H1 = 3600
- H10 = 10 * 3600
- MIN1 = 60
- MIN10 = 600
- S1 = 1
- S10 = 10
- # Each tuple: (height_in_seconds, display_label, component)
- formats = {
- "yyyy-mm-dd HH:MM:SS": [
- (Y1000, "y\n×1000", "y"),
- (Y100, "y\n×100", "y"),
- (Y10, "y\n×10", "y"),
- (Y1, "y\n×1", "y"),
- (M10, "m\n×10", "m"),
- (M1, "m\n×1", "m"),
- (D10, "d\n×10", "d"),
- (D1, "d\n×1", "d"),
- (H10, "H\n×10", "h"),
- (H1, "H\n×1", "h"),
- (MIN10, "M\n×10", "n"),
- (MIN1, "M\n×1", "n"),
- (S10, "S\n×10", "s"),
- (S1, "S\n×1", "s"),
- ],
- }
- COLORS = {
- "y": "#1D6FA4",
- "m": "#1A7A3C",
- "d": "#C2530A",
- "h": "#7B3FA0",
- "n": "#B8860B",
- "s": "#B22222",
- }
- def sci_label(v):
- if v < 1:
- return rf"$10^{{0}}$"
- exp = int(np.floor(np.log10(v)))
- coeff = v / 10**exp
- if abs(coeff - 1.0) < 0.05:
- return rf"$10^{{{exp}}}$"
- return rf"${coeff:.1f}\times10^{{{exp}}}$"
- def sci_tick(v, _=None):
- if v <= 0:
- return ""
- exp = int(np.floor(np.log10(v)))
- return rf"$10^{{{exp}}}$"
- fig, axes = plt.subplots(1, 1, figsize=(10, 8))
- axes = [axes]
- fig.patch.set_facecolor("white")
- fig.suptitle(
- "ISO 8601 digit significance",
- fontsize=15, fontweight="bold", color="#111827", y=0.97,
- )
- YMIN, YMAX = 0.5, 1.5e11
- for ax, (title, bars) in zip(axes, formats.items()):
- ax.set_facecolor("#F8FAFC")
- ax.spines[:].set_color("#CBD5E1")
- ax.tick_params(colors="#1E293B")
- ax.yaxis.label.set_color("#1E293B")
- n = len(bars)
- x = np.arange(n)
- heights = [b[0] for b in bars]
- labels = [b[1] for b in bars]
- comps = [b[2] for b in bars]
- bar_colors = [COLORS[c] for c in comps]
- rects = ax.bar(x, heights, color=bar_colors, edgecolor="white",
- linewidth=0.8, zorder=3, width=0.65, alpha=0.88)
- ax.set_yscale("log")
- ax.set_ylim(YMIN, YMAX)
- ax.set_xticks(x)
- ax.set_xticklabels(labels, fontsize=9, color="#1E293B")
- ax.set_title(title, fontsize=12, fontweight="bold", color="#111827", pad=10,
- fontfamily="monospace")
- ax.grid(axis="y", color="#CBD5E1", linewidth=0.6, zorder=0)
- # Vertical separator between date and time digits
- ax.axvline(x=7.5, color="#94A3B8", linewidth=1.2, linestyle="--", zorder=2)
- for rect, val in zip(rects, heights):
- ax.text(
- rect.get_x() + rect.get_width() / 2,
- val * 2.5,
- sci_label(val),
- ha="center", va="bottom",
- fontsize=8, color="#111827", fontweight="bold",
- )
- ax.yaxis.set_major_formatter(ticker.FuncFormatter(sci_tick))
- ax.yaxis.set_minor_locator(ticker.NullLocator())
- if True:
- ax.set_ylabel("Seconds", color="#1E293B", fontsize=11)
- ax.set_xlabel("Digit position →", color="#64748B", fontsize=9)
- patches = [
- mpatches.Patch(color=COLORS["y"], alpha=0.88, label="Year digit"),
- mpatches.Patch(color=COLORS["m"], alpha=0.88, label="Month digit"),
- mpatches.Patch(color=COLORS["d"], alpha=0.88, label="Day digit"),
- mpatches.Patch(color=COLORS["h"], alpha=0.88, label="Hour digit"),
- mpatches.Patch(color=COLORS["n"], alpha=0.88, label="Minute digit"),
- mpatches.Patch(color=COLORS["s"], alpha=0.88, label="Second digit"),
- ]
- fig.legend(
- handles=patches,
- loc="lower center",
- ncol=6,
- fontsize=10,
- framealpha=1.0,
- labelcolor="#111827",
- facecolor="white",
- edgecolor="#CBD5E1",
- bbox_to_anchor=(0.5, 0.01),
- )
- plt.tight_layout(rect=[0, 0.07, 1, 0.96])
- plt.savefig("/workspace/datetime_formats_comparison.png", dpi=150,
- bbox_inches="tight", facecolor=fig.get_facecolor())
- print("Saved to /workspace/datetime_formats_comparison.png")
Advertisement
Add Comment
Please, Sign In to add comment