-
-
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
Remove font warning when legend is added while using Tex #17521
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way we can test this?
I can confirm that after building from source, with diff git diff master e17518
diff --git a/lib/matplotlib/offsetbox.py b/lib/matplotlib/offsetbox.py
index 7bcf3f3de..60ddf06ef 100644
--- a/lib/matplotlib/offsetbox.py
+++ b/lib/matplotlib/offsetbox.py
@@ -876,7 +876,8 @@ class TextArea(OffsetBox):
def get_extent(self, renderer):
_, h_, d_ = renderer.get_text_width_height_descent(
- "lp", self._text._fontproperties, ismath=False)
+ "lp", self._text._fontproperties,
+ ismath="TeX" if self._text.get_usetex() else False)
bbox, info, d = self._text._get_layout(renderer)
w, h = bbox.width, bbox.height
and confirming that I have moved on from matplotlib 3.2.1: Python 3.8.2 (default, Apr 27 2020, 15:53:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> matplotlib.__version__
'3.2.1.post2648+g80935b1f9' That the MWE: from matplotlib import rc
import matplotlib.pyplot as plt
rc('font', **{'family':'serif','serif':'Computer Modern'})
rc('text', usetex=True)
plt.figure()
plt.plot(0,0,label='Input')
plt.title('Commenting out legend removes warning')
plt.text(0, 0, 'Text test')
plt.xlabel('Label Test')
plt.xticks(ticks=[-1,1],labels=['TestA','$MATH_{\pi}$'])
plt.legend()
plt.show() now does not give an error while using |
I mean an automated test. |
I don't know how to do that, where should I start? |
Hi @Struan-Murray There are instructions in the developer guide. But really, just do this if you are interested a) not sure that a test is easy for this and b) its a bit of a process if you are just trying to get a bug squashed... |
Hey @jklymak, I'm reading up on pytest just now in the developer guide, I assume this is what you meant unless it's the online tests further down, let me know. I just started using matplotlib a few days ago (it's working great so far, thanks!) so I might be a bit out of my depth. I'm understanding that I would need to try find a way to assert that the code does not throw a warning when the second argument (ismath) is set to "TeX" as opposed to "False" where it does, I don't know how to do this within a test environment, yet. From my side the bug is squashed, I'll probably continue reading up on testing but might just observe any further changes as it seems a little too complicated for what was just a minor annoyance. Thanks for the help. |
Well the nice thing about warnings is that they lead to a failed test, so you could just add your routine above (boil it down as simple as you can) to the end of |
Smaller MWE: from matplotlib import rc
import matplotlib.pyplot as plt
rc('font', **{'family':'serif','serif':'Computer Modern'})
rc('text', usetex=True)
plt.figure()
plt.plot(0,0,label='Input')
plt.legend()
plt.show() So as I understand it, this test should give a warning to show that it will still alert the user that an incorrect font has been selected? I see what you mean, I'll have to find a way to select a font that is guaranteed to not be installed to show that it still throws a warning code, maybe I'll just put some random characters in. Either way it's late here so I'll work on it later. |
ping @Struan-Murray thanks for working on this - any progress on an automated test? |
Hi @jklymak. I tried to create a test for this at the time but unfortunately have not had the time to get it to work, especially for something which was such a small issue. Had I worked on anything like this before I would likely be able to do it but I am interested to see how this is handled (if anyone is willing) so I am capable of creating tests in the future. |
Fixes matplotlib#17518. Co-authored-by: Antony Lee <anntzer.lee@gmail.com>
I rebased and added a test for you. Note, this is a little harder than @jklymak guessed, as it's not a warning, but a warning-level log message. |
Thanks for the PR! |
PR Summary
Addresses issue #17518 regarding a warning being shown when legend is added while using Tex. Code by @anntzer.
PR Checklist