pythonmatplotlibplotcontourcontourf

Python matplotlib contourf hatches in black instead of white


I am trying to plot a significance mask over a variable, using hatches in contourf.

I have not managed to change the colour of the hatches from white to black (wanted). I have tried multiple combinations of edgecolor, facecolor, color and contourf and contour code. Any help would be greatly appreciated!

Here is my code:

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import cartopy.crs as ccrs
from cartopy.feature import NaturalEarthFeature


all_plots = []
plt.rcParams.update({'hatch.color': 'k'})

land = NaturalEarthFeature('physical', 'land', '50m', edgecolor='face', facecolor='white')

# Create a 2x2 subplot (2 rows, 2 columns) for 4 plots, with Miller projection
fig, axs = plt.subplots(2, 2, figsize=(18, 9), subplot_kw={'projection': ccrs.Miller()})
plt.subplots_adjust(hspace=0.25, wspace=0.1)  # Adjust spacing

# Shared colorbar arguments
cmap = 'RdBu_r'

# Function to set up common gridlines and features
def setup_ax(ax, title):
    ax.coastlines(zorder=2)
    ax.add_feature(land, zorder=1)
    ax.set_extent([-180, 180, -1, 90], crs=ccrs.PlateCarree())  # Limit y-axis to 0-90 degrees
    gl = ax.gridlines(draw_labels=True, linewidth=0.6, zorder=1, color='dimgray', alpha=0.3)
    gl.left_labels = True
    gl.bottom_labels = True
    gl.top_labels = False
    gl.right_labels = False
    gl.xlabel_style = {'size': 10}
    gl.ylabel_style = {'size': 10}
    gl.ylocator = plt.FixedLocator(np.arange(0, 91, 20))  # Add y-axis gridlines at 0°, 20°, 40°, etc.
    gl.xlocator = plt.FixedLocator(np.arange(-180, 181, 60))  # Optional: adjust x-axis labels
    ax.set_title(title, fontsize=15)

# Plot 1: Strongest months of hailseason (May-August)
im = mean_sst_strongest_months_north.plot(ax=axs[0, 0], cmap=cmap, add_colorbar=False, 
vmin=vmin, vmax=vmax, transform=ccrs.PlateCarree())
# Overlay the significance mask for the first plot
cax1 = axs[0, 0].contourf(significant_mask_months_strongest_north.longitude, 
                      significant_mask_months_strongest_north.latitude, 
                      significant_mask_months_strongest_north, levels=[0.5, 1], 
                      colors='none', hatches=['oo'], transform=ccrs.PlateCarree())
setup_ax(axs[0, 0], 'Strongest months of hailseason (May-Aug)')
for ax in axs.flat:
    rect = Rectangle((5.5, 45.3), 5.4, 2.7, linewidth=1.5, edgecolor='limegreen', 
facecolor='none', transform=ccrs.PlateCarree())
    ax.add_patch(rect)

[... other 3 plots]

# Add a shared colorbar at the bottom
cbar = fig.colorbar(im, ax=axs, pad=0.06, orientation="horizontal", shrink=0.7, 
extend="both", aspect=50)
cbar.set_label('SST anomalies [K]', fontsize=15)

# Add a main title for the figure
fig.suptitle('Mean SST anomalies of 10 strongest hailyears in northern Switzerland', 
fontsize=18, y=0.97)

# Save the plot to the specified directory
plt.savefig("/sst_strongest_4periods_north_significance_95.jpeg", dpi=350, 
bbox_inches='tight', pad_inches=0.05)

# Close all open plots
plt.close('all')

And how the plot looks now:

enter image description here


Solution

  • eцрвене звезде

    You can directly manipulate a "hidden" property of the contour object

    import matplotlib.pyplot as plt
    import numpy as np
    
    # we'll plot circles
    x = y = range(-5, 6)
    x, y = np.meshgrid(x, y)
    z = np.sqrt(x*x+y*y)
    
    fig, ax = plt.subplots()
    
    # circles, not ellipses
    ax.set_aspect(1)
    
    # first the contours
    c0 = ax.contour(x, y, z, levels=range(7))
    # next a single filled contour
    c1 = ax.contourf(x, y, z,
        levels=[3, 4], colors='w', hatches=['**'])
    # you are not supposed to do this
    c1._hatch_color = (1, .4, .4, 1.0)
    
    plt.show()