matplotlib-label-lines icon indicating copy to clipboard operation
matplotlib-label-lines copied to clipboard

font properties

Open ljing1209 opened this issue 2 years ago • 1 comments

Thanks for sharing!

I have a question about how to change the font for the label; I tried

font_path = fm.findfont(fm.FontProperties(family='Times New Roman')) font_prop = fm.FontProperties(fname=font_path, size=18)

labelLines(axs["D"].get_lines(), align=True, fontsize=18,fontproperties=font_prop)

It seems can't work...

ljing1209 avatar Aug 26 '23 02:08 ljing1209

Actually, you can use font and fontsize directly. For example:

import numpy as np
from matplotlib import pyplot as plt
from labellines import labelLines

x = np.linspace(0, 10, 300)

fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label='np.sin')
labelLines(ax.get_lines(), zorder=2.5, font="Times New Roman", fontsize=18)

If you want to use fontproperties for some reason, you can try:

import  matplotlib.font_manager as fm
x = np.linspace(0, 10, 300)
font_prop = fm.FontProperties(family="Times New Roman", size=18)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label='np.sin')
labelLines(ax.get_lines(), zorder=2.5, fontproperties=font_prop)

But in the end, your example of using the font_path should work. For example:

font_path = fm.findfont(fm.FontProperties(family='Times New Roman'))
font_prop = fm.FontProperties(fname=font_path, size=18)
fig, ax = plt.subplots()
ax.plot(x, np.sin(x), label='np.sin')
labelLines(ax.get_lines(), zorder=2.5, fontproperties=font_prop)

gepcel avatar Aug 30 '23 11:08 gepcel