Skip to content

Fix .plot() method for 1D data groups with no nxcoordinates#250

Merged
rayosborn merged 1 commit intonexpy:mainfrom
stevenjgomez:main
Feb 2, 2026
Merged

Fix .plot() method for 1D data groups with no nxcoordinates#250
rayosborn merged 1 commit intonexpy:mainfrom
stevenjgomez:main

Conversation

@stevenjgomez
Copy link
Contributor

The recent addition of nxcoordinate logic to the plot.py module has broken some downstream behavior, in my case for the .plot() method of 1D data groups. The following is a minimal example to recreate the issue:

from nexusformat.nexus import *
import numpy as np

# Make 1D Gaussian
x = np.linspace(-10,10,51)
gaussian = np.exp(-x**2 / (2 * 1.0**2))

# Make NXaxes
X = NXfield(x, name='Qh')

# Make 1D NXdata
data_1D = NXdata(signal=gaussian, axes=X)

# Try to plot
data_1D.plot()

This raises the following:

Traceback (most recent call last):
  File "/Users/stevengomezalvarado/nxs_analysis_tools/scratch/reproduce_issue.py", line 15, in <module>
    data_1D.plot()
    ~~~~~~~~~~~~^^
  File "/Users/stevengomezalvarado/miniconda3/envs/nxs/lib/python3.13/site-packages/nexusformat/nexus/tree.py", line 7627, in plot
    plotview.plot(self, fmt, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax,
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                  vmin=vmin, vmax=vmax, **kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/stevengomezalvarado/miniconda3/envs/nxs/lib/python3.13/site-packages/nexusformat/nexus/plot.py", line 273, in plot
    y = boundaries(axes[-2], data.shape[-2])
                   ~~~~^^^^
IndexError: list index out of range

The plot.py module contains the following if/else logic:

class PyplotPlotter:

    def plot(self, data_group, fmt=None, xmin=None, xmax=None,
             ymin=None, ymax=None, vmin=None, vmax=None, **kwargs):
        
        # Some code...

        try:
            
            # One-dimensional Plot
            if len(data.shape) == 1 and len(coordinates) == 0:
                # Some code
            
            if len(data.shape) == 1 and len(coordinates) > 0:
                # Some code
            
            # Two-dimensional plot
            else:
                # Some code

My assumption is that this if/if/else chain is actually meant to be an if/elif/else chain, which appears to resolve the issue and results in the expected behavior.

class PyplotPlotter:

    def plot(self, data_group, fmt=None, xmin=None, xmax=None,
             ymin=None, ymax=None, vmin=None, vmax=None, **kwargs):
        
        # Some code...

        try:
            
            # One-dimensional Plot
            if len(data.shape) == 1 and len(coordinates) == 0:
                # Some code
            
            elif len(data.shape) == 1 and len(coordinates) > 0:
                # Some code
            
            # Two-dimensional plot
            else:
                # Some code

Let me know your thoughts.

@rayosborn
Copy link
Contributor

Thank you for the PR, which looks sound. It's a while since I made this change so I'm not sure why this problem has not appeared before but I confirmed that your fix works.

@rayosborn rayosborn merged commit c61244b into nexpy:main Feb 2, 2026
15 checks passed
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

Successfully merging this pull request may close these issues.

2 participants