How can we set mathtext font without manipulating RC per Text element. Text objects cannot take in this parameter, as apparently the only way to set "mathtext.fontset"="Serif" is to make it global (not per some Text element).
This would be a very useful feature for packages using matplotlib as a backend. For example at JuliaPlots/Plots.jl we have user settable fonts per every plot element, hence mathfont should also change per each element.
I don't think it should be "particularly" hard to add a per-Text property to set this (I guess we can't really reuse FontProperties for that because they are not precise enough for mathtext); as usual someone needs to give it a try :)
Find the (only) usage of rcParams['mathtext.fontset'] in MathTextParser.parse().
Trace the call hirachy back to find out how a Text rendering finally calls parse().
Add a parameter to define the font for Text objects.
Pass this parameter down the call hirachy so that is can be used in parse()
Check that the parameter can be passed from relevant functions such as Axes.title() and Axes.text() (This is likely the case because keyword arguments are passed).
While trying to solve this I realised that the fontset passed to rcParams is validated and the methods to make this validation are all private to the rcsetup.py and the init from matplotlib.
I think the fontset used in a Text should have the same validations as if it were an rcParam, but I see no easy way of doing that.
Edit: well I could just import _validators from rcsetup.py and do the validations myself, it would be 3 lines of code, but I think using a private variable (one that starts with _) outside of its domain would be considered a bad practice, I am not sure.
The topic of validator availability is still an open issue. Historically, validators were only used by the rcParams parser. But I agree that they can be useful in other parts of the library as well. We should solve this on a fundamental level from the library side. This needs careful consideration regarding import order. For now, you may import _validators. As long as the import of _validators is only within the library, it's ok-ish. The important aspect is that it's not public API for users of Matplotlib.
Activity
isentropic commented on Jul 13, 2020
The only way is to set it globally according to docs
isentropic commented on Jul 13, 2020
This does not help a lot too
timhoffm commented on Jul 13, 2020
AFAICS this is a global setting that cannot be modified per Text element. ping @anntzer
isentropic commented on Jul 14, 2020
This would be a very useful feature for packages using matplotlib as a backend. For example at JuliaPlots/Plots.jl we have user settable fonts per every plot element, hence mathfont should also change per each element.
anntzer commented on Jul 14, 2020
I don't think it should be "particularly" hard to add a per-Text property to set this (I guess we can't really reuse FontProperties for that because they are not precise enough for mathtext); as usual someone needs to give it a try :)
timhoffm commented on Jul 14, 2020
👉 Things to do:
timhoffm commented on Jul 14, 2020
Related: #6518, #7107
diegopetrola commented on Jul 27, 2020
While trying to solve this I realised that the fontset passed to rcParams is validated and the methods to make this validation are all private to the rcsetup.py and the init from matplotlib.
I think the fontset used in a Text should have the same validations as if it were an rcParam, but I see no easy way of doing that.
Edit: well I could just import _validators from rcsetup.py and do the validations myself, it would be 3 lines of code, but I think using a private variable (one that starts with _) outside of its domain would be considered a bad practice, I am not sure.
If someone has a suggestion or want to discuss any change I made, feel free to contact me.
I am working on the problem here:
https://github.com/diegoleal/matplotlib/tree/issue%2317906
timhoffm commented on Jul 27, 2020
The topic of validator availability is still an open issue. Historically, validators were only used by the rcParams parser. But I agree that they can be useful in other parts of the library as well. We should solve this on a fundamental level from the library side. This needs careful consideration regarding import order. For now, you may import _validators. As long as the import of _validators is only within the library, it's ok-ish. The important aspect is that it's not public API for users of Matplotlib.