首页 > 解决方案 > 可变大小图形的图例位置

问题描述

我的绘图功能每年为不同大小的数据创建水平条。我必须更改每组子图的图形大小。我需要将我的两个图例放在 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)

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

这里

这里

标签: pythonmatplotlibbar-chartlegendsubplot

解决方案


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


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()


推荐阅读