MCPcopy Index your code
hub / github.com/matplotlib/matplotlib / _char_index_at

Method _char_index_at

lib/matplotlib/text.py:307–337  ·  view source on GitHub ↗

Calculate the index closest to the coordinate x in display space. The position of text[index] is assumed to be the sum of the widths of all preceding characters text[:index]. This works only on single line texts.

(self, x)

Source from the content-addressed store, hash-verified

305 return self._horizontalalignment
306
307 def _char_index_at(self, x):
308 """
309 Calculate the index closest to the coordinate x in display space.
310
311 The position of text[index] is assumed to be the sum of the widths
312 of all preceding characters text[:index].
313
314 This works only on single line texts.
315 """
316 if not self._text:
317 return 0
318
319 text = self._text
320
321 fontproperties = str(self._fontproperties)
322 if fontproperties not in Text._charsize_cache:
323 Text._charsize_cache[fontproperties] = dict()
324
325 charsize_cache = Text._charsize_cache[fontproperties]
326 for char in set(text):
327 if char not in charsize_cache:
328 self.set_text(char)
329 bb = self.get_window_extent()
330 charsize_cache[char] = bb.x1 - bb.x0
331
332 self.set_text(text)
333 bb = self.get_window_extent()
334
335 size_accum = np.cumsum([0] + [charsize_cache[x] for x in text])
336 std_x = x - bb.x0
337 return (np.abs(size_accum - std_x)).argmin()
338
339 def get_rotation(self):
340 """Return the text angle in degrees in the range [0, 360)."""

Callers 2

_clickMethod · 0.80
test_char_index_atFunction · 0.80

Calls 2

set_textMethod · 0.95
get_window_extentMethod · 0.95

Tested by 1

test_char_index_atFunction · 0.64