plotpy.tools.curve — PlotPy 2.7 Manual (original) (raw)
-- coding: utf-8 --
"""Curve tools"""
from future import annotations
import weakref from typing import TYPE_CHECKING, Any, Callable
import numpy as np import scipy.integrate as spt from guidata.dataset import ChoiceItem, DataSet, FloatItem, IntItem from guidata.qthelpers import execenv from guidata.widgets.arrayeditor import ArrayEditor from qtpy import QtCore as QC from qtpy import QtGui as QG from qtpy import QtWidgets as QW
from plotpy.config import _ from plotpy.constants import SHAPE_Z_OFFSET from plotpy.coords import axes_to_canvas, canvas_to_axes from plotpy.events import ( KeyEventMatch, QtDragHandler, StandardKeyMatch, StatefulEventFilter, setup_standard_tool_filter, ) from plotpy.interfaces import ICurveItemType from plotpy.items import Marker, XRangeSelection from plotpy.items.curve.base import CurveItem from plotpy.tools.base import DefaultToolbarID, InteractiveTool, ToggleTool from plotpy.tools.cursor import BaseCursorTool
if TYPE_CHECKING: from qwt import QwtText
from plotpy.items.label import DataInfoLabel
from plotpy.plot.base import BasePlot
from plotpy.plot.manager import PlotManager
class InsertionDataSet(DataSet): """Insertion parameters"""
__index = IntItem(_("Insertion index"), min=0)
index = __index
value = FloatItem(_("New value"))
index_offset = ChoiceItem(
_("Location"), choices=[_("Before"), _("After")], default=0
)
@classmethod
def set_max_index(cls, max_index: int):
"""Sets the maximum index value for the index field
Args:
max_index: max index value
"""
cls.index.set_prop("data", max=max_index)
def export_curve_data(item: CurveItem) -> None: """Export curve item data to text file
Args:
item: curve item
"""
item_data = item.get_data()
if len(item_data) > 2:
x, y, dx, dy = item_data
array_list = [x, y]
if dx is not None:
array_list.append(dx)
if dy is not None:
array_list.append(dy)
data = np.array(array_list).T
else:
x, y = item_data
data = np.array([x, y]).T
plot = item.plot()
title = _("Export")
if item.param.label:
title += f" ({item.param.label})"
fname, _f = QW.QFileDialog.getSaveFileName(
plot, title, "", _("Text file") + " (*.txt)"
)
if fname:
try:
np.savetxt(str(fname), data, delimiter=",")
except RuntimeError as error:
QW.QMessageBox.critical(
plot,
_("Export"),
_("Unable to export item data.")
+ "<br><br>"
+ _("Error message:")
+ "<br>"
+ str(error),
)
def edit_curve_data(item: CurveItem) -> None: """Edit curve item data in array editor
Args:
item: curve item
"""
item_data = item.get_data()
if len(item_data) > 2:
x, y, dx, dy = item_data
array_list = [x, y]
if dx is not None:
array_list.append(dx)
if dy is not None:
array_list.append(dy)
data = np.array(array_list).T
else:
x, y = item_data
data = np.array([x, y]).T
dialog = ArrayEditor(item.plot())
dialog.setup_and_check(data)
if dialog.exec_():
if data.shape[1] > 2:
if data.shape[1] == 3:
x, y, tmp = data.T
if dx is not None:
dx = tmp
else:
dy = tmp
else:
x, y, dx, dy = data.T
item.set_data(x, y, dx, dy)
else:
x, y = data.T
item.set_data(x, y)