Skip to content

Commit d815050

Browse files
authored
Add files via upload
interactive plot of 2*2 matrix in cartesian plane
1 parent 0a9c9fd commit d815050

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "1a98487a",
7+
"metadata": {},
8+
"outputs": [
9+
{
10+
"data": {
11+
"application/vnd.jupyter.widget-view+json": {
12+
"model_id": "ef816049e28e4bd89f1c499af1a2b64c",
13+
"version_major": 2,
14+
"version_minor": 0
15+
},
16+
"text/plain": [
17+
"interactive(children=(FloatSlider(value=1.0, description='val_11', max=2.0, min=-2.0), FloatSlider(value=0.0, …"
18+
]
19+
},
20+
"metadata": {},
21+
"output_type": "display_data"
22+
},
23+
{
24+
"data": {
25+
"text/plain": [
26+
"<function __main__.plot_interactive_matrix(val_11=1.0, val_12=0.0, val_21=0.0, val_22=1.0)>"
27+
]
28+
},
29+
"execution_count": 2,
30+
"metadata": {},
31+
"output_type": "execute_result"
32+
}
33+
],
34+
"source": [
35+
"# This code sets up an interactive plot where you can adjust the values of a 2x2 transformation matrix using sliders, and see how it affects the grid of points in the plot.\n",
36+
"# The determinant of the matrix is also displayed in the title, allowing you to see how it changes with different transformations. \n",
37+
"import numpy as np\n",
38+
"import matplotlib.pyplot as plt\n",
39+
"# We import the specific tools from the library here\n",
40+
"from ipywidgets import interact, FloatSlider\n",
41+
"\n",
42+
"def plot_interactive_matrix(val_11=1.0, val_12=0.0, val_21=0.0, val_22=1.0):\n",
43+
" # Constructing the Transformation Matrix\n",
44+
" transformation_matrix = np.array([[val_11, val_12], # First row of the matrix\n",
45+
"\n",
46+
" [val_21, val_22]]) # Second row of the matrix\n",
47+
" \n",
48+
" # Calculate the Determinant (Math check!)\n",
49+
" det = np.linalg.det(transformation_matrix)\n",
50+
" \n",
51+
" # Create the grid\n",
52+
" x_range = np.linspace(-5, 5, 11) # We create a range of x values from -5 to 5, with 11 points (including endpoints)\n",
53+
" y_range = np.linspace(-5, 5, 11) # We create a range of y values from -5 to 5, with 11 points (including endpoints)\n",
54+
" grid_x, grid_y = np.meshgrid(x_range, y_range) # We create a grid of points from the x and y ranges, resulting in two 2D arrays: grid_x and grid_y\n",
55+
" \n",
56+
" # Flatten and transform\n",
57+
" original_points = np.vstack([grid_x.ravel(), grid_y.ravel()]) # We flatten the grid points into a 2xN array, where N is the total number of points in the grid\n",
58+
" transformed_points = transformation_matrix @ original_points # We apply the transformation by multiplying the transformation matrix with the original points, resulting in the transformed points\n",
59+
" \n",
60+
" # Reshape for plotting\n",
61+
" t_x = transformed_points[0, :].reshape(grid_x.shape) # We reshape the first row of the transformed points back into the original grid shape for x-coordinates\n",
62+
" t_y = transformed_points[1, :].reshape(grid_y.shape) # We reshape the second row of the transformed points back into the original grid shape for y-coordinates\n",
63+
" \n",
64+
" # Create the plot\n",
65+
" plt.figure(figsize=(7, 7)) # We set the figure size to 7x7 inches for better visibility\n",
66+
" \n",
67+
" # Plot transformed grid lines\n",
68+
" for i in range(len(x_range)): # We loop through each index of the x_range to plot the transformed grid lines\n",
69+
" plt.plot(t_x[i, :], t_y[i, :], color='teal', alpha=0.4) # We plot the transformed grid lines for the x-direction with a teal color and some transparency\n",
70+
" plt.plot(t_x[:, i], t_y[:, i], color='teal', alpha=0.4) # We plot the transformed grid lines for the y-direction with the same teal color and transparency\n",
71+
" \n",
72+
" plt.axhline(0, color='black', lw=1.5) # We draw a horizontal line at y=0 with a black color and a line width of 1.5\n",
73+
" plt.axvline(0, color='black', lw=1.5) # We draw a vertical line at x=0 with the same black color and line width\n",
74+
" plt.xlim(-10, 10) # We set the limits of the x-axis to be from -10 to 10 for better visibility of the transformed grid\n",
75+
" plt.ylim(-10, 10) # We set the limits of the y-axis to be from -10 to 10 for better visibility of the transformed grid\n",
76+
" plt.grid(True, linestyle=':', alpha=0.5) # We add a grid to the plot with dotted lines and some transparency\n",
77+
" plt.title(f\"Determinant: {det:.2f}\\nMatrix: {transformation_matrix.tolist()}\") # We set the title of the plot to display the determinant and the current transformation matrix\n",
78+
" plt.show() # We display the plot\n",
79+
"\n",
80+
"# Setting up the Sliders\n",
81+
"interact(plot_interactive_matrix, # We use the interact function to create interactive sliders for the matrix values\n",
82+
" val_11=FloatSlider(min=-2, max=2, step=0.1, value=1.0), # We create a slider for the value at position (1,1) of the matrix, with a range from -2 to 2, a step of 0.1, and a default value of 1.0\n",
83+
" val_12=FloatSlider(min=-2, max=2, step=0.1, value=0.0), # We create a slider for the value at position (1,2) of the matrix, with the same range and step, but a default value of 0.0\n",
84+
" val_21=FloatSlider(min=-2, max=2, step=0.1, value=0.0), # We create a slider for the value at position (2,1) of the matrix, with the same range and step, but a default value of 0.0\n",
85+
" val_22=FloatSlider(min=-2, max=2, step=0.1, value=1.0)) # We create a slider for the value at position (2,2) of the matrix, with the same range and step, and a default value of 1.0\n",
86+
"# When you run this code in a Jupyter Notebook, it will display an interactive plot where you can adjust the sliders to see how the transformation matrix affects the grid of points. The determinant will update in real-time, allowing you to explore different transformations and their effects on the grid.\n",
87+
"# Note: Make sure you have the ipywidgets library installed and enabled in your Jupyter environment to use the interactive sliders. You can install it using pip if you haven't already:\n",
88+
"# pip install ipywidgets\n",
89+
"# After installing, you may need to enable the extension for Jupyter Notebook:\n",
90+
"# jupyter nbextension enable --py widgetsnbextension\n",
91+
"\n"
92+
]
93+
}
94+
],
95+
"metadata": {
96+
"kernelspec": {
97+
"display_name": "Python 3",
98+
"language": "python",
99+
"name": "python3"
100+
},
101+
"language_info": {
102+
"codemirror_mode": {
103+
"name": "ipython",
104+
"version": 3
105+
},
106+
"file_extension": ".py",
107+
"mimetype": "text/x-python",
108+
"name": "python",
109+
"nbconvert_exporter": "python",
110+
"pygments_lexer": "ipython3",
111+
"version": "3.14.0"
112+
}
113+
},
114+
"nbformat": 4,
115+
"nbformat_minor": 5
116+
}

0 commit comments

Comments
 (0)