Я знаю, что для обычного художника я могу сделать следующее:
Код: Выделить всё
# Create the plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=300)
# Create a generic triangle artist at the midpoint of the axis
triangle = ax.add_artist(matplotlib.patches.RegularPolygon((0.5,0.5), 3, radius=0.2))
# Retrieve the bbox of the triangle in data coordinates
ax.transData.inverted().transform(triangle.get_window_extent())
Код: Выделить всё
# Create the plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=300)
# Create a generic triangle artist
triangle = matplotlib.patches.RegularPolygon((0.5,0.5), 3, radius=0.2)
# Create an offsetbox in data coordinates
offsetbox = matplotlib.offsetbox.AuxTransformBox(ax.transData)
# Place the triangle in the offsetbox
offsetbox.add_artist(triangle)
# Place the offsetbox in the AnchoredOffsetBox
container = matplotlib.offsetbox.AnchoredOffsetbox(loc="center", pad=0, child=offsetbox)
# Add the AnchoredOffsetBox to the figure
ax.add_artist(container)
# Retrieve the bbox of the AnchoredOffsetBox in data coordinates
ax.transData.inverted().transform(container.get_window_extent())
(Примечание: я не уверен, почему треугольник, отображаемый с помощью приведенного выше кода, немного «ниже», чем в первом блоке кода, но пока я игнорирую это)
p>
Однако мне нужно получить положение AnchoredOffsetBox до его визуализации, чтобы при необходимости можно было изменить его положение. Я пытался сделать это так:
Код: Выделить всё
### SAME AS ABOVE ###
# Create the plot
fig, ax = matplotlib.pyplot.subplots(1,1, figsize=(5,5), dpi=300)
# Create a generic triangle artist
triangle = matplotlib.patches.RegularPolygon((0.5,0.5), 3, radius=0.2)
# Create an offsetbox in data coordinates
offsetbox = matplotlib.offsetbox.AuxTransformBox(ax.transData)
# Place the triangle in the offsetbox
offsetbox.add_artist(triangle)
# Place the offsetbox in the AnchoredOffsetBox
container = matplotlib.offsetbox.AnchoredOffsetbox(loc="center", pad=0, child=offsetbox)
### NEW CODE ###
# Set the figure for the container
container.set_figure(fig)
# Retrieve the renderer for the figure
renderer = fig._get_renderer()
# Retrieve the bbox of the AnchoredOffsetBox in data coordinates
ax.transData.inverted().transform(container.get_bbox(renderer))
Если это бесполезное занятие, было бы здорово узнать. Я открыт для любых других предложений относительно того, как я могу совершить аналогичный подвиг: по какой-то причине я решил, что это слишком сложно, например. обновить вращение художника после того, как он был добавлен на ось, но я так долго работал над этой проблемой, что не могу вспомнить почему.
Подробнее здесь: https://stackoverflow.com/questions/785 ... -rendering