python - Use different font properties for label in matplotlib legend -


i use 2 different font size in same legend in matplotlib graph.

for exemple, use 15 points bold font labels , 10 points normal font others labels in same legend box.

my plot legend regroup legend 2 axes (single legend multiple axes) , have variable number of data plotted.

i use blank data strategy add annotation legend plot (is possible add string legend item in matplotlib). make bigger sized, bolt fount if it's possible.

thank you!

you may able use following pattern on text objects contained in legend() :

leg = plt.legend()  i, text in enumerate(leg.get_texts()):      # pretend element changed in index position 3     if == 3:          text.set_weight("bold")         text.set_size("x-large")          # or equivalently 1 call setp()         plt.setp(text, weight="bold", size="x-large") 

edit: looks changing things in 1 text object inside legend applies objects. 1 workaround works use mathtext text.set_text(r'$\mathbf{label}$'). in loop, we'd need retrieve label , deal mathtext "condensing" spaces. here's example:

line1, = plt.plot([3,2,1], marker='o', label='line 1 label long , has spaces') line2, = plt.plot([1,2,3], marker='o', label='line 2') leg = plt.legend()  i, text in enumerate(leg.get_texts()):      # element changed in index position 0     if == 0:          t = text.get_text()         new_label = "".join([i + r"\/" in t.split()])         text.set_text(r'$\mathbf{}$'.format("{" + new_label + "}")) 

i don't know if using mathtext or latex acceptable you, appears allow unique formatting of single legend text label.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -