Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Bug]: Font weight is broken in the AGG backend for some fonts on Windows #27398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TiborGY opened this issue Nov 29, 2023 · 2 comments
Closed

Comments

@TiborGY
Copy link

TiborGY commented Nov 29, 2023

Bug summary

Bold text using the Bahnschrift font is rendered as normal weight on Windows using the AGG backend (which appears to be the default). Cairo works OK.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.ticker as ticker

SMALL_SIZE = 10
MEDIUM_SIZE = 12
BIGGER_SIZE = 16

plt.rc('font', size=SMALL_SIZE)          # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE)     # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE)    # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE)    # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE)    # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE)  # fontsize of the figure title
plt.rcParams['axes.linewidth'] = 1.5

def set_axis_style(ax, labels):
    ax.set_xticks(np.arange(1, len(labels) + 1), labels=labels, fontweight='bold', fontstretch='condensed', fontname='Bahnschrift')
    ax.set_xlabel('PES', fontweight='bold', fontstretch='condensed',fontname='Bahnschrift')
    ax.yaxis.set_major_locator(ticker.MultipleLocator(3))
    ax.set_ylim([-18.0,9.0])
    ax.yaxis.set_minor_locator(ticker.MultipleLocator(1))
    ax.grid(visible=True, which='major', axis='y')

data = [
	[-16.60102571,-6.699822806,-14.62564481,7.456503834,4.016659607,7.210896854,1.804969856,0.467860939],
    [-17.32762431,-6.081446527,-14.87504614,8.068733361,6.162393395,5.727964021,2.63439223,1.278821509],
	[-10.35923985,-3.819050878,-9.664667008,5.082031256,3.879507143,5.145900878,0.234401452,-0.866473206],
    [-11.0059302,-3.560643243,-9.808961668,5.689461703,5.9734276,3.684341201,1.02932442,-0.060885493],
	[3.427329458,3.58409437,0.391482303,0.330079435,2.898602566,1.53808872,1.063593669,3.281844567,-0.075796436,0.25954612],
    [3.691012844,2.388512547,0.626317599,1.167470753,3.415479718,0.73615229,2.157193802,0.457135001,0.093632333,0.395061974],
	[1.834710076,2.068101436,0.438865565,0.98388445,1.693632724,0.239971571,1.67850917,-0.368657719,-0.221180384,0.150743242],
	[-0.350021341,-0.151871497,0.065295165,-0.107972774,-0.253106388,2.553227692,1.090027229,4.109293976,0.051641275,0.045709255],
	[-2.909762523,-1.321763299,0.285956032,0.627310784,-2.347696693,1.308652494,2.247634606,0.387280379,0.081192904,0.074938114],
	[1.426280957,-0.253391384,1.610038134,1.178748087,-1.883853498,3.30948195,-0.369635367,-0.158668355],
	[-0.673590433,-0.865603666,-0.182107106,0.356435843,-0.307266771,0.010776048,-0.052132211,0.196368734],
	[1.216522788,-0.389357266,1.176203029,1.21521229,-1.953162885,3.036492967,-0.741968769,-0.546354717],
	[-1.119479317,-2.353230497,-1.1,-1.1,-0.487929244,-0.498971093,-1.81732571,-0.846761305,-1.018986275,-1.023515653],
	[2.263427409,1.705632712,0.407670257,0.862491961,2.09552031,-0.018730139,1.602867776,-0.34663782,-0.074009823,0.160711069]
]

fig, ax1 = plt.subplots(nrows=1, ncols=1, figsize=(15, 10))
ax1.set_ylabel('Deviation of optimized stationary point energy from benchmark (kcal/mol)', fontweight='bold', fontstretch='condensed', fontname='Bahnschrift')
parts1 = ax1.violinplot(data, bw_method=0.3, points=300, widths=0.9,showmeans=False, showmedians=False, showextrema=False,positions=np.arange(1, len(data) + 1))
ax1.boxplot(data,positions=np.arange(1, len(data) + 1))


labels = ['HF\n/DZ','HF\n/TZ','HF-D3(BJ)\n/DZ','HF-D3(BJ)\n/TZ','MP2\n/DZ','MP2\n/TZ','MP2-F12\n/DZ','CCSD\n/DZ','CCSD-F12b\n/DZ','CCSD(T)\n/DZ','CCSD(T)\n-F12b/DZ','OQVCCD(T)\n/DZ','OSC','MP2-F12\n/TZ']
set_axis_style(ax1, labels)
fig.savefig('bold_Cairo.png', transparent=False, dpi=300, bbox_inches="tight", backend='Cairo')
fig.savefig('bold_AGG.png', transparent=False, dpi=300, bbox_inches="tight", backend='AGG')

Actual outcome

Cairo
bold_Cairo
AGG
bold_AGG

Expected outcome

Non-default font weights (bold, etc.) should be rendered correctly by the AGG backend, it should be much closer to the Cairo output.

Additional information

For some other fonts, AGG renders bold correctly. This issue is not just a problem for PNG output, the default interactive backend also appears to be AGG on Windows, and the same issue can be observed.

Operating system

Windows 10

Matplotlib Version

matplotlib: 3.8.2

Matplotlib Backend

QtAgg

Python version

Python 3.9.13

Jupyter version

No response

Installation

pip

@TiborGY
Copy link
Author

TiborGY commented Nov 29, 2023

Update, the default PDF writer backend also fails to render bold Bahnschrift correctly.
Fig1_default.pdf
Fig1_Cairo.pdf

@QuLogic
Copy link
Member

QuLogic commented Nov 30, 2023

This is a variable font, though looking at it in Windows font viewer appears to show multiple subfonts within. Variable fonts are not supported by the FreeType version we have bundled in Matplotlib, and updating that is tracked in #20842. The font collection part is tracked in #3135.

You may be able to get it to work if you extract all the subfonts with something like FontForge. You can also try mplcairo, which has better font support.

I'm going to close this though, as both causes are tracked elsewhere.

@QuLogic QuLogic closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants