You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Public API to modify mathtext font constants #9165
I recently needed to modify the mathtext font constants (the superscript height) of a non-standard font (Arial) and found that there is no public API to accomplish this. I did, however, find that I could do what I wanted by accessing the private mathtext._font_constant_mapping dictionary. A minimal working example is below:
importmatplotlib.pyplotaspltfrommatplotlibimportmathtextplt.rcParams.update({
'text.usetex':False,
'font.family':'sans-serif',
'font.sans-serif':['Arial'],
'mathtext.default':'regular',
})
classArialFontConstants(mathtext.FontConstantsBase):
# Percentage of x-height that superscripts are raised from the baselinesup1=0.4# Register font constants for Arialmathtext._font_constant_mapping['Arial'] =ArialFontConstantstext=r'$h = 6.62607004 \times 10^{-34}\; m^2\, kg\,/\,s$'plt.figure(figsize=(3, 2))
plt.axis('off')
plt.text(0, 0.5, text)
plt.show()
This works great, but I think there should be a public and documented method to customize mathtext font constants. Perhaps we should just remove the leading underscore of _font_constant_mapping to make it permanent and public? Also, I think that an example of the new public API should be added to the mathtext tutorial documentation.
Thanks,
Tested with matplotlib 2.0.2 and python 3.6.2 running on Arch Linux.
@mdboom Michael, git blame tells me that you changed the API back in baa2b6e. Did you have a particular reason in mind for keeping _font_constant_mapping private?
It's probably private because no one asked for it to be public, and we are actively trying to keep the public API as small as possible (and if it goes public it should be properly documented). Of course the funny thing is that the various constant classes are themselves public.
FWIW having spent ~10s looking at it I'd guess the various classes should just be namedtuples (using ._replace if we want to be able to inherit from another font constant), but otherwise have no strong opinion as to making the dictionary public (if documented).
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!
Activity
u55 commentedon Sep 9, 2017
@mdboom Michael, git blame tells me that you changed the API back in baa2b6e. Did you have a particular reason in mind for keeping
_font_constant_mappingprivate?anntzer commentedon Sep 10, 2017
It's probably private because no one asked for it to be public, and we are actively trying to keep the public API as small as possible (and if it goes public it should be properly documented). Of course the funny thing is that the various constant classes are themselves public.
FWIW having spent ~10s looking at it I'd guess the various classes should just be namedtuples (using
._replaceif we want to be able to inherit from another font constant), but otherwise have no strong opinion as to making the dictionary public (if documented).github-actions commentedon Apr 18, 2023
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!