Bug summary
Using plt.clabel(manual=True) adds labels but the labels are not easily readable because only a small gap is created in the contour.
Code for reproduction
The following works:
plt.figure() x = np.arange(400).reshape((20,20)) c = plt.contour(x) plt.clabel(c)
But the following doesn't give as nice a result:
plt.figure() x = np.arange(400).reshape((20,20)) c = plt.contour(x) plt.clabel(c, manual=True) # click around
This happens with both the Qt5 and MacOS X backends, and I am using a retina display.
Matplotlib version
My guess is that this is a dpi scaling related issue as there is a mapping from screen-space back to data space...
FWIW I can't repro on a non-high-DPI display, even using QT_SCALE_FACTOR to fake one.
But perhaps @astrofrog you can try whether something along the lines of
diff --git i/lib/matplotlib/contour.py w/lib/matplotlib/contour.py index 59b9fe63b..198aa78f9 100644 --- i/lib/matplotlib/contour.py +++ w/lib/matplotlib/contour.py @@ -17,9 +17,7 @@ import matplotlib.collections as mcoll import matplotlib.font_manager as font_manager import matplotlib.text as text import matplotlib.cbook as cbook -import matplotlib.mathtext as mathtext import matplotlib.patches as mpatches -import matplotlib.texmanager as texmanager import matplotlib.transforms as mtransforms # Import needed for adding manual selection capability to clabel @@ -263,24 +261,12 @@ class ContourLabeler(object): """ if not isinstance(lev, str): lev = self.get_text(lev, fmt) - - lev, ismath = text.Text.is_math_text(lev) - if ismath == 'TeX': - if not hasattr(self, '_TeX_manager'): - self._TeX_manager = texmanager.TexManager() - lw, _, _ = self._TeX_manager.get_text_width_height_descent(lev, - fsize) - elif ismath: - if not hasattr(self, '_mathtext_parser'): - self._mathtext_parser = mathtext.MathTextParser('bitmap') - img, _ = self._mathtext_parser.parse(lev, dpi=72, - prop=self.labelFontProps) - lw = img.get_width() # at dpi=72, the units are PostScript points - else: - # width is much less than "font size" - lw = (len(lev)) * fsize * 0.6 - - return lw + fig = self.ax.figure + width = (text.Text(0, 0, lev, + figure=fig, fontproperties=self.labelFontProps) + .get_window_extent(fig.canvas.get_renderer()).width) + width *= 72 / fig.dpi + return width def set_label_props(self, label, text, color): """Set the label properties - color, fontsize, text."""
helps? Note that get_window_extent() also takes dpi as a second argument; you may also want to check whether setting the value there helps...
Can anyone with a high-dpi display check this?
Check what? The patch or just that the original issue is there?
Both.
The clabel works fine on master w/ hi-dpi and Qt5Agg. Didn't check the patch (do I need to?)
Note I don't manually set the dpi at any point.
Let's ping @astrofrog to see whether he can give this a try again; if there's no reply we'll just close this.
Assuming fixed on master. Please ping if the problem still exists.
Activity
tacaswell commented on Jun 29, 2017
My guess is that this is a dpi scaling related issue as there is a mapping from screen-space back to data space...
anntzer commented on Jan 13, 2019
FWIW I can't repro on a non-high-DPI display, even using QT_SCALE_FACTOR to fake one.
anntzer commented on Jan 13, 2019
But perhaps @astrofrog you can try whether something along the lines of
diff --git i/lib/matplotlib/contour.py w/lib/matplotlib/contour.py index 59b9fe63b..198aa78f9 100644 --- i/lib/matplotlib/contour.py +++ w/lib/matplotlib/contour.py @@ -17,9 +17,7 @@ import matplotlib.collections as mcoll import matplotlib.font_manager as font_manager import matplotlib.text as text import matplotlib.cbook as cbook -import matplotlib.mathtext as mathtext import matplotlib.patches as mpatches -import matplotlib.texmanager as texmanager import matplotlib.transforms as mtransforms # Import needed for adding manual selection capability to clabel @@ -263,24 +261,12 @@ class ContourLabeler(object): """ if not isinstance(lev, str): lev = self.get_text(lev, fmt) - - lev, ismath = text.Text.is_math_text(lev) - if ismath == 'TeX': - if not hasattr(self, '_TeX_manager'): - self._TeX_manager = texmanager.TexManager() - lw, _, _ = self._TeX_manager.get_text_width_height_descent(lev, - fsize) - elif ismath: - if not hasattr(self, '_mathtext_parser'): - self._mathtext_parser = mathtext.MathTextParser('bitmap') - img, _ = self._mathtext_parser.parse(lev, dpi=72, - prop=self.labelFontProps) - lw = img.get_width() # at dpi=72, the units are PostScript points - else: - # width is much less than "font size" - lw = (len(lev)) * fsize * 0.6 - - return lw + fig = self.ax.figure + width = (text.Text(0, 0, lev, + figure=fig, fontproperties=self.labelFontProps) + .get_window_extent(fig.canvas.get_renderer()).width) + width *= 72 / fig.dpi + return width def set_label_props(self, label, text, color): """Set the label properties - color, fontsize, text."""helps?
Note that get_window_extent() also takes dpi as a second argument; you may also want to check whether setting the value there helps...
anntzer commented on Oct 18, 2019
Can anyone with a high-dpi display check this?
jklymak commented on Oct 18, 2019
Check what? The patch or just that the original issue is there?
anntzer commented on Oct 18, 2019
Both.
jklymak commented on Oct 18, 2019
The clabel works fine on master w/ hi-dpi and Qt5Agg. Didn't check the patch (do I need to?)
jklymak commented on Oct 18, 2019
Note I don't manually set the dpi at any point.
anntzer commented on Oct 18, 2019
Let's ping @astrofrog to see whether he can give this a try again; if there's no reply we'll just close this.
timhoffm commented on Nov 5, 2019
Assuming fixed on master. Please ping if the problem still exists.