LalaLand
LalaLand

Reputation: 139

How to remove the symbol part before text in legend in matplotlib

enter image description here

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

Upvotes: 0

Views: 58

Answers (3)

gboffi
gboffi

Reputation: 25093

A no-plot plot

You can simply use plt.text

import matplotlib.pyplot as plt

plt.plot((0,7),(0,145), lw=0)
plt.text(0, 130, "Initial …\nFlow …\nFeeding …" size=20,
         va='top', bbox=dict(boxstyle='round', fill=0, edgecolor='gray'))
plt.show()

Upvotes: 1

RuthC
RuthC

Reputation: 3896

As gboffi notes, you do not need a legend you only need text. You can place your text in the upper left with AnchoredText.

import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredText

fig, ax = plt.subplots(figsize=(7, 5))
at = AnchoredText("Initial...\nFlow...\nFeeding...",
                  prop=dict(size=20), frameon=True, loc='upper left')
at.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
at.patch.set_edgecolor("gray")
ax.add_artist(at)

plt.show()

enter image description here

Upvotes: 1

HH-HELP-ME
HH-HELP-ME

Reputation: 1

You could try removing the border around the legend and then aligning the text to the left, specifically

# 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, frameon=False)  # Set legend font size - remove frame
plt.gca().get_legend()._legend_box.align = "left"  # Align legend to left
figure_path = os.path.join('figures','figure6','figure6a.svg')
plt.savefig(figure_path, format="svg", bbox_inches="tight", dpi=300)
plt.show()

Might need some tweaking for this to work exactly as I don't have access to your data.

Upvotes: 0

Related Questions