Skip to content

Commit e146596

Browse files
committed
Transformation matrix app created
- Can display the app with pyqt5 - It can show the plot of a 3d grid with colors - It can transform the space with a given transformation matrix TODO: - Default colors and alpha when the PlotWidget class is created (maybe) - Create a way to replot the grid while the app is running
0 parents  commit e146596

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

Plotting.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import PyQt5.QtWidgets as QtWidgets
2+
from matplotlib.figure import Figure
3+
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
4+
5+
"""
6+
TODO:
7+
- I should add the option of default colors and alpha but i couldn't, it's something to fix
8+
"""
9+
class PlotWidget(QtWidgets.QWidget):
10+
"""
11+
12+
Widget for PyQt5 with the plot
13+
14+
...
15+
16+
Parameters
17+
----------
18+
x: list
19+
List with the x values
20+
y: list
21+
List with the y values
22+
z: list
23+
List with the z values
24+
colors: tuple
25+
Tuple with the RGB colors
26+
alpha: float
27+
The transparency of the plot
28+
29+
...
30+
31+
Attributes
32+
----------
33+
fig: matplotlib.figure
34+
35+
axes: matplotlib.axes
36+
37+
canvas: matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg
38+
"""
39+
def __init__(self, x: list, y: list, z: list, colors: tuple, alpha: float):
40+
super().__init__()
41+
self.fig = Figure()
42+
self.canvas = FigureCanvas(self.fig)
43+
self.axes = self.fig.add_subplot(111,projection='3d')
44+
45+
layout = QtWidgets.QVBoxLayout(self)
46+
layout.addWidget(self.canvas)
47+
48+
self.axes.scatter(x, y, z, c = colors, alpha = alpha)

main.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""
2+
Visualization of the tranformation matrix
3+
Author: Manuel Garcia
4+
Date: January 27 2021
5+
"""
6+
from sympy.matrices import Matrix #It's easier work with matrices in sympy than numpy
7+
from sympy import sin,cos,pi #Trigonometric functions
8+
import matplotlib.pyplot as plt #Plotting
9+
from numpy import linspace,column_stack,array #For create the linspace for the plotting
10+
11+
import PyQt5.QtWidgets as QtWidgets #Python GUI
12+
13+
from Plotting import PlotWidget #The plot object
14+
15+
"""Transformation matrix"""
16+
transformationMatrix = Matrix([[1, 0, 0],
17+
[0, 0.5, 0],
18+
[0, 0, 1]])
19+
20+
def transformPoint(x,y,z):
21+
"""Return the vector transformed
22+
23+
"""
24+
return transformationMatrix*Matrix([x,y,z])
25+
26+
def colorizer(x, y,z):
27+
"""Color the points in the space by their position
28+
29+
"""
30+
r = min(1, 1-y/3)
31+
g = min(1, 1+y/3)
32+
b = 1/4 + x/16
33+
return (r, g, b)
34+
35+
36+
def get_grid(min,max):
37+
"""Return a grid in x, y and z
38+
39+
...
40+
41+
Parameters
42+
----------
43+
min
44+
minimum value in the x,y and z axes
45+
max
46+
maximum value in the x,y and z axes
47+
"""
48+
xvals = linspace(min, max, abs(min-max)+1)
49+
yvals = linspace(min, max, abs(min-max)+1)
50+
zvals = linspace(min, max, abs(min-max)+1)
51+
xyzgrid = column_stack([[x, y,z] for x in xvals for y in yvals for z in zvals])
52+
return xyzgrid
53+
54+
if __name__ == "__main__":
55+
56+
originalGrid = get_grid(-2,2)
57+
transformedGrid = transformPoint(originalGrid[0],originalGrid[1],originalGrid[2])
58+
xyzgrid = array([transformedGrid[0,:][0,:], #
59+
transformedGrid[1,:][0,:], # Save the transformed grid as a numpy array
60+
transformedGrid[2,:][0,:]]) #
61+
62+
colors = list(map(colorizer, originalGrid[0], originalGrid[1], originalGrid[2])) # Asign the colors to the points
63+
64+
app = QtWidgets.QApplication([])
65+
plot = PlotWidget(xyzgrid[0], xyzgrid[1], xyzgrid[2], colors = colors, alpha = 0.7)
66+
67+
plot.axes.set_xlim([-2, 2]) #
68+
plot.axes.set_ylim([-2, 2]) # Set the limits of the plot
69+
plot.axes.set_zlim([-2, 2]) #
70+
plot.axes.axis('on') # Show the axis
71+
plot.axes.set_xlabel('x') #
72+
plot.axes.set_ylabel('y') # Axes names
73+
plot.axes.set_zlabel('z') #
74+
plot.axes.grid(False) # Don't show the grid
75+
plot.axes.view_init(10,5) # View init at 15 and 5 degrees
76+
77+
plot.show()
78+
app.exec()

0 commit comments

Comments
 (0)