Skip to content

Confusing error on fully commented-out usetex strings #16409

Closed

Description

@cgalleguillosm

Bug report

I was trying to generate some figures using the latex fonts (rc('text', usetex=True)), but I always get an error (I also tried on linux). If I set usetex=False, no error is raised and the figure is properly generated, even the tex characters are generated.

If the figure is not composed by subplots, no error is raised, so, I think is something between subplots and tex.

Code for reproduction

from matplotlib import rc, gridspec
import matplotlib.pyplot as plt
from numpy.random.mtrand import randint
from numpy import linspace
import matplotlib.ticker as mtick

fig = plt.figure(tight_layout=True, figsize=(9, 6))
rc('text', usetex=True)
gs = gridspec.GridSpec(2, 2)
axs = [ fig.add_subplot(gs[0, :]), fig.add_subplot(gs[1, 0]), fig.add_subplot(gs[1, 1])]
colors = ['royalblue', 'darkorange']

all_names = [['TEXT$_1$-A', 'TEXT$_1$-B'], ['TEXT$_2$-A', 'TEXT_2-B']]

for names in all_names:
    y_plot = randint(-50, 50, 5000)
    x_plot = linspace(0, 100, len(y_plot))
    
    ax = axs[0]
    ax.plot(x_plot, y_plot, label='{} / {}'.format(*names) if len(all_names) > 1 else None)
    
    ax.set_xlabel("% of instances")
    ax.set_yscale('symlog')
    
    ax.set_ylabel('ratio'.format(*names))
    ax.legend(loc='upper right')

    ax.xaxis.set_major_locator(mtick.MultipleLocator(25))
    ax.xaxis.set_minor_locator(mtick.MultipleLocator(5))
    ax.xaxis.set_major_formatter(mtick.PercentFormatter(100))
    ax.grid(True, which='Major', axis='both', linestyle='--', color='silver')
    
    ax = axs[1]
    ax.plot(x_plot, y_plot, label=r'{} / {}'.format(*names) if len(all_names) > 1 else None)
    ax.set_xlim(0, 5)    
    ax.grid(True, which='both', axis='both', linestyle='--', color='silver')
    ax.set_yscale('linear')

    ax = axs[2]
    ax.plot(x_plot, y_plot, label=r'{} / {}'.format(*names) if len(all_names) > 1 else None)
    ax.set_yscale('log')
    ax.set_xlim(95, 100)
    ax.grid(True, which='Major', axis='both', linestyle='--', color='silver')
    ax.xaxis.set_major_locator(mtick.MultipleLocator(5))
    ax.xaxis.set_minor_locator(mtick.MultipleLocator(1))
    ax.xaxis.set_major_formatter(mtick.PercentFormatter(100))
    ax.xaxis.set_minor_formatter(mtick.PercentFormatter(100))

fig.align_labels()
plt.tight_layout()
plt.show()

Actual outcome

Traceback (most recent call last):
  File "C:\research\accasim-tests\new_model\analysis\tex_error.py", line 52, in <module>
    plt.tight_layout()
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\pyplot.py", line 1368, in tight_layout
    gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\figure.py", line 2476, in tight_layout
    pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\tight_layout.py", line 362, in get_tight_layout_figure
    pad=pad, h_pad=h_pad, w_pad=w_pad)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\tight_layout.py", line 111, in auto_adjust_subplotpars
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\tight_layout.py", line 112, in <listcomp>
    if ax.get_visible()])
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\axes\_base.py", line 4357, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\axis.py", line 1178, in get_tightbbox
    *ticklabelBoxes2,
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\axis.py", line 1176, in <genexpr>
    if a.get_visible()),
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\text.py", line 890, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\text.py", line 298, in _get_layout
    clean_line, self._fontproperties, ismath=ismath)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\backends\backend_agg.py", line 201, in get_text_width_height_descent
    s, fontsize, renderer=self)
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\texmanager.py", line 449, in get_text_width_height_descent
    with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
  File "C:\Program Files\Python36\lib\site-packages\matplotlib\dviread.py", line 198, in __init__
    self.file = open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\cgall\\.matplotlib\\tex.cache\\aee1b2d0939944ebcda4231dd9a1549a.dvi'

Matplotlib version

  • Operating system: Win 10
  • Matplotlib version: 3.1.2
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.6
  • Other libraries: numpy 1.16.3

Matplotlib installed via pip command

Activity

  1. anntzer commented on Feb 4, 2020

    @anntzer
    Contributor

    Actually the problem is unrelated to subplots, but rather to the fact that your xlabel is an empty string (because it starts with a LaTeX comment, "%"). Fixing that, as well as the missing $$ in TEXT_2-B, fixes the issue.
    Still, we should handle (effectively) empty latex inputs more cleanly...

  2. cgalleguillosm commented on Feb 4, 2020

    @cgalleguillosm
    Author

    I completely forgot the % in the xlabel. The TEXT_2-B was on purpose (I was expecting a tex compilation error).
    Thanks a lot

  3. anntzer commented on Feb 4, 2020

    @anntzer
    Contributor

    Let's keep this open to make the error less confusing.

  4. reopened this on Feb 4, 2020
  5. changed the title [-]Incompatibility of subplots and tex [/-] [+]Confusing error on fully commented-out usetex strings[/+] on Feb 7, 2020
  6. added this to the v3.2.2 milestone on Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions