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 # average ~2,629,800
- SECONDS_PER_YEAR = 365.25 * 86_400 # average ~31,557,600
- 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
- # Each tuple: (height_in_seconds, display_label, component)
- # component: 'y', 'm', 'd'
- formats = {
- "yyyy-mm-dd (ISO 8601)": [
- (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"),
- ],
- "dd-mm-yyyy": [
- (D10, "d\n×10", "d"),
- (D1, "d\n×1", "d"),
- (M10, "m\n×10", "m"),
- (M1, "m\n×1", "m"),
- (Y1000, "y\n×1000", "y"),
- (Y100, "y\n×100", "y"),
- (Y10, "y\n×10", "y"),
- (Y1, "y\n×1", "y"),
- ],
- "mm/dd/yyyy": [
- (M10, "m\n×10", "m"),
- (M1, "m\n×1", "m"),
- (D10, "d\n×10", "d"),
- (D1, "d\n×1", "d"),
- (Y1000, "y\n×1000", "y"),
- (Y100, "y\n×100", "y"),
- (Y10, "y\n×10", "y"),
- (Y1, "y\n×1", "y"),
- ],
- }
- COLORS = {"y": "#1D6FA4", "m": "#1A7A3C", "d": "#C2530A"}
- def sci_label(v):
- 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):
- exp = int(np.floor(np.log10(v)))
- return rf"$10^{{{exp}}}$"
- fig, axes = plt.subplots(1, 3, figsize=(18, 8), sharey=True)
- fig.patch.set_facecolor("white")
- fig.suptitle(
- "Date Format Comparison",
- fontsize=15, fontweight="bold", color="#111827", y=0.97,
- )
- YMIN, YMAX = 3e4, 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")
- x = np.arange(8)
- 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.5, color="#1E293B")
- ax.set_title(title, fontsize=13, fontweight="bold", color="#111827", pad=10)
- ax.grid(axis="y", color="#CBD5E1", linewidth=0.6, zorder=0)
- # Annotate each bar with scientific notation
- for rect, val in zip(rects, heights):
- ax.text(
- rect.get_x() + rect.get_width() / 2,
- val * 2.0,
- sci_label(val),
- ha="center", va="bottom",
- fontsize=8.5, color="#111827", fontweight="bold",
- )
- ax.yaxis.set_major_formatter(ticker.FuncFormatter(sci_tick))
- ax.yaxis.set_minor_locator(ticker.NullLocator())
- if ax == axes[0]:
- ax.set_ylabel("Seconds (log scale)", color="#1E293B", fontsize=11)
- ax.set_xlabel("Digit position →", color="#64748B", fontsize=9)
- # Legend
- 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"),
- ]
- fig.legend(
- handles=patches,
- loc="lower center",
- ncol=4,
- 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/date_formats_comparison.png", dpi=150,
- bbox_inches="tight", facecolor=fig.get_facecolor())
- print("Saved to /workspace/date_formats_comparison.png")
Advertisement
Add Comment
Please, Sign In to add comment