-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
[Bug]: Font weight of label cannot be overwritten from rcParams when using mathtext #23860
Comments
This bug is also reproduced on |
This is the intended behavior. You can instead control the default mathtext going using mathtext.default:
One reason is that there are mathtext-only font settings that can only be selected that way (e.g. mathcal). |
@anntzer Thank you for your quick reply. The point of this issue is difference by executing order even if same options are given. For example, I think the current result is not intended behaviour. Could you give me a comment about it? |
Ah, very sorry, I misread the issue. I agree something's strange here. |
The parameter is used here: matplotlib/lib/matplotlib/axis.py Lines 674 to 679 in c551a56
So there is a risk that the same holds for size and color. Maybe there is some inadvertent reuse of the Axis/Text object? |
Colour and size don't appear to change when the corresponding parameters are changed after creating the figure. Only the weight. And if one displays the figure instead of saving it, the opposite happens. I was able to narrow it down to this.
and
on standard output. matplotlib/lib/matplotlib/mathtext.py Lines 228 to 252 in 269c0b9
I traced the code and found that when control is passed to Mathtext here, prop.get_weight() == 'normal' . But Mathtext renders the text using 'bold' -weight glyphs. Does Mathtext directly or indirectly look up plt.rcParams["font.weight"] at any point?
|
OK, figured this out: the patch is diff --git i/lib/matplotlib/font_manager.py w/lib/matplotlib/font_manager.py
index a5742ef88f..b3e139397c 100644
--- i/lib/matplotlib/font_manager.py
+++ w/lib/matplotlib/font_manager.py
@@ -1342,9 +1342,11 @@ class FontManager:
# Pass the relevant rcParams (and the font manager, as `self`) to
# _findfont_cached so to prevent using a stale cache entry after an
# rcParam was changed.
- rc_params = tuple(tuple(mpl.rcParams[key]) for key in [
- "font.serif", "font.sans-serif", "font.cursive", "font.fantasy",
- "font.monospace"])
+ rc_params = [mpl.rcParams[f"font.{key}"] for key in [
+ "family", "style", "variant", "weight", "stretch", "size",
+ "serif", "sans-serif", "cursive", "fantasy", "monospace"]]
+ rc_params = tuple(tuple(e) if isinstance(e, list) else e
+ for e in rc_params) # Make this hashable.
ret = self._findfont_cached(
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
rc_params) Turns out this is completely independent of mathtext and can be reproduced as follows: import matplotlib as mpl
from matplotlib.font_manager import fontManager, findfont
mpl.rcParams["font.weight"] = "normal"
print(findfont("DejaVu Sans")) # DejaVuSans.ttf
mpl.rcParams["font.weight"] = "bold"
print(findfont("DejaVu Sans")) # DejaVuSans.ttf -> oops!
fontManager._findfont_cached.cache_clear()
print(findfont("DejaVu Sans")) # DejaVuSans-Bold.ttf i.e. the result of findfont depends on the default font weight/slant/etc. and these should thus also be taken into account in the caching. Actually even that approach is slightly brittle because FontProperties are mutable (they shouldn't be, but that's another problem); best would be for findfont to just call (Note that a similar problem also affects RendererWx.get_wx_font.) |
Bug summary
Font weight of x/y axis label is expected to be changed by
rcParams["font.weight"]
andrcParams["axes.labelweight"]
, and it works on first plot on the script execution. In same script, however, the updating the rcParams after first plot doesn't take effect. The problem only occurs when the label uses mathtext.Code for reproduction
I will show two different source.
By running code below, four image will be generated.
Actual outcome
1-first.png
1-second.png
2-first.png
2-second.png
Expected outcome
2-second.png
is expected to be same as1-first.png
1-second.png
is expected to be same as2-first.png
Additional information
This is also reproduced by using
plt.rcParams.update()
andwith plt.rc_context()
instead of directly assigning toplt.rcParams[key]
.Operating system
Ubuntu 20.04 on Windows subsystem for Linux
Matplotlib Version
v3.3.3, v3.5.3, HEAD (2171d95)
Matplotlib Backend
agg
Python version
3.8.11
Jupyter version
not using
Installation
pip
The text was updated successfully, but these errors were encountered: