0

Hatching don't work on the last plot in a panel plot. The first and second plots are ploted fine, but the last plot doesn't work. The "DJF_sig" variable is fine, I have attached a previously failed plot where the statistically significant areas are colored yellow. But I don't know why it doesn't work. If anyone knows why, please let me know. enter image description here

import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
import sys
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon

file_path = './Diff_COBALT_DAILY_DJF_JJA.nc'
a_path='./signi_COBALT_DAILY_DJF_JJA.nc'
ds = nc.Dataset(file_path)
ds_a=nc.Dataset(a_path)
annual_sig=ds_a.variables['annual_sig2'][:]
DJF_sig=ds_a.variables['DJF_sig2'][:]
JJA_sig=ds_a.variables['JJA_sig2'][:]

annual = ds.variables['tos_diff_annual'][:]
DJF = ds.variables['tos_diff_DJF'][:]
JJA = ds.variables['tos_diff_JJA'][:]
yh=ds.variables['yh'][:]
xh=ds.variables['xh'][:]

an_sig_grid = np.where(annual_sig == 2)
an_sig_lon = xh[an_sig_grid[1]]
an_sig_lat = yh[an_sig_grid[0]]

DJF_sig_grid = np.where(DJF_sig == 2)
DJF_sig_lon = xh[DJF_sig_grid[1]]
DJF_sig_lat = yh[DJF_sig_grid[0]]

JJA_sig_grid = np.where(JJA_sig == 2)
JJA_sig_lon = xh[JJA_sig_grid[1]]
JJA_sig_lat = yh[JJA_sig_grid[0]]
xh, yh = np.meshgrid(xh, yh)

fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(12, 12), sharex=True, sharey=True)

def plot_with_hatching(ax, m, sst_data, sig_data):
    x, y = m(xh, yh)
    cs = m.pcolormesh(x, y, sst_data, shading='auto', cmap='coolwarm', vmin=-0.5, vmax=0.5)
    sig_mask = np.ma.masked_where(sig_data != 2, sig_data)
    m.contourf(x, y, sig_mask, colors='none', hatches=['////'],extend='both', alpha=0)

    m.drawcoastlines()
    m.drawcountries()
    m.fillcontinents(color='black')
    m.drawparallels(np.arange(-90., 91., 30.), labels=[1,0,0,0])
    m.drawmeridians(np.arange(-180., 181., 60.), labels=[0,0,0,1])
    return cs
ax1 = axes[0]
m1 = Basemap(projection='cyl', lon_0=180, resolution='c', ax=ax1)
cs1=plot_with_hatching(ax1, m1, annual, annual_sig)
ax1.set_title('Annual')

ax2 = axes[1]
m2 = Basemap(projection='cyl', lon_0=180, resolution='c', ax=ax2)
cs2=plot_with_hatching(ax2, m2, JJA, JJA_sig)
ax2.set_title('JJA')

ax3 = axes[2]
m3 = Basemap(projection='cyl', lon_0=180, resolution='c', ax=ax3)
cs3=plot_with_hatching(ax3, m3, DJF, DJF_sig)
ax3.set_title('DJF')

cbar_ax = fig.add_axes([0.75, 0.15, 0.02, 0.7])
cbar = fig.colorbar(m3.pcolormesh(xh, yh, DJF, shading='auto', cmap='coolwarm', vmin=-0.5, vmax=0.5), cax=cbar_ax, ticks=np.arange(-0.5, 0.6, 0.1))

plt.subplots_adjust(hspace=0.4)

plt.show()

enter image description here

If the "*_sig" variables have "2", these are statistically significant areas.

4
  • 1
    Are you sure that the areas in DJF are large enough and are not just too small for the hatch fill (i.e., a smaller than the width between hatch lines), and so are not just not be showing up. Could just try a single color fill and check this? Commented Jul 24, 2024 at 13:32
  • I additionally attached the link to the previously failed plot where the statistically significant areas are colored yellow. The regions of DJF are large enough. Please check the above edited context. Thanks for your comment. Commented Jul 25, 2024 at 6:07
  • As an experiment, could you try removing the two lines that add the colorbar? Commented Jul 25, 2024 at 8:37
  • Another thing to try might be explicitly setting the zorder of the contour plots to something large, e.g., in m.contourf(..., zorder=100) to make sure the hatching is always on top. Commented Jul 25, 2024 at 8:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.