String representation of the data at every tick.
| 137 | |
| 138 | |
| 139 | class StrCategoryFormatter(ticker.Formatter): |
| 140 | """String representation of the data at every tick.""" |
| 141 | def __init__(self, units_mapping): |
| 142 | """ |
| 143 | Parameters |
| 144 | ---------- |
| 145 | units_mapping : dict |
| 146 | Mapping of category names (str) to indices (int). |
| 147 | """ |
| 148 | self._units = units_mapping |
| 149 | |
| 150 | def __call__(self, x, pos=None): |
| 151 | # docstring inherited |
| 152 | return self.format_ticks([x])[0] |
| 153 | |
| 154 | def format_ticks(self, values): |
| 155 | # docstring inherited |
| 156 | r_mapping = {v: self._text(k) for k, v in self._units.items()} |
| 157 | return [r_mapping.get(round(val), '') for val in values] |
| 158 | |
| 159 | @staticmethod |
| 160 | def _text(value): |
| 161 | """Convert text values into utf-8 or ascii strings.""" |
| 162 | if isinstance(value, bytes): |
| 163 | value = value.decode(encoding='utf-8') |
| 164 | elif not isinstance(value, str): |
| 165 | value = str(value) |
| 166 | return value |
| 167 | |
| 168 | |
| 169 | class UnitData: |
no outgoing calls
no test coverage detected
searching dependent graphs…