SciQLop / speasy

Space Physics made EASY! A simple Python package to deal with main Space Physics WebServices (CDA,SSC,AMDA,..)
Other
24 stars 7 forks source link

Issue with SpeasyVariable.plot() #59

Closed cmoissar closed 1 year ago

cmoissar commented 1 year ago

Description

Reproduce AMDA First Steps.

What I Did

Copy and paste the first steps from the link above.

Output:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_17102/2607499050.py in <module>
      1 plt.figure()
      2 ace_mag = spz.get_data(amda_tree.Parameters.ACE.MFI.ace_imf_all.imf, datetime(2016,6,2), datetime(2016,6,5))
----> 3 ace_mag.plot()
      4 plt.tight_layout()
      5 plt.show()

~/.local/lib/python3.10/site-packages/speasy/plotting/__init__.py in __call__(self, backend, *args, **kwargs)
     69         if self._infer_plot_type() == PlotType.SPECTRO:
     70             return self.colormap(backend=backend, *args, **kwargs)
---> 71         return self.line(backend=backend, *args, **kwargs)
     72 
     73     def __getitem__(self, item):

~/.local/lib/python3.10/site-packages/speasy/plotting/__init__.py in line(self, backend, *args, **kwargs)
     46         units = self.values.unit
     47         yaxis_label = self.values.name
---> 48         return self._get_backend(backend).line(x=self.axes[0].values, y=self.values, labels=self.columns_names,
     49                                                units=units,
     50                                                xaxis_label=self.axes[0].name,

~/.local/lib/python3.10/site-packages/speasy/plotting/mpl_backend/__init__.py in line(self, x, y, ax, labels, units, xaxis_label, yaxis_label, *args, **kwargs)
     20         ax = self._get_ax(ax)
     21         ax.tick_params(axis='x', labelrotation = 45)
---> 22         ax.plot(x, y)
     23         if labels is not None:
     24             ax.legend(labels)

/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1630         """
   1631         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1632         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1633         for line in lines:
   1634             self.add_line(line)

/usr/lib/python3/dist-packages/matplotlib/axes/_base.py in __call__(self, data, *args, **kwargs)
    310                 this += args[0],
    311                 args = args[1:]
--> 312             yield from self._plot_args(this, kwargs)
    313 
    314     def get_next_color(self):

/usr/lib/python3/dist-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs, return_kwargs)
    486         if len(xy) == 2:
    487             x = _check_1d(xy[0])
--> 488             y = _check_1d(xy[1])
    489         else:
    490             x, y = index_of(xy[-1])

/usr/lib/python3/dist-packages/matplotlib/cbook/__init__.py in _check_1d(x)
   1325                     message='Support for multi-dimensional indexing')
   1326 
-> 1327                 ndim = x[:, None].ndim
   1328                 # we have definitely hit a pandas index or series object
   1329                 # cast to a numpy array.

AttributeError: 'DataContainer' object has no attribute 'ndim'

Summary

Everything works great and I really wish I had access to such a great tool 5 years ago at the start of my PhD. But the .plot() function a speasy.products.variable.SpeasyVariable doesn't seem to work on my laptop. Is it only me?

Best wishes, Clement

jeandet commented 1 year ago

@cmoissar, which version of matplotlib are you using? and also are you using the latest version of speasy (1.0.2)? The code you are using should definitely be working.

cmoissar commented 1 year ago

Matplotlib 3.5.1 Speasy 1.02

jeandet commented 1 year ago

Ok, I can reproduce it, this seems to be fixed with matplotlib 3.5.2+. I'm not 100% sure but this https://github.com/matplotlib/matplotlib/pull/22592/ could be related. The quickest fix on your side is I think to update matplotlib. On speasy side I will check if it makes more sense to support matplotlib 3.5.1- or or make speasy only compatible with matplotlib>= 3.5.2.

jeandet commented 1 year ago

@cmoissar I published the 1.0.3 release with the fix for matplotlib<= 3.5.1.

cmoissar commented 1 year ago

Thanks @jeandet !