Legend alignment

Hello,

Below is a MWE of a figure that includes two legends, one in two-column format and the other in one-column format. As written, the horizontal alignment between the two legends depends on details such as whether I generate the figure in a Jupyter notebook or with a script. I’ve experimented with transformations and have been unable to find a solution that robustly centers the legend keys of the single-column legend at the horizontal center of the two legend keys in the two-column legend. Any suggestions?

fig, ax = plt.subplots()

x = np.linspace(0, 1)
m = np.arange(1, 4)
lgnd_loc = (0.1, 0.1)
lbl = []
ln_pos = []
ln_neg = []
for k in m:
    ln_pos += ax.plot(x, k * x)
    ln_neg += ax.plot(x, -k * x)
    lbl.append(f"m = {k:d}")
ln_zero = ax.plot(x, np.zeros_like(x), label=f"m = 0")

# Create first part of the legend
lgnd_2col = ax.legend(
    title="Pos.   Neg.",
    alignment="left",
    handles=[*zip(ln_pos, ln_neg)],
    labels=lbl,
    frameon=False,
    fontsize="small",
    handler_map={tuple: HandlerTuple(ndivide=None, pad=1.0)},
    handlelength=6.0,
    loc="lower left",
    bbox_to_anchor=lgnd_loc,
    borderpad=0,
)

# Add first part of legend as an artist to create second part
lgnd_2col_art = ax.add_artist(lgnd_2col)

# Create second part of the legend
lgnd_loc_new = (lgnd_loc[0] - ax.transAxes.inverted().transform((60, 0))[0], lgnd_loc[1])
lgnd_1col = ax.legend(
    alignment="center",
    handles=ln_zero,
    ncols=1,
    frameon=False,
    fontsize="medium",
    loc="upper left",
    bbox_to_anchor=lgnd_loc_new,
    borderpad=0,
)

plt.show()

legend_mwe