Skip to content
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

IndexError when using plot() or scatter() with values that cross 0 using ATLAS style #512

Open
Benkendorfer opened this issue Aug 8, 2024 · 1 comment

Comments

@Benkendorfer
Copy link
Contributor

I have encountered a strange crash when I try to use axes with the ATLAS style. The following MWE

from matplotlib import gridspec, pyplot as plt
import mplhep as hep

x_vals = [0, 1]
y_vals = [1, -1] # changing y_vals to not cross 0 stops error

hep.style.use(hep.style.ATLAS) # necessary for error

fig = plt.figure(figsize=(6, 5)) # changing figsize stops error
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1]) # changing height_ratios stops error
ax1 = plt.subplot(gs[0])

ax2 = plt.subplot(gs[1])
ax2.plot(x_vals, y_vals) # also occurs with scatter()

plt.show()

produces an IndexError

Traceback (most recent call last):
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 1233, in _on_timer
    ret = func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/backends/backend_macosx.py", line 68, in callback_func
    callback()
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/backends/backend_macosx.py", line 90, in _draw_idle
    self.draw()
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/backends/backend_macosx.py", line 53, in draw
    super().draw()
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 388, in draw
    self.figure.draw(self.renderer)
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/figure.py", line 3154, in draw
    mimage._draw_list_compositing_images(
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3012, in draw
    self._unstale_viewLim()
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 844, in _unstale_viewLim
    self.autoscale_view(**{f"scale{name}": scale
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 2939, in autoscale_view
    handle_single_axis(
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 2932, in handle_single_axis
    x0, x1 = locator.view_limits(x0, x1)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/ticker.py", line 2168, in view_limits
    return self._raw_ticks(dmin, dmax)[[0, -1]]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/keesbenkendorfer/miniconda3/envs/ttZ_MVA/lib/python3.11/site-packages/matplotlib/ticker.py", line 2112, in _raw_ticks
    istep = np.nonzero(large_steps)[0][0]
            ~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: index 0 is out of bounds for axis 0 with size 0

I made the following observations:

  • The error does not occur if I comment out the line hep.style.use(hep.style.ATLAS)
  • The error only occurs when y_vals has both positive and negative values
  • The error does not occur if I change the height ratios to height_ratios=[2, 1] instead of height_ratios=[3, 1]
  • The error does not occur if I change the figsize to figsize=(6, 6) instead of figsize=(6, 5)
  • The error only occurs when I use multiple axes
  • The error also occurs when using scatter() instead of plot()

I guess somehow there is a sizing problem that messes with auto-tick placement in the ATLAS style. Perhaps a secret logarithmic transformation is being applied? It seems a bit deeper than I can dig into at this moment.

@andrzejnovak
Copy link
Member

That seems rather quirky. hep.style.ATLAS is just a dictionary of standard mpl rc options, so this seems almost like a pure mpl issue. When you have a moment, could you loop through the settings in hep.style.ATLAS and apply them selectively to the demo with https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.rc_context.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants