Skip to content

Commit b9c73fd

Browse files
ppinardzezhong-zhang
authored andcommitted
Add info after scale bar has been drawn (ppinard#67)
1 parent 7b65a2f commit b9c73fd

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,35 @@ ax.add_artist(scalebar)
108108
fig.savefig("original_resolution.png", dpi=dpi)
109109
```
110110

111+
### Information about the drawn scale bar
112+
113+
After the scale bar has been drawn, the `info` property returns the following dataclass.
114+
115+
```python
116+
@dataclasses.dataclass
117+
class ScaleBarInfo:
118+
length_px: int
119+
value: float
120+
units: str
121+
scale_text: str
122+
window_extent: matplotlib.transforms.Bbox
123+
```
124+
125+
Note that the `info` property returns a `ValueError` exception if the scale bar has not been drawn.
126+
127+
```python
128+
fig, ax = plt.subplots()
129+
130+
scalebar = ScaleBar(0.08, "cm", length_fraction=0.25)
131+
ax.add_artist(scalebar)
132+
133+
print(scalebar.info) # raises a ValueError exception
134+
135+
fig.canvas.draw()
136+
137+
print(scalebar.info) # works
138+
```
139+
111140
## ScaleBar arguments
112141

113142
Here are arguments of the **ScaleBar** class constructor and examples how to use them.

matplotlib_scalebar/scalebar.py

+23
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
# Standard library modules.
4444
import bisect
4545
import warnings
46+
import dataclasses
4647

4748
# Third party modules.
4849
import matplotlib
@@ -151,6 +152,15 @@ def _validate_legend_loc(loc):
151152
}
152153

153154

155+
@dataclasses.dataclass
156+
class ScaleBarInfo:
157+
length_px: int
158+
value: float
159+
units: str
160+
scale_text: str
161+
window_extent: matplotlib.transforms.Bbox
162+
163+
154164
class ScaleBar(Artist):
155165
zorder = 6
156166

@@ -379,6 +389,7 @@ def __init__(
379389
self.rotation = rotation
380390
self.bbox_to_anchor = bbox_to_anchor
381391
self.bbox_transform = bbox_transform
392+
self._info = None
382393

383394
def _calculate_best_length(self, length_px):
384395
dx = self.dx
@@ -404,6 +415,8 @@ def _calculate_exact_length(self, value, units):
404415
return newvalue / self.dx
405416

406417
def draw(self, renderer, *args, **kwargs):
418+
self._info = None
419+
407420
if not self.get_visible():
408421
return
409422
if self.dx == 0:
@@ -566,6 +579,10 @@ def _get_value(attr, default):
566579
box.patch.set_alpha(box_alpha)
567580
box.draw(renderer)
568581

582+
self._info = ScaleBarInfo(
583+
length_px, value, units, scale_text, box.get_window_extent(renderer)
584+
)
585+
569586
def get_dx(self):
570587
return self._dx
571588

@@ -864,3 +881,9 @@ def set_bbox_transform(self, bbox_transform):
864881
self._bbox_transform = bbox_transform
865882

866883
bbox_transform = property(get_bbox_transform, set_bbox_transform)
884+
885+
@property
886+
def info(self):
887+
if self._info is None:
888+
raise ValueError("Scale bar has not been drawn. Call figure.canvas.draw()")
889+
return self._info

0 commit comments

Comments
 (0)