0

I am facing serious difficulties in retrieving the bounding box of a mpl_toolkits.axes_grid1.anchored_artists "AnchoredSizeBar" object.

So far, all I found was how to insert (create) an AnchoredSizeBar into my figure. But I can't retrieve its position. A relative position in respect to its given axes would also be welcome.

Ultimately, I would like to make a AnchoredSizeBar as Behnam asks in1. But in order to do so, I would need to create a series of AnchoredSizeBars all contiguous one to another in the figure. In order to do so, I would need a function to retrieve their relative positions and apply those into the creation of the other AnchoredSizeBars, creating finally a full scalebar.

This subject is related to other links presented below:

1) How to insert scale bar in a map in matplotlib

2) Add fill_bar argument to AnchoredSizeBar

Here is a snippet code of a desired API for AnchoredSizeBar object extent retrieval.

import matplotlib.pyplot as plt


from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar


fig, ax = plt.subplots(figsize=(3, 3))

bar0 = AnchoredSizeBar(ax.transData, 0.3, 'unfilled', loc=3, frameon=False,
                       size_vertical=0.05, fill_bar=False)
ax.add_artist(bar0)


bar0_extent = bar0.get_extent()

fig.show()

I thank you for your time, sincerely yours,

Philipe Leal

Philipe Riskalla Leal
  • 954
  • 1
  • 10
  • 28
  • That will become utterly complicated. I would opt for creating one single `AnchoredOffsetbox` with a `DrawingArea` or an `AuxTransformBox`; then add as many rectangles as you need to it to give it a (black and white kind of?) scale. You may look at [this answer](https://stackoverflow.com/a/43343934/4124317) for a start. – ImportanceOfBeingErnest May 27 '19 at 21:45

1 Answers1

0

To get the extent you can pass in ax.figure.canvas.renderer where ax is the axes to which the object has been added to:

bar0_extent = bar0.get_extent(ax.figure.canvas.renderer)

But I second @ImportanceOfBeingErnest's suggestion for your particular use case. The doc-string for AnchoredSizeBar suggests that methods:

Docstring: An offset box placed according to the legend location loc. AnchoredOffsetbox has a single child. When multiple children is needed, use other OffsetBox class to enclose them. By default, the offset box is anchored against its parent axes. You may explicitly specify the bbox_to_anchor.

Hope this helps.

Prasanth
  • 412
  • 2
  • 4
  • Dear Prasanth, Thank you for your reply. It seems that ImportanceOfBeingErnest's alternative is OK. Nevertheless, I noticed a small problem to the given solution. The scalebar ends up always over the plotted axes. Therefore, if one has a Map of any given place (i.e.: a continent as Brazil), inevitably the scalebar will cover some of the Map. If possible, I would like to place the scalebar outside of the given axes. If possible, I would like to be able to set the scalebar position based on the figure.transFigure (figure transformation). Any ideas? – Philipe Riskalla Leal May 29 '19 at 02:47
  • You could try passing `bbox_transform=transFigure` and `bbox_to_anchor=(x,y)` to `AnchoredOffsetbox`. With these options, I think, `loc` will be taken as the point of the box that will be placed at `(x,y)`. – Prasanth May 29 '19 at 03:03