3

I am trying to use alternatives font in matplotlib. by using font_manager, I can get things like xlabel, ylabel or title working, but I can't figure out how to change font on line label

I pick "Phosphate.ttc" font as it's easy to spot.


import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

def lineChart():
    my_font = fm.FontProperties(fname='/Library/Fonts/Phosphate.ttc')
    day = ('1','2','3')
    line1 = (10,20,30)
    line2 = (30,20,10)

    fig, ax = plt.subplots()
    ax.plot(day, line1, label='this goes to default font')
    ax.plot(day, line2, label='this is default font too')
    ax.legend(loc='upper center')
    plt.title('title display correct', fontproperties=my_font)
    plt.xlabel('xlabel display correct', fontproperties=my_font)
    plt.ylabel('ylabel display correct', fontproperties=my_font)
    plt.savefig("font_test.png")

if __name__ == '__main__':
    lineChart()

if there anyway to set alternatives font globally? (without install it to matplotlib module path)

or to setup line label to use alternatives font?

OS: OSX
Python: 3.6.4
matplotlib: 2.1.1

Thanks

3
  • I saw that post before. it's to set font family via rcParams. not really using a whole different font (not exist from matplotlib's own font dir). Or is there a way to register new font? I tried fm.FontProperties = fm.FontProperties(fname='/Library/Fonts/Phosphate.ttc') seems matplotlib doesn't like it Commented Jan 19, 2018 at 21:01
  • Ok, I'm not sure if the duplicate flag is removed by removing the comment that is shown, but I'll try. Commented Jan 19, 2018 at 21:08
  • Note to moderators: I flagged this question as duplicate, but the OP pointed out that the suggested duplicate does not solve his problem, so my flag is probably not justified. Therefore also the answer below. Commented Jan 19, 2018 at 21:20

1 Answer 1

2

You can set the font in the legend command. Changing the respective line to:

ax.legend(loc='upper center', prop=my_font)

produces this image:

legend with custom font

EDIT:

Changing the font of the tick labels can be done in the following way (add this after the plot command):

for tick in ax.xaxis.get_major_ticks():
    tick.label.set_fontproperties(my_font)

for tick in ax.yaxis.get_major_ticks():
    tick.label.set_fontproperties(my_font)

The result then looks like this:

ticks with custom font

Sign up to request clarification or add additional context in comments.

5 Comments

ax.legend(prop=my_font) do not seems cover "day"; ax.plot(day, ...) font in day is also "default".
@RuiLi Do you mean the xtick and ytick labels?
yes. I remember I saw one stack overflow has something about xticks and yticks font setting. some how I can't find it anymore
I fount it from matplotlib.org/api/text_api.html#matplotlib.text.Text. in the set_xticklabels, it accept **kwargs, and one of the keyword it accept is fontproperties
@RuiLi Good. See also the edit to my answer for an alternative method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.