CDAT / Jupyter-notebooks

1 stars 0 forks source link

Add script showing how to use raw=True before calling matplotlib function #14

Open jypeter opened 5 years ago

jypeter commented 5 years ago

Once again, I had to convert an MV2 array to numpy.ma array before calling matplotlib, using the possibly under-documented raw=1 feature (https://github.com/CDAT/cdms/issues/338)

I was lucky to remember that trick, because it was either that or deal with a quite obscure and unrelated matplotlib traceback

>>> type(pr_rmin)
<class 'cdms2.tvariable.TransientVariable'>

>>> diff_plot.plot(x_lats, pr_rmin)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/share/unix_files/cdat/miniconda3/envs/cdatm_py2/lib/python2.7/site-packages/matplotlib/__init__.py", line 1867, in inner
    return func(ax, *args, **kwargs)
  File "/home/share/unix_files/cdat/miniconda3/envs/cdatm_py2/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 1528, in plot
    for line in self._get_lines(*args, **kwargs):
  File "/home/share/unix_files/cdat/miniconda3/envs/cdatm_py2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 406, in _grab_next_args
    for seg in self._plot_args(this, kwargs):
  File "/home/share/unix_files/cdat/miniconda3/envs/cdatm_py2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 391, in _plot_args
    ncx, ncy = x.shape[1], y.shape[1]
IndexError: tuple index out of range

>>> diff_plot.plot(x_lats, pr_rmin(raw=True))
[<matplotlib.lines.Line2D object at 0x2b038d6eb210>]

Or maybe there is an easy fix in the cdms2 (masked) variables to make them compatible with matplotlib?

Anyway it's probably easy and useful to create a short notebook to illustrate this

doutriaux1 commented 5 years ago

@jypeter what about numpy_var = cdmsvar.asma() if masked or numpy_var = cdms_var.filled(-999) if no missing needed.

jypeter commented 5 years ago

asma() sounds nice, but there is little documentation for it. I searched the cdms2 documentation and the only useful ref I found was in cdms2.tvariable module which may not be a place that an end user is likely to visit

As a matter of fact, I had never been on that page! Should there be some kind of link to this from 2.3. Cdms Module ?

And maybe asma should also be documented in 2.3. Cdms Module

I have extracted below a page from my old 2007 CDAT tutorial. It's in French (@dnadeau4 that's for you!), but it shows how the different data objects from files/python/Numeric/CDAT relate to each other, and which functions you can use to go from one type of data to the other. Maybe there could be a similar (and updated!) Data Overview Page (a good DOPe) on the CDAT site? Showing that asma can be used to go from and MV2 var to a numpy.ma var

MiscDataObjectsByJYP