Updated 2022-05-22
Viewed 17 times
0

I'm trying to make a 3D plot with dials that can rotate points around axes, here is a basic example with rotation around the z axis

``````
import numpy as np
import sys
from PyQt5 import QtCore, QtWidgets
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib
matplotlib.interactive(True)

class MainWindow(QtWidgets.QMainWindow):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.widget = QtWidgets.QWidget()
grid = QtWidgets.QGridLayout()
self.fig = Figure()
self.canvas = FigureCanvas(self.fig)
self.x, self.y, self.z = 1.0,1.0,1.0
plot_refs = self.axes.plot(self.x, self.y, self.z, 'ro')
self._plot_ref = plot_refs[0]
self.axes.set_xlabel('Horizontal')
self.axes.set_ylabel('Vertical')
self.axes.set_zlabel('Depth')
self.zdeg = QtWidgets.QDial()
self.zdeg.valueChanged.connect(self.rotz)
self.zdial_val = self.zdeg.value()
self.widget.setLayout(grid)
self.setCentralWidget(self.widget)

def rotz(self):
if self.zdial_val < self.zdeg.value():
self.x = self.x*np.cos(np.pi/50) + self.y*np.sin(np.pi/50)
self.y = -self.x*np.sin(np.pi/50) + self.y*np.cos(np.pi/50)
else:
self.x = self.x*np.cos(np.pi/50) - self.y*np.sin(np.pi/50)
self.y = self.x*np.sin(np.pi/50) + self.y*np.cos(np.pi/50)
self.zdial_val = self.zdeg.value()
self.plot()

def plot(self):

self._plot_ref.set_xdata(self.x)
self._plot_ref.set_ydata(self.y)
self._plot_ref.set_3d_properties(self.z)

self.fig.canvas.draw()

def closeEvent(self, event):
super(MainWindow, self).closeEvent(event)
app.quit()

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())

``````

But when I rotate the point, it starts decreasing to the center. I'm guessing from rounding errors. Is there a way to correct for this?

ðŸ”´ No definitive solution yet