import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
def create_function_plot(ax, title, x, y, connections):
ax.set_xlim(0, 5)
ax.set_ylim(0, 5)
ax.set_aspect('equal')
ax.set_title(title)
ax.set_xticks([])
ax.set_yticks([])
ax.text(2.5, -0.5, "X", ha='center')
ax.text(-0.5, 2.5, "Y", va='center', rotation=90)
for i, xi in enumerate(x):
ax.plot(xi, 0, 'bo')
ax.text(xi, -0.3, f'x{i+1}', ha='center')
for i, yi in enumerate(y):
ax.plot(0, yi, 'ro')
ax.text(-0.3, yi, f'y{i+1}', va='center')
for xi, yi in connections:
ax.annotate("", xy=(0, yi), xytext=(xi, 0),
arrowprops=dict(arrowstyle="->", color='g'))
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5))
# 单射(Injection)
x_inj = [1, 2, 3, 4]
y_inj = [1, 2, 3, 4, 5]
connections_inj = [(1, 1), (2, 3), (3, 4), (4, 2)]
create_function_plot(ax1, "单射 (Injection)", x_inj, y_inj, connections_inj)
# 满射(Surjection)
x_surj = [1, 2, 3, 4, 5]
y_surj = [1, 2, 3, 4]
connections_surj = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 2)]
create_function_plot(ax2, "满射 (Surjection)", x_surj, y_surj, connections_surj)
# 双射(Bijection)
x_bij = [1, 2, 3, 4]
y_bij = [1, 2, 3, 4]
connections_bij = [(1, 3), (2, 1), (3, 4), (4, 2)]
create_function_plot(ax3, "双射 (Bijection)", x_bij, y_bij, connections_bij)
plt.tight_layout()
plt.show()