可变大小图形的图例位置



我的绘图函数每年为不同大小的数据创建水平条。我必须更改每组子画面的图形大小。我需要把我的两个图例放在x轴标签下方每个图形的下中心。位置需要根据图形大小而变化,并保持一致。因此,对于所有制作的数字,图例都会像这个数字。

在此处查找我的数据帧片段。我已经尽可能地简化了代码,我知道这个情节缺少一些元素,但我只想得到我的问题的答案,而不是在这里创建一个完美的情节。我知道我可能需要为我的锚点边界框创建一个变量,但我不知道如何创建。这是我的代码:

def plot_bars(data,ax):
""" Plots a single chart of work plan for a specific routeid
data: dataframe with section length and year
Returns: None"""
ax.barh(df['year'], df['sec_len'] , left = df['sec_begin'])
ax.set_yticklabels('')
def plot_fig(df):
# Draw the plots
ax_set = df[['routeid','num_bars']].drop_duplicates('routeid')
route_set = ax_set['routeid'].values
h_ratios = ax_set['num_bars'].values
len_ratio = h_ratios.sum()/BARS_PER_PAGE # Global constant set to 40 based on experiencing 
fig, axes = plt.subplots(len(route_set), 1, squeeze=False, sharex=True
, gridspec_kw={'height_ratios':h_ratios}
, figsize=(10.25,7.5*len_ratio))

for i, r in enumerate(route_set):
plot_bars(df[df['routeid']==r], axes[i,0])
plt.xlabel('Section length')
## legends
fig.legend(labels=['Legend2'], loc=8, bbox_to_anchor=(0.5, -0.45))
fig.legend( labels=['Legend1'], loc = 8, bbox_to_anchor=(0.5, -0.3))
## Title
fig.suptitle('title', fontsize=16, y=1)
fig.subplots_adjust(hspace=0, top = 1-0.03/len_ratio)
for df in df_list:
plot_fig(df)

问题是,当图形大小发生变化时,图例会像这些图片中那样移动:

此处

此处

我认为问题归结为相对于xlabel具有正确的相对位置,因此您需要使用xlabel的位置和轴的高度/宽度来计算bbox_to_anchor,这是正确的。类似这样的东西:


fig, (ax, ax1) = plt.subplots(nrows=2, figsize=(5, 4), gridspec_kw={'height_ratios':[4, 1]})

ax.plot(range(10), range(10), label="myLabel")
ax.set_xlabel("xlabel")
x, y = ax.xaxis.get_label().get_position() # position of xlabel
h, w = ax.bbox.height, ax.bbox.width # height and width of the Axes
leg_pos = [x + 0 / w, y - 55 / h] # this needs to be adjusted according to your needs
fig.legend(loc="lower center", bbox_to_anchor=leg_pos, bbox_transform=ax.transAxes)
plt.show()

最新更新