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

triplot IndexError when lost marker and linestyle paraments #1060

Closed
purpleskyfall opened this issue Apr 17, 2018 · 8 comments
Closed

triplot IndexError when lost marker and linestyle paraments #1060

purpleskyfall opened this issue Apr 17, 2018 · 8 comments

Comments

@purpleskyfall
Copy link

purpleskyfall commented Apr 17, 2018

Description

In my case, using plt.triplot() function when lost marker and linestyle paraments will cause an error.

Code to reproduce

import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature

xy = np.asarray([
    [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890],
    [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898],
    [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919],
    [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949],
    [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959],
    [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965],
    [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980],
    [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996],
    [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021],
    [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005],
    [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987],
    [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968],
    [-0.020, 0.954], [-0.006, 0.947], [ 0.003, 0.935], [ 0.006, 0.926],
    [ 0.005, 0.921], [ 0.022, 0.923], [ 0.033, 0.912], [ 0.029, 0.905],
    [ 0.017, 0.900], [ 0.012, 0.895], [ 0.027, 0.893], [ 0.019, 0.886],
    [ 0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879],
    [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872],
    [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933],
    [-0.077, 0.990], [-0.059, 0.993]])

x = np.degrees(xy[:, 0])
y = np.degrees(xy[:, 1])

triangles = np.asarray([
    [67, 66,  1], [65,  2, 66], [ 1, 66,  2], [64,  2, 65], [63,  3, 64],
    [60, 59, 57], [ 2, 64,  3], [ 3, 63,  4], [ 0, 67,  1], [62,  4, 63],
    [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [ 4, 62, 68],
    [ 6,  5,  9], [61, 68, 62], [69, 68, 61], [ 9,  5, 70], [ 6,  8,  7],
    [ 4, 70,  5], [ 8,  6,  9], [56, 69, 57], [69, 56, 52], [70, 10,  9],
    [54, 53, 55], [56, 55, 53], [68, 70,  4], [52, 56, 53], [11, 10, 12],
    [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71],
    [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71],
    [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19],
    [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24],
    [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28],
    [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45],
    [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40],
    [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40],
    [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38],
    [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]])

fig = plt.figure(figsize=[8, 16])
ax = plt.axes(projection=ccrs.OSGB())
ax.add_feature(cfeature.OCEAN.with_scale('50m'))
ax.add_feature(cfeature.LAND.with_scale('50m'))
ax.gridlines(linestyle='--')
# plt.triplot(x, y, triangles, transform=ccrs.Geodetic(), marker='o', linestyle='-')
plt.triplot(x, y, triangles, transform=ccrs.Geodetic())
plt.show()

will case:

Traceback

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    239 
    240     if 'png' in formats:
--> 241         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    242     if 'retina' in formats or 'png2x' in formats:
    243         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    123 
    124     bytes_io = BytesIO()
--> 125     fig.canvas.print_figure(bytes_io, **kw)
    126     data = bytes_io.getvalue()
    127     if fmt == 'svg':

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2208                 bbox_filtered = []
   2209                 for a in bbox_artists:
-> 2210                     bbox = a.get_window_extent(renderer)
   2211                     if a.get_clip_on():
   2212                         clip_box = a.get_clip_box()

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\matplotlib\lines.py in get_window_extent(self, renderer)
    630         bbox = Bbox([[0, 0], [0, 0]])
    631         trans_data_to_xy = self.get_transform().transform
--> 632         bbox.update_from_data_xy(trans_data_to_xy(self.get_xydata()),
    633                                  ignore=True)
    634         # correct for marker size, if any

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\matplotlib\transforms.py in transform(self, values)
   1330 
   1331         # Transform the values
-> 1332         res = self.transform_affine(self.transform_non_affine(values))
   1333 
   1334         # Convert the result back to the shape of the input values.

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\matplotlib\transforms.py in transform_non_affine(self, points)
   2374             return points
   2375         elif not self._a.is_affine and self._b.is_affine:
-> 2376             return self._a.transform_non_affine(points)
   2377         else:
   2378             return self._b.transform_non_affine(

c:\users\asus\appdata\local\programs\python\python35\lib\site-packages\cartopy\mpl\geoaxes.py in transform_non_affine(self, xy)
    135         if isinstance(xy, np.ndarray):
    136             return prj.transform_points(self.source_projection,
--> 137                                         xy[:, 0], xy[:, 1])[:, 0:2]
    138         else:
    139             x, y = xy

lib\cartopy\_crs.pyx in cartopy._crs.CRS.transform_points()

IndexError: Out of bounds on buffer access (axis 0)

but when I add 2 paraments marker='o' and linestyle='-' in plt.triplot() function, it works:

plt.triplot(x, y, triangles, transform=ccrs.Geodetic(), marker='o', linestyle='-')

So, did I find a bug?

Full environment definition

Operating system

Windows 10, 1709, 64bit.

Cartopy version

CPython: 3.5.3
Cartopy: 0.16.0

pip list

Package             Version
------------------- -----------
aiofiles            0.3.1
aioftp              0.8.0
astroid             1.5.3
backcall            0.1.0
bleach              2.0.0
Cartopy             0.16.0
colorama            0.3.9
cycler              0.10.0
decorator           4.1.2
entrypoints         0.2.3
gnsscal             1.1.0
html5lib            0.999999999
ipykernel           4.6.1
ipython             6.3.1
ipython-genutils    0.2.0
ipywidgets          7.0.1
isort               4.2.15
jdcal               1.3
jedi                0.11.0
Jinja2              2.9.6
jsonschema          2.6.0
jupyter             1.0.0
jupyter-client      5.1.0
jupyter-console     5.2.0
jupyter-core        4.3.0
lazy-object-proxy   1.3.1
MarkupSafe          1.0
matplotlib          2.0.2
mccabe              0.6.1
mistune             0.7.4
mpmath              0.19
nbconvert           5.3.1
nbformat            4.4.0
notebook            5.1.0
numpy               1.14.1+mkl
pandas              0.20.3
pandocfilters       1.4.2
parso               0.1.0
pickleshare         0.7.4
Pillow              5.0.0
pip                 10.0.0
prompt-toolkit      1.0.15
Pygments            2.2.0
pylint              1.7.2
PyMySQL             0.7.11
pynmea2             1.12.0
pyparsing           2.2.0
pyproj              1.9.5.1
pyshp               1.2.12
python-dateutil     2.6.1
pytz                2017.2
PyYAML              3.12
pyzmq               16.0.2
qtconsole           4.3.1
scikit-learn        0.19.1
scipy               0.19.1
setuptools          38.5.1
Shapely             1.6.4.post1
simplegeneric       0.8.1
six                 1.11.0
sympy               1.1.1
testpath            0.3.1
tornado             4.5.2
tqdm                4.19.9
traitlets           4.3.2
wcwidth             0.1.7
webencodings        0.5.1
wheel               0.29.0
widgetsnbextension  3.0.3
win-unicode-console 0.5
wrapt               1.10.10
@dopplershift
Copy link
Contributor

Hmm. This worked fine for me (without the extra arguments) on Python 3.6, matplotlib 2.2.2, cartopy 0.16.0, Shapely 1.6.4 (all from conda-forge). What about you?

@purpleskyfall
Copy link
Author

I upgraded matplotlib from 2.0.2 to 2.2.2 from https://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib, but it not works with Python 3.5, cartopy 0.16.0, Shapely 1.6.4.

Also, I test this on another PC with Python 3.6.3, matplotlib 2.1.0, cartopy 0.16.0 & Shapely 1.6.4 (install by anaconda), the IndexError sitll happens.

Thanks, @dopplershift

@cgohlke
Copy link

cgohlke commented Apr 19, 2018

Works for me too. I can't reproduce the issue with binaries from https://www.lfd.uci.edu/~gohlke/pythonlibs

@purpleskyfall
Copy link
Author

For me, this worked fine in Python Shell but failed in Jupyter notebook, so could you test it in Jupyter notebook? Thanks. @cgohlke @dopplershift

@cgohlke
Copy link

cgohlke commented Apr 23, 2018

For me, this worked fine in Python Shell but failed in Jupyter notebook, so could you test it in Jupyter notebook?

Confirmed. I can reproduce the issue in Jupyter notebook.

@dopplershift
Copy link
Contributor

I can reproduce in the notebook as well. It looks like this is caused by the fact that the notebook calls savefig() with bbox_inches='tight'. A workaround is to disable this by putting this in a cell and running it before making the plot:

%config InlineBackend.print_figure_kwargs = {'bbox_inches':None}

This at least makes it work for me. I need to dig further to understand why it fails.

@dopplershift
Copy link
Contributor

Ok, so the problem is that without a marker, triplot ends up doing:

ax.plot([], [], **kw)

This yields a line with 0 points--just doing the above is enough to trigger the problem.

The code for transform_points() on CRS has one line that doesn't like this:

status = pj_transform(src_crs.proj4, self.proj4, npts, 3,
                      &result[0, 0], &result[0, 1], &result[0, 2])

(0 index on a size 0 axis is invalid). PR incoming...

dopplershift added a commit to dopplershift/cartopy that referenced this issue Apr 24, 2018
Empty arrays (i.e. one axis is size 0) were causing problems with
transform_points since it was explicitly asking for index 0. Wrapping
this in a check for the number of points seems to allow things to work
just fine.
@dopplershift
Copy link
Contributor

See #1062.

@dopplershift dopplershift added this to the 0.16.1 milestone Apr 24, 2018
dopplershift added a commit to dopplershift/cartopy that referenced this issue Jul 15, 2018
Empty arrays (i.e. one axis is size 0) were causing problems with
transform_points since it was explicitly asking for index 0. Wrapping
this in a check for the number of points seems to allow things to work
just fine.
@QuLogic QuLogic modified the milestones: 0.16.1, 0.17 Sep 27, 2018
greglucas pushed a commit to greglucas/cartopy that referenced this issue Sep 30, 2018
Empty arrays (i.e. one axis is size 0) were causing problems with
transform_points since it was explicitly asking for index 0. Wrapping
this in a check for the number of points seems to allow things to work
just fine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants