[Python] How to remove the symbol part before text in legend in matplotlib

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    [​IMG]

    I added these custom legends in the attached plot, but it has a weird space in front of the text. Is there a way I can remove the empty space?

    Code:

    # Plotting
    plt.figure(figsize=(7, 5)) # Set figure size
    plt.plot(time_vector[:len(volumes)], np.array(volumes)*1e3)

    # Add labels with fontsize 15
    plt.xlabel("Time (h)", fontsize=25)
    plt.ylabel("Flow Volume (mL)", fontsize=25)

    # Define custom legend
    custom_lines = [
    Line2D([0], [0], color='none', marker=None, linestyle='None', label=f'Initial Flow Volume : 0.78 (mL)'),
    Line2D([0], [0], color='none', marker=None, linestyle='None', label=fr'Flow Ramp Factor : 0.170 (h$^{{-1}}$)'),
    Line2D([0], [0], color='none', marker=None, linestyle='None', label=f'Feeding Interval : 4.434 (min)'),
    ]

    # Add the custom legend
    plt.xticks(fontsize=25) # Set x-tick font size
    plt.yticks(fontsize=25) # Set y-tick font size
    plt.legend(handles=custom_lines, fontsize=20) # Set legend font size
    figure_path = os.path.join('figures','figure6','figure6a.svg')
    plt.savefig(figure_path, format="svg", bbox_inches="tight", dpi=300)
    plt.show()

    Continue reading...

Compartilhe esta Página