Matplotlib Output Legend box has wrong size in PGF output

Viewed 198

currently, I'm struggling with an odd problem which occurs when exporting a matplotlib figure to PGF. As seen in the screenshot (from my LaTeX document), the output has a corrupted legend box, see fig. 1, which is too small. However, when I look at the PDF output (not depicted), the legend box has the correct, large width, so that the legend text fits perfectly.

PGF output (screenshot from LaTex document) with corrupted legend box

Here is the code snippet from my LaTeX python document. (The # ... stands for lines which are not important for the issue.)


    import matplotlib.pyplot as plt
    import numpy as np
    import seaborn as podo
    from matplotlib import rc
    import csv, sys, glob, re
    rc('text', usetex=True)
    
    # ...
    
    plt.rc('font', family='serif')
    box_linewidth = 0.416667
    plt_linewidth = 0.625
    plt.rcParams['axes.linewidth'] = box_linewidth
    plt.rcParams['axes.labelcolor'] = "k"
    plt.rcParams['axes.edgecolor'] = "k"
    plt.rcParams['lines.linewidth'] = plt_linewidth
    plt.rcParams['patch.linewidth'] = box_linewidth
    plt.rcParams['xtick.minor.width'] = box_linewidth
    plt.rcParams['xtick.major.width'] = box_linewidth
    plt.rcParams['ytick.minor.width'] = box_linewidth
    plt.rcParams['ytick.major.width'] = box_linewidth
    plt.rcParams['hatch.linewidth'] = 0.1
    plt.rcParams['lines.markersize'] = 4
    plt.rcParams['lines.markeredgewidth'] = plt_linewidth
    plt.rcParams['font.size'] = 10
    plt.rcParams['errorbar.capsize'] = 10
    plt.rcParams['grid.color'] = "k"
    plt.rcParams['grid.linewidth'] = box_linewidth
    plt.rcParams['legend.edgecolor'] = "k"
    plt.rcParams['legend.framealpha'] = 1.0
    plt.rcParams['legend.columnspacing'] = 1.5
    plt.rcParams['legend.labelspacing'] = 0.5
    plt.rcParams['legend.handletextpad'] = 0.6
    plt.rcParams['errorbar.capsize'] = 2
    plt.rcParams['axes.xmargin'] = 0.05
    
    # ...
    
    fig = plt.figure(num=i, figsize=(plot_width, plot_height), tight_layout=False, constrained_layout=True, facecolor='w', edgecolor='k')
    
    # ...
    
    plt.subplot(4, 1, 1)
    ax4 = plt.gca()
    ax4.set_xlim(0,60)
    ax4.set_ylim(-10,0)
    ax4.yaxis.label.set_color('white')
    ax4.set_ylabel("z")
    ax4.set_xlabel("$x$ / mm")
    ax4.tick_params(axis='x', colors='white')
    ax4.set_aspect(1)
    p1 = plt.plot(mean_step_intpl(cant_p_x_1000_sim,cant_p_z_1000_sim,cant_m_x_1000_sim,cant_m_z_1000_sim)[0],mean_step_intpl(cant_p_x_1000_sim,cant_p_z_1000_sim,cant_m_x_1000_sim,cant_m_z_1000_sim)[1], linestyle="solid", color="gray", label="simulation",zorder=2)
    p2 = plt.plot(circ_x_1000_sim, circ_z_1000_sim, "-k", label="pressure stamp",zorder=1)
    p3 = plt.scatter(cant_x_1000_meas, cant_z_1000_meas, label="measurement", s=18.8, facecolors='none', edgecolors='k',zorder=3)
    
    plt.legend(loc="lower left")
    
    # ...
    
    plt.savefig(path + "/" +  filename + ".pdf")
    plt.savefig(path + "/" +  filename + ".pgf")

Do you know why the PGF output re-sizes the legend in such a wrong way? Looking forward to your suggestions, and kind regards,

Pascal B.

0 Answers
Related