Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Matplotlib would not try to apply all the font in font list to draw all characters in the given string. #18883

Copy link
Copy link
Closed
@Hsins

Description

@Hsins
Issue body actions

Bug report

Bug summary

The document page Configuring the font family said that:

You can explicitly set which font family is picked up for a given font style (e.g., 'serif', 'sans-serif', or 'monospace').
In the example below, we only allow one font family (Tahoma) for the sans-serif font style. You the default family with the font.family rc param, e.g.,:

rcParams['font.family'] = 'sans-serif'

and for the font.family you set a list of font styles to try to find in order:

rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans', 'Lucida Grande', 'Verdana']

But the mechanism behind the strings rendering with the font-family is not as same as what most users think.

  • The matplotlib just finds the font in the font list and apply the first valid one (can be found in the given path) to all characters in the given string.
  • We can use a list of fonts to the font-family settings in Visual Studio Code, Sublime Text, and websites. They have their own algorithm to deal with the problem of fonts fallback so that we can apply each font in the list in order to draw the correct character. Refer to How do web browsers implement font fallback?

Would matplotlib have any plan to implement this feature? There were so many users got into the trouble of missing fonts when we want to show the string mixed with CJK characters and Latin characters.

Code for reproduction

Just take a look at this Colab Notebook.

use ['DejaVu Sans', 'SimHei', 'sans-serif']

# Setup the fonts
matplotlib.rcParams['font.sans-serif'] = ['DejaVu Sans', 'SimHei', 'sans-serif']

def model(x, p):
    return x ** (2 * p + 1) / (1 + x ** (2 * p))

x = np.linspace(0.75, 1.25, 201)

fig, ax = plt.subplots()
for p in [10, 15, 20, 30, 50, 100]:
    ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='電壓 Voltage (mV)')
ax.set(ylabel='電流 Current (A)')
ax.autoscale(tight=True)
  • 電壓 and 電流 can't be correctly rendered with the current font DejaVu Sans.

use ['SimHei', 'DejaVu Sans', 'sans-serif']

# Setup the fonts
matplotlib.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'sans-serif']

def model(x, p):
    return x ** (2 * p + 1) / (1 + x ** (2 * p))

x = np.linspace(0.75, 1.25, 201)

fig, ax = plt.subplots()
for p in [10, 15, 20, 30, 50, 100]:
    ax.plot(x, model(x, p), label=p)
ax.legend(title='Order')
ax.set(xlabel='電壓 Voltage (mV)')
ax.set(ylabel='電流 Current (A)')
ax.autoscale(tight=True)
  • 電壓 and 電流 can be correctly rendered with the current font SimHei.
  • Voltage and Current are also be rendered with the current font SimHei.

Expected outcome

We would like to apply the Chinese font SimHei to the Chinese characters and the English font DejaVu Sans to the English characters. Just like what we use font-family style in CSS:

Similar Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Hardhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issueshttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuestopic: text/fonts

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.