# -*- coding: utf-8 -*- """ 图表1:某市居民每周锻炼时长 2019–2024 配套《完形前四篇_可迁移句型.md》第八节 B 节图表作文范例。 数据与范文完全对齐:2019 年 2.1 小时 → 2024 年 3.6 小时,稳步上升。 运行:python make_chart1.py 生成 图表1_每周锻炼时长.png(同目录) """ import os import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from matplotlib import font_manager as fm # ---------- 中文字体(Windows 自带 SimHei)---------- CJK = "SimHei" for p in (r"C:\Windows\Fonts\simhei.ttf", r"C:\Windows\Fonts\msyh.ttc"): if os.path.exists(p): fm.fontManager.addfont(p) CJK = fm.FontProperties(fname=p).get_name() break plt.rcParams["font.family"] = [CJK, "DejaVu Sans"] plt.rcParams["axes.unicode_minus"] = False # ---------- 数据 ---------- years = ["2019", "2020", "2021", "2022", "2023", "2024"] hours = [2.1, 2.3, 2.6, 2.9, 3.2, 3.6] C_BASE = "#7A9CC6" # 2019–2023 C_LAST = "#2F5597" # 2024 高亮 C_TEXT = "#333333" C_GRID = "#D9D9D9" # ---------- 画布 ---------- fig, ax = plt.subplots(figsize=(8.0, 4.6), dpi=200) fig.patch.set_facecolor("white") bars = ax.bar(years, hours, width=0.56, color=[C_BASE] * (len(years) - 1) + [C_LAST], edgecolor="white", linewidth=0.8, zorder=3) # 数值标签 for b, v in zip(bars, hours): ax.text(b.get_x() + b.get_width() / 2, v + 0.06, f"{v:.1f}", ha="center", va="bottom", fontsize=11.5, color=C_TEXT, zorder=4) # 首末两端各加一个小圆点,强化"起点→终点" ax.plot([0, len(years) - 1], [hours[0], hours[-1]], linestyle="none", marker="o", markersize=0, zorder=1) # ---------- 坐标轴 ---------- ax.set_ylim(0, 4.3) ax.set_ylabel("平均时长 / 小时", fontsize=11, color=C_TEXT, labelpad=8) ax.set_xlabel("年份", fontsize=11, color=C_TEXT, labelpad=6) ax.tick_params(axis="both", labelsize=10.5, colors=C_TEXT, length=0) ax.set_yticks([0, 1, 2, 3, 4]) ax.yaxis.grid(True, color=C_GRID, linewidth=0.8, linestyle=(0, (3, 3)), zorder=0) ax.set_axisbelow(True) for s in ("top", "right", "left"): ax.spines[s].set_visible(False) ax.spines["bottom"].set_color("#BFBFBF") # ---------- 标题 ---------- ax.set_title("图表 1 某市居民每周锻炼时长(2019–2024)", fontsize=14, color="#1F1F1F", pad=22, loc="left", x=0.0) ax.text(0.0, 1.045, "Average weekly exercise per urban resident, in hours", transform=ax.transAxes, fontsize=10, color="#7F7F7F", ha="left") # 右下角数据来源脚注(考场图表作文常给 Source) fig.text(0.905, 0.018, "总量增幅 +71%", fontsize=9.5, color=C_LAST, ha="right", va="bottom") fig.tight_layout(rect=[0.01, 0.03, 0.99, 0.96]) out = os.path.join(os.path.dirname(os.path.abspath(__file__)), "图表1_每周锻炼时长.png") fig.savefig(out, dpi=200, bbox_inches="tight", facecolor="white") print("saved:", out) print("data:", dict(zip(years, hours)))