fork download
  1. import matplotlib
  2. # 关键:设置无交互后端,适配 ideone 云端环境
  3. matplotlib.use('Agg')
  4.  
  5. import matplotlib.pyplot as plt
  6. import numpy as np
  7. from io import BytesIO
  8.  
  9. # 适配Linux环境中文字体
  10. plt.rcParams["font.family"] = ["DejaVu Sans"]
  11. plt.rcParams['axes.unicode_minus'] = False
  12.  
  13. cities = [
  14. "Nanning", "Liuzhou", "Guilin", "Wuzhou", "Beihai", "Fangchenggang", "Qinzhou",
  15. "Guigang", "Yulin", "Baise", "Hezhou", "Hechi", "Laibin", "Chongzuo"
  16. ]
  17. out_contract = np.array([16.18, 31.03, 20.19, 0.64, 2.38, 0.25, 2.01,
  18. 5.16, 1.88, 0.29, 6.15, 4.92, 7.13, 1.11])
  19. in_contract = np.array([162.52, 20.02, 6.89, 12.80, 4.66, 4.09, 57.62,
  20. 10.42, 13.73, 3.78, 1.66, 8.31, 6.69, 2.27])
  21. total = out_contract + in_contract
  22. finish_rate = np.array([46, 43, 46, 35, 5, 7, 70, 22, 22, 8, 30, 24, 36, 7])
  23.  
  24.  
  25. def save_fig(name):
  26. buf = BytesIO()
  27. plt.tight_layout()
  28. plt.savefig(buf, format='png', dpi=100, bbox_inches="tight")
  29. print(f"{name}‑plot generated (bytes length: {len(buf.getvalue())})")
  30. buf.close()
  31.  
  32.  
  33. # 1. Pie chart
  34. plt.figure(figsize=(10, 10))
  35. plt.pie(total, labels=cities, autopct="%.1f%%", startangle=90)
  36. plt.title("City Total‑Contract Proportion")
  37. save_fig("pie")
  38. plt.close()
  39.  
  40. # 2. Group bar chart
  41. x = np.arange(len(cities))
  42. width = 0.35
  43. plt.figure(figsize=(14, 7))
  44. plt.bar(x - width/2, out_contract, width, label="Output Contract")
  45. plt.bar(x + width/2, in_contract, width, label="Absorb Contract")
  46. plt.xticks(x, cities, rotation=45, ha="right")
  47. plt.xlabel("City")
  48. plt.ylabel("Amount")
  49. plt.title("Output & Absorb Contract Comparison")
  50. plt.legend()
  51. save_fig("bar_group")
  52. plt.close()
  53.  
  54. # 3. Finish‑rate bar
  55. plt.figure(figsize=(14, 7))
  56. bars = plt.bar(cities, finish_rate)
  57. plt.xticks(rotation=45, ha="right")
  58. plt.ylabel("Finish‑rate %")
  59. plt.title("City Task Completion Rate")
  60. for bar in bars:
  61. h = bar.get_height()
  62. plt.text(bar.get_x() + bar.get_width() / 2, h, f"{h}%", ha="center", va="bottom")
  63. save_fig("bar_rate")
  64. plt.close()
  65.  
  66. print("All three charts have been rendered successfully.")
Success #stdin #stdout #stderr 3.85s 90112KB
stdin
Standard input is empty
stdout
pie‑plot generated (bytes length: 91218)
bar_group‑plot generated (bytes length: 47488)
bar_rate‑plot generated (bytes length: 48376)
All three charts have been rendered successfully.
stderr
Fontconfig error: No writable cache directories