1

I am facing serious difficulties in placing a AnchoredSizeBar outside its given axes. From the AnchoredSizeBar reference, the loc attribute accepts only 'string' methods that are relative to the given axes used for the AnchoredSizeBar creation.

Therefore, if I wanted to set the AnchoredSizeBar position outside the given axes, the loc attribute wouldn't work. In fact, it would raise an error message.

Would someone knows a way to circumvent that problem?

If possible, I would like to create a AnchoredSizeBar, whose bar size is yet relative to a given axes in the figure, but the AnchoredSizeBar location can be placed anywhere inside the figure instance.

Here is a code snipped of what I would like:

import matplotlib.pyplot as plt


from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar


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

x_position = 0.15
y_position = 0.35

Figure_location = (x_position, y_position)    # figure xy locations relative to fig.transFigure.

axes_width_to_size_bar = 0.3

bar0 = AnchoredSizeBar(ax.transData, axes_width_to_size_bar, 'unfilled', loc=Figure_location, 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 Riskalla Leal

Philipe Riskalla Leal
  • 954
  • 1
  • 10
  • 28

1 Answers1

3

AnchoredSizeBar subclasses matplotlib.offsetbox.AnchoredOffsetbox. Additional arguments are hence passed on to AnchoredOffsetbox. This provides arguments bbox_to_anchor and bbox_transform. Those are the same as you have for legends, so for explanations see any legend example, e.g. How to put the legend out of the plot.

For example, to put the AnchoredSizeBar in the upper right corner of the figure,

import matplotlib.pyplot as plt

def draw_sizebar(ax):
    from mpl_toolkits.axes_grid1.anchored_artists import AnchoredSizeBar
    from matplotlib.transforms import Bbox
    asb = AnchoredSizeBar(ax.transData,
                          0.1,
                          "5 warp units",
                          loc='upper right',
                          pad=0.1, borderpad=0.5, sep=5,
                          frameon=False,
                          bbox_to_anchor=Bbox.from_bounds(0, 0, 1, 1),
                          bbox_transform=ax.figure.transFigure)
    ax.add_artist(asb)


fig, ax = plt.subplots()
draw_sizebar(ax)

plt.show()

enter image description here

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Dear ImportanceOfBeingErnest, would you know a way to add a background color to the AnchoredSizeBar? The figure is still messy. – Philipe Riskalla Leal May 30 '19 at 04:48
  • Dear ImportanceOfBeingErnest, one thing from your example that caused me some doubt was the label: "5 warp units". Does it mean that the given AnchoredSizeBar length represents a ruler of 5 units in respect to the Axes projection unit? If that were the case, from a visual analysis of the given figure, one can observe that it is not the case. In fact, the AnchoredSizeBar has dimension 0.1 (Axes units). Would you be so kind as to shed some light on to the subject? Sincerely, – Philipe Riskalla Leal Nov 07 '19 at 01:32
  • Yes, in my example 0.1 data units are 5 warp units. – ImportanceOfBeingErnest Nov 07 '19 at 01:36
  • Most interesting! Could you explain what is the transformation that has taken place to transform the 0.1 Axes units to 5 warp units? Furthermore, what does it mean warp units? I thank you for your time. Sincerely, – Philipe Riskalla Leal Nov 07 '19 at 03:15
  • 1
    There is no transformation happening. "warp units" is just made up by me. Replace that string by "0.1 data units" if you find that confusing. – ImportanceOfBeingErnest Nov 07 '19 at 12:08
  • Thank you ImportanceOfBeingernest. Now it is finally clear to me. Sincerely, – Philipe Riskalla Leal Nov 07 '19 at 13:36