mathematik1/mengen/kartesiches_produkt.py

100 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#%%
import numpy as np
import matplotlib.pyplot as plt
def setup_axes(ax, xlim, ylim, xticks, yticks):
"""Stellt die Achsen als klassisches x-y-Koordinatensystem mit Ursprung dar."""
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.plot(1, 0, ">k", transform=ax.get_yaxis_transform(), clip_on=False)
ax.plot(0, 1, "^k", transform=ax.get_xaxis_transform(), clip_on=False)
ax.set_xlabel('x', loc='right', fontsize=13)
ax.set_ylabel('y', loc='top', rotation=0, fontsize=13)
ax.set_xlim(*xlim)
ax.set_ylim(*ylim)
ax.set_xticks(xticks)
ax.set_yticks(yticks)
ax.grid(True, linestyle='--', alpha=0.4)
ax.axis("image")
# --- 1. {1, 2} × {1, 2} ---
fig, ax = plt.subplots(figsize=(6, 6))
ax.scatter([1, 1, 2, 2], [1, 2, 1, 2], s=120, color='steelblue', zorder=3)
setup_axes(ax, (-3, 6), (-3, 6), [1, 2, 3, 4, 5], [1, 2, 3, 4, 5])
ax.legend([r'$\{1,\,2\} \times \{1,\,2\}$'], loc='upper center',
bbox_to_anchor=(0.5, -0.12), fontsize=12, frameon=True)
ax.set_title("1.")
plt.tight_layout()
plt.savefig("1_kartesiche_produkte.png", dpi=150, bbox_inches='tight')
plt.close()
# --- 2. {1, 2} × {2, 3, 4} ---
fig, ax = plt.subplots(figsize=(6, 6))
ax.scatter([1, 1, 1, 2, 2, 2], [2, 3, 4, 2, 3, 4], s=120, color='darkorange', zorder=3)
setup_axes(ax, (-3, 7), (-3, 7), [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6])
ax.legend([r'$\{1,\,2\} \times \{2,\,3,\,4\}$'], loc='upper center',
bbox_to_anchor=(0.5, -0.12), fontsize=12, frameon=True)
ax.set_title("2.")
plt.tight_layout()
plt.savefig("2_kartesiche_produkte.png", dpi=150, bbox_inches='tight')
plt.close()
# --- 3. {2, 3, 4} × {1, 2} ---
fig, ax = plt.subplots(figsize=(6, 6))
ax.scatter([2, 2, 3, 3, 4, 4], [1, 2, 1, 2, 1, 2], s=120, color='seagreen', zorder=3)
setup_axes(ax, (-3, 7), (-3, 6), [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5])
ax.legend([r'$\{2,\,3,\,4\} \times \{1,\,2\}$'], loc='upper center',
bbox_to_anchor=(0.5, -0.12), fontsize=12, frameon=True)
ax.set_title("3.")
plt.tight_layout()
plt.savefig("3_kartesiche_produkte.png", dpi=150, bbox_inches='tight')
plt.close()
# --- 4. [0, 1] × {1, 2} ---
fig, ax = plt.subplots(figsize=(6, 6))
x_line = np.linspace(0, 1, 100)
ax.plot(x_line, np.ones_like(x_line), color='crimson', linewidth=3)
ax.plot(x_line, 2 * np.ones_like(x_line), color='crimson', linewidth=3)
ax.scatter([0, 1, 0, 1], [1, 1, 2, 2], s=60, color='crimson', zorder=3)
setup_axes(ax, (-3, 5), (-3, 5), [0, 1, 2, 3, 4], [1, 2, 3, 4])
ax.legend([r'$[0,\,1] \times \{1,\,2\}$'], loc='upper center',
bbox_to_anchor=(0.5, -0.12), fontsize=12, frameon=True)
ax.set_title("4.")
plt.tight_layout()
plt.savefig("4_kartesiche_produkte.png", dpi=150, bbox_inches='tight')
plt.close()
# --- 5. {1, 2} × [0, 1] ---
fig, ax = plt.subplots(figsize=(6, 6))
y_line = np.linspace(0, 1, 100)
ax.plot(np.ones_like(y_line), y_line, color='mediumpurple', linewidth=3)
ax.plot(2 * np.ones_like(y_line), y_line, color='mediumpurple', linewidth=3)
ax.scatter([1, 1, 2, 2], [0, 1, 0, 1], s=60, color='mediumpurple', zorder=3)
setup_axes(ax, (-3, 5), (-3, 5), [1, 2, 3, 4], [0, 1, 2, 3, 4])
ax.legend([r'$\{1,\,2\} \times [0,\,1]$'], loc='upper center',
bbox_to_anchor=(0.5, -0.12), fontsize=12, frameon=True)
ax.set_title("5.")
plt.tight_layout()
plt.savefig("5_kartesiche_produkte.png", dpi=150, bbox_inches='tight')
plt.close()
# --- 6. [0, 1] × [1, 2] ---
fig, ax = plt.subplots(figsize=(6, 6))
rect = plt.Rectangle((0, 1), 1, 1, linewidth=2, edgecolor='teal', facecolor='teal', alpha=0.3)
ax.add_patch(rect)
ax.plot([0, 1, 1, 0, 0], [1, 1, 2, 2, 1], color='teal', linewidth=2)
setup_axes(ax, (-3, 5), (-3, 5), [0, 1, 2, 3, 4], [1, 2, 3, 4])
ax.legend([r'$[0,\,1] \times [1,\,2]$'], loc='upper center',
bbox_to_anchor=(0.5, -0.12), fontsize=12, frameon=True)
ax.set_title("6.")
plt.tight_layout()
plt.savefig("6_kartesiche_produkte.png", dpi=150, bbox_inches='tight')
plt.close()
# %%