Up front: totally open to other methods that might accomplish the same thing – I thought this would be the most straightforward way to accomplish this.
I’m simply trying to rotate HPackers/VPackers/other types of OffsetBoxes by an arbitrary amount. I was hoping to use an AuxTransformBoxes
with a rotation transformation applied, but it doesn’t seem to work.
Start by importing the following modules:
import matplotlib
import matplotlib.artist
import matplotlib.lines
import matplotlib.pyplot
import matplotlib.patches
import matplotlib.patheffects
import matplotlib.offsetbox
import matplotlib.transforms
This is a code snippet that stacks a text area and a line artist vertically using a VPacker:
# Creating a 5x5 plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
# Creating a line
line = matplotlib.lines.Line2D([0,2],[2,2])
# This box will ONLY scale
atb = matplotlib.offsetbox.AuxTransformBox(fig.dpi_scale_trans)
# Adding the line to the box
atb.add_artist(line)
# Creating a TextArea
txt = matplotlib.offsetbox.TextArea("Example")
# This will pack the atb and txt artists together vertically
vpack = matplotlib.offsetbox.VPacker(children=[txt,atb], align="center")
# AOB will contain the final artist, and is used for centering
aob = matplotlib.offsetbox.AnchoredOffsetbox(loc="center", child=vpack, frameon=False, pad=0, borderpad=0)
# Adding the AOB to the plot
ax.add_artist(aob)
However, the same code does not work when everything is placed inside of an AuxTransformBox
that is rotated (the frame of the AnchoredOffsetbox
is kept on here):
# Creating a 5x5 plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=150)
# Creating a line
line = matplotlib.lines.Line2D([0,2],[2,2])
# This box will ONLY scale
atb_scale = matplotlib.offsetbox.AuxTransformBox(fig.dpi_scale_trans)
# Adding the line to the box
atb_scale.add_artist(line)
# Creating a TextArea
txt = matplotlib.offsetbox.TextArea("Example")
# This will pack the atb and txt artists together vertically
vpack = matplotlib.offsetbox.VPacker(children=[txt,atb_scale], align="center")
# This box will ONLY rotate
atb_rotate = matplotlib.offsetbox.AuxTransformBox(matplotlib.transforms.Affine2D().rotate_deg(45))
# Adding the vpacker
atb_rotate.add_artist(vpack)
# AOB will contain the final artist, and is used for centering
aob = matplotlib.offsetbox.AnchoredOffsetbox(loc="center", child=atb_rotate, frameon=True, pad=0, borderpad=0)
# Adding the AOB to the plot
ax.add_artist(aob)
The composite artist is found at the bottom of the screen (note the word Example), not respecting where the AnchoredOffsetBox
tries to place it (the black box in the middle of the screen). Why would this be the case?