-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcoords.py
More file actions
93 lines (70 loc) · 2.24 KB
/
coords.py
File metadata and controls
93 lines (70 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
"""
Plot coordinates
----------------
Overview
^^^^^^^^
The :mod:`plotpy.coords` module provides functions to convert coordinates
between canvas and axes coordinates systems.
The following functions are available:
* :py:func:`.canvas_to_axes`
* :py:func:`.axes_to_canvas`
* :py:func:`.pixelround`
Reference
^^^^^^^^^
.. autofunction:: canvas_to_axes
.. autofunction:: axes_to_canvas
.. autofunction:: pixelround
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
if TYPE_CHECKING:
from qtpy.QtCore import QPointF
from qwt import QwtPlot, QwtPlotItem
def canvas_to_axes(item: QwtPlotItem, pos: QPointF) -> tuple[float, float] | None:
"""Convert position from canvas coordinates system to axes coordinates
Args:
item: Plot item
pos: Position in canvas coordinates system
Returns:
Position in axes coordinates system or None if item is None
"""
if item is None:
return None
plot: QwtPlot = item.plot()
ax, ay = item.xAxis(), item.yAxis()
return plot.invTransform(ax, pos.x()), plot.invTransform(ay, pos.y())
def axes_to_canvas(item: QwtPlotItem, x: float, y: float) -> tuple[float, float] | None:
"""Convert (x,y) from axes coordinates to canvas coordinates system
Args:
item: Plot item
x: X position in axes coordinates system
y: Y position in axes coordinates system
Returns:
Position in canvas coordinates system or None if item is None
"""
if item is None:
return None
plot: QwtPlot = item.plot()
ax, ay = item.xAxis(), item.yAxis()
return plot.transform(ax, x), plot.transform(ay, y)
def pixelround(x: float, corner: str | None = None) -> int:
"""Get pixel index from pixel coordinate
Args:
x: Pixel coordinate
corner: None (not a corner), 'TL' (top-left corner),
'BR' (bottom-right corner)
Returns:
int: Pixel index
"""
assert corner is None or corner in ("TL", "BR")
if corner is None:
return np.floor(x)
elif corner == "BR":
return np.ceil(x)
elif corner == "TL":
return np.floor(x)