Return the property dictionary for a matplotlib.Axis instance
(axis)
| 198 | |
| 199 | |
| 200 | def get_axis_properties(axis): |
| 201 | """Return the property dictionary for a matplotlib.Axis instance""" |
| 202 | props = {} |
| 203 | label1On = axis.get_tick_params().get("label1On", True) |
| 204 | |
| 205 | if isinstance(axis, matplotlib.axis.XAxis): |
| 206 | if label1On: |
| 207 | props["position"] = "bottom" |
| 208 | else: |
| 209 | props["position"] = "top" |
| 210 | elif isinstance(axis, matplotlib.axis.YAxis): |
| 211 | if label1On: |
| 212 | props["position"] = "left" |
| 213 | else: |
| 214 | props["position"] = "right" |
| 215 | else: |
| 216 | raise ValueError("{0} should be an Axis instance".format(axis)) |
| 217 | |
| 218 | # Use tick values if appropriate |
| 219 | locator = axis.get_major_locator() |
| 220 | props["nticks"] = len(locator()) |
| 221 | if isinstance(locator, ticker.FixedLocator): |
| 222 | props["tickvalues"] = list(locator()) |
| 223 | else: |
| 224 | props["tickvalues"] = None |
| 225 | |
| 226 | # Find tick formats |
| 227 | formatter = axis.get_major_formatter() |
| 228 | if isinstance(formatter, ticker.NullFormatter): |
| 229 | props["tickformat"] = "" |
| 230 | elif isinstance(formatter, ticker.FixedFormatter): |
| 231 | props["tickformat"] = list(formatter.seq) |
| 232 | elif isinstance(formatter, ticker.FuncFormatter): |
| 233 | props["tickformat"] = list(formatter.func.args[0].values()) |
| 234 | elif not any(label.get_visible() for label in axis.get_ticklabels()): |
| 235 | props["tickformat"] = "" |
| 236 | else: |
| 237 | props["tickformat"] = None |
| 238 | |
| 239 | # Get axis scale |
| 240 | props["scale"] = axis.get_scale() |
| 241 | |
| 242 | # Get major tick label size (assumes that's all we really care about!) |
| 243 | labels = axis.get_ticklabels() |
| 244 | if labels: |
| 245 | props["fontsize"] = labels[0].get_fontsize() |
| 246 | else: |
| 247 | props["fontsize"] = None |
| 248 | |
| 249 | # Get associated grid |
| 250 | props["grid"] = get_grid_style(axis) |
| 251 | |
| 252 | # get axis visibility |
| 253 | props["visible"] = axis.get_visible() |
| 254 | |
| 255 | return props |
| 256 | |
| 257 |
no test coverage detected