Skip to content

Bug: Can't plot figures stretching over the 180 degree line? #2598

@465b

Description

@465b

I stumbled over this then my axis frames didn't cover the full figure anymore.
E.g. the following minimal example should stretch over the 180 degree line to show a map of New Zealand:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

projection = ccrs.epsg(2193)
fig = plt.figure()
fig.patch.set_facecolor('lightblue')  # Figure background for visualization purposes
ax = plt.subplot(111, projection=projection)  
ax.set_xlim(987000, 2500000)
ax.set_ylim(4700000, 6250000)
Image

This behavior doesn't occur if you stay west of the 180 degree line.

Next, I tried to use ax.set_extend instead and that just threw an error:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

projection = ccrs.epsg(2193)
fig = plt.figure()
ax = plt.subplot(111, projection=projection)  
ax.set_extent([987000, 2500000, 4700000, 6250000], crs=projection)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[95], [line 8](vscode-notebook-cell:?execution_count=95&line=8)
      6 fig.patch.set_facecolor('lightblue')  # Figure background
      7 ax = plt.subplot(111, projection=projection)  # Use subplot instead of axes
----> [8](vscode-notebook-cell:?execution_count=95&line=8) ax.set_extent([987000, 2500000, 4700000, 6250000], crs=projection)

File ~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/cartopy/mpl/geoaxes.py:841, in GeoAxes.set_extent(self, extents, crs)
    834 except ValueError:
    835     raise ValueError(
    836         'Failed to determine the required bounds in projection '
    837         'coordinates. Check that the values provided are within the '
    838         f'valid range (x_limits={self.projection.x_limits}, '
    839         f'y_limits={self.projection.y_limits}).')
--> [841](https://vscode-remote+ssh-002dremote-002bcawthron-005fhpc.vscode-resource.vscode-cdn.net/home/laurins/nz_plastic_dispersal/19288_PCEPlastics/visualization_and_dev_notebooks/~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/cartopy/mpl/geoaxes.py:841) self.set_xlim([x1, x2])
    842 self.set_ylim([y1, y2])

File ~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/matplotlib/axes/_base.py:3828, in _AxesBase.set_xlim(self, left, right, emit, auto, xmin, xmax)
   3826         raise TypeError("Cannot pass both 'right' and 'xmax'")
   3827     right = xmax
-> [3828](https://vscode-remote+ssh-002dremote-002bcawthron-005fhpc.vscode-resource.vscode-cdn.net/home/laurins/nz_plastic_dispersal/19288_PCEPlastics/visualization_and_dev_notebooks/~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/matplotlib/axes/_base.py:3828) return self.xaxis._set_lim(left, right, emit=emit, auto=auto)

File ~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/matplotlib/axis.py:1216, in Axis._set_lim(self, v0, v1, emit, auto)
   1213 name = self._get_axis_name()
   1215 self.axes._process_unit_info([(name, (v0, v1))], convert=False)
-> [1216](https://vscode-remote+ssh-002dremote-002bcawthron-005fhpc.vscode-resource.vscode-cdn.net/home/laurins/nz_plastic_dispersal/19288_PCEPlastics/visualization_and_dev_notebooks/~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/matplotlib/axis.py:1216) v0 = self.axes._validate_converted_limits(v0, self.convert_units)
   1217 v1 = self.axes._validate_converted_limits(v1, self.convert_units)
   1219 if v0 is None or v1 is None:
   1220     # Axes init calls set_xlim(0, 1) before get_xlim() can be called,
   1221     # so only grab the limits if we really need them.

File ~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/matplotlib/axes/_base.py:3749, in _AxesBase._validate_converted_limits(self, limit, convert)
   3746     converted_limit = converted_limit.squeeze()
   3747 if (isinstance(converted_limit, Real)
   3748         and not np.isfinite(converted_limit)):
-> [3749](https://vscode-remote+ssh-002dremote-002bcawthron-005fhpc.vscode-resource.vscode-cdn.net/home/laurins/nz_plastic_dispersal/19288_PCEPlastics/visualization_and_dev_notebooks/~/miniforge3/envs/oceantracker_plastic/lib/python3.13/site-packages/matplotlib/axes/_base.py:3749)     raise ValueError("Axis limits cannot be NaN or Inf")
   3750 return converted_limit

ValueError: Axis limits cannot be NaN or Inf

I assume this isn't intended behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions