3

I have an xarray DataArray da containing a slice of data for Ireland which looks like this:

<xarray.DataArray 'co2' (lat: 733, lon: 720)>
array([[nan, nan, nan, ..., nan, nan, nan],
   [nan, nan, nan, ..., nan, nan, nan],
   [nan, nan, nan, ..., nan, nan, nan],
   ...,
   [nan, nan, nan, ..., nan, nan, nan],
   [nan, nan, nan, ..., nan, nan, nan],
   [nan, nan, nan, ..., nan, nan, nan]])
Coordinates:
  * lat      (lat) float32 49.9 49.908333 49.916664 49.924995 49.933327 ...
  * lon      (lon) float32 -11.0 -10.991667 -10.983334 -10.975 -10.966667 ...

I can map it like so:

import matplotlib.pyplot as plt
import xarray
import os
from mpl_toolkits.basemap import Basemap, cm

m= Basemap(projection='cyl',lat_0=ds.co2.lat[0],lon_0=ds.co2.lon[len(ds.co2.lon)/2])
m.drawcoastlines()
da.plot()

The problem is that lat/lon gridlines don't plot.

enter image description here

When I use the meridians command:

meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])

I get the following error:

ValueError: dimensions () must have the same length as the number of data dimensions, ndim=1

I do not know what to try next.

EDIT: Full error trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-46-45a293c8bb99> in <module>()
  4 
  5 # draw grid plots
----> 6 m.drawmeridians(np.arange(-8.0,2.0,1.0),labels=[1,0,0,0]) #longitudes
      7 m.drawparallels(np.arange(51.0,59.0,1.0),labels=[0,0,0,1]) #latitudes
      8 

C:\Users\AppData\Local\Continuum\Anaconda\lib\site-    packages\mpl_toolkits\basemap\__init__.pyc in drawmeridians(self, meridians, color, linewidth, zorder, dashes, labels, labelstyle, fmt, xoffset, yoffset, ax, latmax, **kwargs)
   2593             # don't really know why, but this appears to be needed to
   2594             # or lines sometimes don't reach edge of plot.
-> 2595             testx = np.logical_and(x>=self.xmin-3*xdelta,x<=self.xmax+3*xdelta)
   2596             x = np.compress(testx, x)
   2597             y = np.compress(testx, y)

C:\Users\\AppData\Local\Continuum\Anaconda\lib\site-packages\xarray\core\dataarray.pyc in func(self, other)
   1550 
   1551             variable = (f(self.variable, other_variable)
-> 1552                         if not reflexive
   1553                         else f(other_variable, self.variable))
   1554             coords = self.coords._merge_raw(other_coords)

C:\Users\\AppData\Local\Continuum\Anaconda\lib\site-packages\xarray\core\variable.pyc in func(self, other)
   1164                         if not reflexive
   1165                         else f(other_data, self_data))
-> 1166             result = Variable(dims, new_data)
   1167             return result
   1168         return func

C:\Users\\AppData\Local\Continuum\Anaconda\lib\site-packages\xarray\core\variable.pyc in __init__(self, dims, data, attrs, encoding, fastpath)
    255         """
    256         self._data = as_compatible_data(data, fastpath=fastpath)
--> 257         self._dims = self._parse_dimensions(dims)
    258         self._attrs = None
    259         self._encoding = None

C:\Users\\AppData\Local\Continuum\Anaconda\lib\site-packages\xarray\core\variable.pyc in _parse_dimensions(self, dims)
    364             raise ValueError('dimensions %s must have the same length as the '
    365                              'number of data dimensions, ndim=%s'
--> 366                              % (dims, self.ndim))
    367         return dims
    368 

ValueError: dimensions () must have the same length as the number of data dimensions, ndim=1
Pad
  • 841
  • 2
  • 17
  • 45
  • Could it be that your grid is too sparse and none of the meridians actually pass through the plotted area? – Thomas Kühn Oct 26 '18 at 11:36
  • @Thomas I don't think so, I tried `# draw grid plots m.drawmeridians(np.arange(-8.0,2.0,1.0),labels=[1,0,0,0]) #longitudes m.drawparallels(np.arange(51.0,59.0,1.0),labels=[0,0,0,1]) #latitudes` and got the same error – Pad Oct 26 '18 at 14:41
  • Can you post the full error trace? – Thomas Kühn Oct 26 '18 at 17:44
  • full error trace added! – Pad Oct 27 '18 at 10:21
  • Hmm this actually plots just fine for me: `m = Basemap(projection='cyl', lat_0=ds.co2.lat[0], lon_0=ds.co2.lon[len(ds.co2.lon)//2]); m.drawcoastlines(); ds.co2.plot(cmap='viridis'); m.drawmeridians(np.arange(-8.0,2.0,1.0),labels=[1,0,0,0]); #longitudes m.drawparallels(np.arange(51.0,59.0,1.0),labels=[0,0,0,1]); #latitudes`. Maybe try updating everything and make sure m is the Basemap object? – Michael Delgado Oct 28 '18 at 19:14
  • Still getting the same error, I updated Basemap and it started introducing new errors (apparently it doesn't work well with jupyter notebooks) so I had to revert back. – Pad Oct 29 '18 at 12:51
  • can you somehow share a pickle file of the data? – Shir Nov 04 '18 at 15:10
  • @shir I can email you a link to the data? It is over 2Gb in size. – Pad Nov 05 '18 at 09:29
  • 3
    Can you maybe send only a little part of it please? :) I guess the problem would exist with 1 MB too :) – Shir Nov 05 '18 at 15:23
  • I would second @Shir request for a minimum working example with data included. If you have the same issue with just a few points then include them here - that way people can confirm if they have the same issue, and try out their ideas to fix it. – kabdulla Nov 05 '18 at 22:24
  • It actually works for me using your small dataset. Can you try it with it and tell me if it works? – Shir Nov 06 '18 at 07:42

2 Answers2

3

Try to use cartopy instead of Basemap. See related issue here.

Ales
  • 495
  • 3
  • 11
  • Thanks, but this is giving me the error `ImportError: DLL load failed: The specified module could not be found.` - I reinstalled all the packages and updated them, error persists :( – Pad Nov 05 '18 at 15:25
  • Trying Python 3.6 I get the error `ValueError: Can't use axes when making faceted plots.` – Pad Nov 05 '18 at 16:08
1

TL:DR- I didn't have a problem using your code with your dataset, let's find out why

I used your small dataset, and this code:

ds=xarray.open_dataset(r"C:\Users\SHIR\Downloads\OneYear.nc")
da=ds.co2
m= Basemap(projection='cyl',lat_0=ds.co2.lat[0],lon_0=ds.co2.lon[len(ds.co2.lon)/2])
m.drawcoastlines()
da.plot()
plt.show()

I got this graph:

enter image description here

When adding the meridians, using:

ds=xarray.open_dataset(r"C:\Users\SHIR\Downloads\OneYear.nc")
da=ds.co2
m= Basemap(projection='cyl',lat_0=ds.co2.lat[0],lon_0=ds.co2.lon[len(ds.co2.lon)/2])
m.drawcoastlines()
meridians = np.arange(10.,351.,20.)
m.drawmeridians(meridians,labels=[True,False,False,True])
da.plot()
plt.show()

I got-

enter image description here

Things that I can think of that cause this difference between us:

First- The smaller dataset- please try the smaller dataset you sent me, and let me know if you get the error again

Second- Packages and versions- I'm using python 2.7. I didn't have basemap before, so I tried to install it using conda, and had tons of problems. In the end, I uninstalled matplotlib using conda (conda uninstall matplotlib), reinstalled it using pip (pip install matplotlib --upgrade --force-reinstall), and installed basemap manually like described in this answer. I used the basemap‑1.2.0‑cp27‑cp27m‑win_amd64.whl file.

I'm really not sure it was smart, and I didn't mess things up with conda, but that was the only thing that worked for me. Maybe try to uninstall only basemap, not matplotlib first (I did it because I already messed things up with it...)

Shir
  • 1,157
  • 13
  • 35
  • 1
    Thank you so much for this. I think you're right about the package issue, Basemap seems to bring everything else backwards, which is an issue for other scripts I run also. I might avoid Basemap completely to avoid screwing up other code! Your answer answers my question however! – Pad Nov 06 '18 at 10:04