Skip to content

cartopy 0.22 + matplotlib 3.8 + contourf bug? #2288

@david-shu

Description

@david-shu

Description

After upgrading to Matplotlib3.8, it is suspected that there is a bug in cartopy 0.22 when drawing contourfs. Test code and draw results in test code and draw result

The problem seems to be here. when not force_path_ccw, got the right contourf.

class GeoAxes(matplotlib.axes.Axes):
    def contourf(self, *args, **kwargs):
            ##....................
            if isinstance(t, mtransforms.Transform):
                for sub_trans, _ in t._iter_break_from_left_to_right():
                    if isinstance(sub_trans, InterProjectionTransform):
                        if not hasattr(sub_trans, 'force_path_ccw'):
                            sub_trans.force_path_ccw = True
           ##.......................
    

The same code is drawn correctly on Matplotlib3.7.

the same data. but got different contourf. see

contourf result

Code to reproduce

import PyQt6.QtCore
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from urllib.request import urlopen


proj0 = ccrs.PlateCarree( central_longitude=0 )

x = np.load( urlopen( 'https://github.com/david-shu/cartopy-contourf-data/raw/main/x.dat' ) )
y = np.load( urlopen( 'https://github.com/david-shu/cartopy-contourf-data/raw/main/y.dat' ) )
v = np.load( urlopen( 'https://github.com/david-shu/cartopy-contourf-data/raw/main/v.dat' ) )

shape = ( 61,240 )

x = x.reshape( shape )
y = y.reshape( shape )
v = v.reshape( shape )

levels = [-4.0, -2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0, 4.0]

### contourf wrong ###### 
ax = plt.subplot( 2, 2, 1, projection=proj0 )
cs = ax.contourf( x, y, v,
                  levels=levels,
                  extend='both',
                  transform_first=False
                  )
plt.colorbar( cs )
ax.set_title('PlateCarree + extend both' )
###########################


ax = plt.subplot( 2, 2, 2, projection=proj0 )
cs = ax.contourf( x, y, v,
                  levels=levels,
                  extend='min',
                  transform_first=False
                  )
plt.colorbar( cs )
ax.set_title('PlateCarree + extend min' )


ax = plt.subplot( 2, 2,3, projection=proj0 )
cs = ax.contourf( x, y, v,
                  levels=levels,
                  extend='both',
                  transform_first=True
                  )
plt.colorbar( cs )
ax.set_title('PlateCarree + transform_first' )

ax = plt.subplot( 2, 2, 4 )
cs = ax.contourf( x, y, v,
                  levels=levels,
                  extend='both',
                  )
plt.colorbar( cs )
ax.set_title('Axes' )

plt.show()
Full environment definition

Operating system

win10 / centos

Cartopy version

cartopy 0.22.0
cartopy 0.21.1

matplotlib version

matplotlib 3.8.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions