Skip to content

tick labels displaced vertically with text.usetex and xcolor #19234

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

Closed
darthoctopus opened this issue Jan 4, 2021 · 9 comments · Fixed by #19501
Closed

tick labels displaced vertically with text.usetex and xcolor #19234

darthoctopus opened this issue Jan 4, 2021 · 9 comments · Fixed by #19501
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: confirmed bug status: has patch patch suggested, PR still needed topic: text/usetex
Milestone

Comments

@darthoctopus
Copy link

darthoctopus commented Jan 4, 2021

Bug report

Tick labels on vertical axes are displaced downwards when text.usetex is set to True.

Code for reproduction

import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
plt.scatter([0, 1, 2, 3], [2,3,1,6])
plt.show()
#

Actual outcome

Screenshot from 2021-01-04 10-51-12

Expected outcome

Screenshot from 2021-01-04 10-51-57

This change happened between 3.2.1 and 3.2.2 (can reproduce in 3.2.2 onwards, inclusive)

Matplotlib version

  • Operating system: Arch Linux
  • Matplotlib version: 3.2.2 onwards
  • Matplotlib backend: module://ipykernel.pylab.backend_inline
  • Python version: 3.9
  • Jupyter version (if applicable): 6.1.6
  • Other libraries: N/A

Matplotlib installed with pip

@darthoctopus
Copy link
Author

I am able to resolve the issue by undoing the changes made to dviread.py in #16476.

@jklymak
Copy link
Member

jklymak commented Jan 4, 2021

I cannot reproduce using either master or v3.2.2. Do you have a pristine matplotlibrc?

@tacaswell
Copy link
Member

tacaswell commented Jan 4, 2021

Can you try clearing ~/.cache/matplotlib/tex.cache?

My guess is that across that version upgrade we changed the baseline, the output of latex changed, but the hash we use to name the files did not. Thus you are seeing a miss-match between where the code that places the image of the text on the output thinks the output is vs where the code that generates the output thought it should have been.

Guess was wrong.

@darthoctopus
Copy link
Author

  • The issue persists after wiping my matplotlib cache directory and restarting the kernel
  • I found that the issue was resolved by removing this snippet: \usepackage[dvipsnames]{xcolor} from my latex preamble in matplotlibrc. However, I would like not to lose this functionality. Is there any alternative solution that allows me to both use latex and also change text colour inside equations?

@darthoctopus darthoctopus changed the title tick labels displaced vertically with text.usetex tick labels displaced vertically with text.usetex and xcolor Jan 4, 2021
@tacaswell
Copy link
Member

I suspect that we will need to make the logic divread.py more sophisticated to cope with what ever changes xcolor adds?

attn @anntzer

@tacaswell
Copy link
Member

For completeness the minimal repro case is

plt.rcParams.find_all('preamble')
import matplotlib.pyplot as plt
plt.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}'
plt.rcParams['text.usetex'] = True
plt.scatter([0, 1, 2, 3], [2,3,1,6])
plt.show()

and I can reproduce the bug.

@anntzer
Copy link
Contributor

anntzer commented Jan 4, 2021

Thanks for the report. Looks like the following patch fixes the bug, can you confirm?

diff --git i/lib/matplotlib/dviread.py w/lib/matplotlib/dviread.py
index 6f65de52b..6b7b52dc1 100644
--- i/lib/matplotlib/dviread.py
+++ w/lib/matplotlib/dviread.py
@@ -313,18 +313,26 @@ class Dvi:
         #   xxx comment
         #   down
         #   push
-        #     down, down
+        #     down
+        #     <push, push, xxx, right, xxx, pop, pop>  # if using xcolor
+        #     down
         #     push
         #       down (possibly multiple)
         #       push  <=  here, v is the baseline position.
         #         etc.
         # (dviasm is useful to explore this structure.)
+        # Thus, we use the vertical position at the first time the stack depth
+        # reaches 3, while at least three "downs" have been executed, as the
+        # baseline (the "down" count is necessary to handle xcolor).
+        downs = 0
         self._baseline_v = None
         while True:
             byte = self.file.read(1)[0]
             self._dtable[byte](self, byte)
+            downs += self._dtable[byte].__name__ == "_down"
             if (self._baseline_v is None
-                    and len(getattr(self, "stack", [])) == 3):
+                    and len(getattr(self, "stack", [])) == 3
+                    and downs >= 4):
                 self._baseline_v = self.v
             if byte == 140:                         # end of page
                 return True

@darthoctopus
Copy link
Author

I can confirm that the patch fixes the issue for me. thanks for the quick response!

@anntzer anntzer added Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: has patch patch suggested, PR still needed labels Jan 4, 2021
@tacaswell
Copy link
Member

@darthoctopus Would you be interested in opening a PR with that patch and a test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. status: confirmed bug status: has patch patch suggested, PR still needed topic: text/usetex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants