FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Unable to set more than one font in legends, impossible to label data with Glyph from multiple languages. · Issue #15260 · matplotlib/matplotlib · GitHub

Repository navigation

Unable to set more than one font in legends, impossible to label data with Glyph from multiple languages. #15260

Closed

Description

Bug report

Bug summary

It is impossible to set more than one font in a legend making it impossible to label data in different languages, such as chinese, arabic and emojis, in the same legend. Allowing prop to accept lists of dictionaries would solve this issue.

Code for reproduction

import matplotlib.pyplot as plt

f, axs = plt.subplots()
plt.plot([i for i in range(5)], label=u"#방탄소년단")
plt.plot([i for i in range(6)], label=u"#🔥")
plt.legend()
plt.show()

Actual outcome

Expected outcome

While it would be nice if matplotlib searched for fonts with support for unicode characters, simply allowing lists of fonts to be specified for each handle label, rather than enforcing using one font.

Activity

  1. mvarnold commented on Sep 12, 2019

    Author

    A work around for at least rendering the text is as follows

    from fontTools.ttLib import TTFont
    import matplotlib.font_manager as mfm
    import matplotlib.pyplot as plt
    
    def char_in_font(Unicode_char, font):
        for cmap in font['cmap'].tables:
            if cmap.isUnicode():
                if ord(Unicode_char) in cmap.cmap:
                    return True
        return False
    
    def label_unicode(char, axs,x=0.5,y=0.5):
        
    
        font_info = [(f.fname, f.name) for f in mfm.fontManager.ttflist]
        
        print('finding fonts for ',char)
        font_list = []
        for i, font in enumerate(font_info):
            if char_in_font(char[1], TTFont(font[0], fontNumber=0)):
                #print(font[0], font[1]) #uncomment to see all your fonts
                font_list.append(font[0])
                
        font_path = font_list[-1]
        prop = mfm.FontProperties(fname=font_path) # find this font
        axs.text(x, y, s=char, fontproperties=prop, fontsize=20)
    
    f, axs = plt.subplots()
    plt.plot([i for i in range(5)])
    label_unicode(u"#방탄소년단 ",axs,1,1)
    label_unicode(u"#裏垢女子",axs,1.5,2)
    label_unicode(u"#🔥",axs,2,3)
    plt.show()

  2. anntzer commented on Sep 13, 2019

    Contributor

    allowing lists of fonts to be specified for each handle label, rather than enforcing using one font.

    The problem is well understood, but would require major rearchitecting of the text handling machinery, so is unlikely to happen soon.

  3. added
    Difficulty: Hardhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Nov 3, 2020
  4. anntzer commented on Nov 3, 2020

    Contributor

    I'll close this in favor of #18883 which is essentially the same but points more explicitly at font-family fallbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL