Я хотел бы знать, как правильно использовать класс matplotlib.offsetbox.PackerBase.
Я пытаюсь вставить в него несколько экземпляров прямоугольников, чтобы позже я мог добавить их в один AnchoredOffsetbox в my matplotlib Axes.
Алгоритм создан по этой ссылке .
, поскольку исходный алгоритм не вставляет масштабную линейку в блок (т. е. matplotlib.offsetbox.AnchoredOffsetbox ), Я пытался адаптировать код с этим намерением.
Вот неработающий пример моей попытки:
import matplotlib.pyplot as plt
import matplotlib.font_manager as mfonts
import cartopy.feature as cfeature
import cartopy.crs as ccrs
# setting a projection for my matplotlib Axes.
projection = ccrs.Mercator()
fig = plt.figure(figsize=(9,7))
ax = plt.axes(projection=projection)
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
# set target rectangle in-visible-area (aka 'Axes') coordinates
ax0, ax1 = at_x
ay0, ay1 = at_y
# choose exact X points as sensible grid ticks with Axis 'ticker' helper
x_targets = [x0 + ax * (x1 - x0) for ax in (ax0, ax1)]
ll = mpl.ticker.MaxNLocator(nbins=max_stripes, steps=[1,2,4,5,10])
x_vals = ll.tick_values(*x_targets)
# grab min+max for limits
xl0, xl1 = x_vals[0], x_vals[-1]
# calculate Axes Y coordinates of box top+bottom
yl0, yl1 = [y0 + ay * (y1 - y0) for ay in [ay0, ay1]]
# calculate Axes Y distance of ticks + label margins
y_margin = (yl1-yl0)*0.25
# fill black/white 'stripes' and draw their boundaries
fill_colors = ['black', 'white']
i_color = 0
regions = []
for xi0, xi1 in zip(x_vals[:-1],x_vals[1:]):
# fill region
plt.fill((xi0, xi1, xi1, xi0, xi0), (yl0, yl0, yl1, yl1, yl0),
fill_colors[i_color], alpha=1)
# Ideally, this should work: getting the region, and later on adding all up into the AnchoredOffsetbox
region = plt.plot((xi0, xi1, xi1, xi0, xi0),
(yl0, yl0, yl1, yl1, yl0),
'black', alpha=1)
i_color = 1 - i_color
regions.append(region)
Rectangles = PackerBase(children=regions)
parambox = AnchoredOffsetbox(loc=2,
child=Rectangles,
pad=0.0, frameon=True,
borderpad=0.0,
bbox_to_anchor=(1.1, 1.0),
bbox_transform=ax.transAxes)
parambox.set_clip_on(False) #so box won't be cut off when saving
ax.add_artist(parambox)
Когда запускается вышеуказанный код, появляется сообщение об ошибке: " AttributeError: у объекта 'list' нет атрибута 'axes' ".
С уважением,