def array2d_to_qpolygonf(xdata, ydata): """ Utility function to convert two 1D-NumPy arrays representing curve data (X-axis, Y-axis data) into a single polyline (QtGui.PolygonF object). This feature is compatible with PyQt4, PyQt5 and PySide2 (requires QtPy). License/copyright: MIT License © Pierre Raybaut 2020. :param numpy.ndarray xdata: 1D-NumPy array (numpy.float64) :param numpy.ndarray ydata: 1D-NumPy array (numpy.float64) :return: Polyline :rtype: QtGui.QPolygonF """ dtype = np.float if not (xdata.size == ydata.size == xdata.shape[0] == ydata.shape[0] and xdata.dtype == ydata.dtype == dtype): raise ValueError( "Arguments must be 1D, float64 NumPy arrays with same size") size = xdata.size polyline = QPolygonF(size) if PYSIDE2: # PySide2 (obviously...) address = shiboken2.getCppPointer(polyline.data())[0] buffer = (ctypes.c_double * 2 * size).from_address(address) else: # PyQt4, PyQt5 buffer = polyline.data() buffer.setsize(2 * size * np.finfo(dtype).dtype.itemsize) memory = np.frombuffer(buffer, dtype) memory[:(size - 1) * 2 + 1:2] = xdata memory[1:(size - 1) * 2 + 2:2] = ydata return polyline
def array2d_to_qpolygonf(xdata, ydata): """ Utility function to convert two 1D-NumPy arrays representing curve data (X-axis, Y-axis data) into a single polyline (QtGui.PolygonF object). This feature is compatible with PyQt4, PyQt5 and PySide2 (requires QtPy). License/copyright: MIT License © Pierre Raybaut 2020-2021. :param numpy.ndarray xdata: 1D-NumPy array :param numpy.ndarray ydata: 1D-NumPy array :return: Polyline :rtype: QtGui.QPolygonF """ if not (xdata.size == ydata.size == xdata.shape[0] == ydata.shape[0]): raise ValueError("Arguments must be 1D NumPy arrays with same size") size = xdata.size if QT_API.startswith("pyside"): # PySide (obviously...) if QT_API == "pyside2": polyline = QPolygonF(size) else: polyline = QPolygonF() polyline.resize(size) address = shiboken.getCppPointer(polyline.data())[0] buffer = (ctypes.c_double * 2 * size).from_address(address) else: # PyQt4, PyQt5 if QT_API == "pyqt6": polyline = QPolygonF([QPointF(0, 0)] * size) else: polyline = QPolygonF(size) buffer = polyline.data() buffer.setsize( 16 * size) # 16 bytes per point: 8 bytes per X,Y value (float64) memory = np.frombuffer(buffer, np.float64) memory[:(size - 1) * 2 + 1:2] = np.array(xdata, dtype=np.float64, copy=False) memory[1:(size - 1) * 2 + 2:2] = np.array(ydata, dtype=np.float64, copy=False) return polyline