-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Setting plt.rc('text', usetex=True) after ticker.ScalarFormatter(useMathText=True) causes Error #10317
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
Comments
While an ad-hoc fix may be possible, I think this is a good opportunity to get rid of \mathdefault and replace it by whatever tex package does the same thing (write math using the externally specified non-math font), e.g. mathastext (https://tex.stackexchange.com/a/39520/4101) or the solution hinted at in https://tex.stackexchange.com/questions/22350/difference-between-textrm-and-mathrm/22353#22353 (i.e. actually define \mathdefault in our tex preamble. Other possibly relevant stuff: |
@svrnwnsch do you solve this problem? I have the same problem like you. |
I think it worked for me to switch the commands. |
@svrnwnsch do you mean to change usetex=False or the Formatter of ticker ? |
@huxiaoqian I mean that I called |
I think that @kathreen8 is seeing a similar issue where |
Note that this issue does not occur for matplotlib version 1.5.3, but I do see it with 2.2.2. |
A look in the source code (ticker.py) shows, that the Consequently, if a |
I originally came here, because I encountered the same error in a different setting. I now found, that my case is also caused by the inconsistent request of the The following code generates the same error as for @svrnwnsch: import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
fig, ax = plt.subplots(1, 1)
ax.set_xscale('log')
fig.savefig('test_tex.png')
plt.rcParams['text.usetex'] = False This happens for matplotlib 2.2.4 (Win7) and 3.0.3 (Win10) with Python 3.6 and Qt5Agg backend. The interesting part is, that the error vanishes, if the figure is not saved (remove the So I think there are two inconsistencies:
Maybe the state of whether to use or not use TeX for a figure should be set as a property of the figure, when the backend starts displaying it. All texts then refer to the value of the parent figure instead of the global state. The problem with formatters is that they do not know their parent figure or can be used in multiple figures. The only solution I can think of is to add a |
For the reference, two following examples (both without ticker and saving figure) produce similar error in my environments. no. 1 # Minimal fail example:
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['text.usetex'] = True
df = pd.DataFrame({'a_b': [1,2,3]})
df.plot(); no. 2 # example without pandas
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['text.usetex'] = True
# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)
fig, ax = plt.subplots()
ax.plot(t, s)
ax.set(xlabel='time (s)', ylabel='voltage (mV)',
title=r'About as simple as it gets,_folks')
ax.grid()
plt.show() |
In the second case, |
Thanks for the answer! Is it because of the unescaped underscore? If so, the same then applies to the first example then. It doesn't seem to be because of the 'rawness' of the string, as the snippet fails without the 'r' prefix too. |
Bug report
Bug summary
If I call
plt.rc('text', usetex=True)
afterticker.ScalarFormatter(useMathText=True)
and plot a image with a colorbar. The plot will not work and a Latex runtime error is thrown.If you switch the commands everything works fine.
Code for reproduction
Actual outcome
Matplotlib version
print(matplotlib.get_backend())
): TkAggThe text was updated successfully, but these errors were encountered: