pytroll / satpy

Python package for earth-observing satellite data processing
http://satpy.readthedocs.org/en/latest/
GNU General Public License v3.0
1.07k stars 295 forks source link

CRS data is being printed to title of image #2977

Closed ustropics closed 1 day ago

ustropics commented 1 day ago

I am getting an issue where my crs is printing to the title of the figure it appears. Below is my code (where recipe is the composite name). Perhaps using image.plot.imshow is causing the issue? I've tried resetting the title with ax but it doesn't work.

area_def = create_area_def(
    'composite_area',  # Name of the area definition
    proj_dict,
    units='degrees',
    width=5240, 
    height=5240,
    area_extent=[lon_min, lat_min, lon_max, lat_max]
)

scn.load([recipe])
new_scn = scn.resample(area_def, resampler='nearest', cache_dir='cache/')

area = new_scn[recipe].attrs['area']

plt_scn = scn.resample(area)

image = get_enhanced_image(plt_scn[recipe]).data
crs = plt_scn[recipe].attrs['area'].to_cartopy_crs()

fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(1, 1, 1, projection=crs)

ax.add_feature(cfeature.COASTLINE, edgecolor=data['border_color_val'], linewidth=float(data['border_width_val']))
ax.add_feature(cfeature.BORDERS, edgecolor=data['border_color_val'], linewidth=float(data['border_width_val']))
ax.add_feature(cfeature.STATES, edgecolor=data['border_color_val'], linewidth=float(data['border_width_val']))
ax.gridlines(draw_labels=True, linewidth=0.5, color='gray', alpha=0.5, linestyle='--')

if len(plt_scn[recipe].shape) > 2:
    print("ax: ", ax)
    image.plot.imshow(vmin=0, vmax=1, add_colorbar=False, rgb='bands', ax=ax)
else:  # Single-band grayscale case
    image = np.squeeze(image)
    image.plot.imshow(vmin=0, vmax=1, add_colorbar=False, ax=ax)

plt.savefig(filename, dpi=300, bbox_inches='tight')
plt.close()

download

djhoese commented 1 day ago

This is coming from xarray's default plotting behavior if I remember correctly. The CRS object is stored in Satpy-created DataArray objects in .coords["crs"]. If forcing the title on axis after the .imshow call doesn't work, then it may not be an axis title but a figure title. In that case it would be fix.suptitle("TITLE") to change that title. But...I'm not sure, your image looks like an axis title.

At the moment I don't see anything obvious in the xarray code that is setting the title. I'll have to do more digging, but please try the figure title or ensure that your changing of the axis title is happening after imshow and let me know how it goes.

djhoese commented 1 day ago

Quick test shows that this works:

In [4]: img = scn["C01"].plot.imshow()

In [5]: img.axes.set_title("TEST")
Out[5]: Text(0.5, 1.0, 'TEST')

In [6]: img.figure.show()
djhoese commented 1 day ago

Ok, found it. It is controlled overall by the add_labels keyword argument:

https://github.com/pydata/xarray/blob/314912ce2715dbe9fb9fa12820559f9e2477d5d7/xarray/plot/dataarray_plot.py#L1616-L1624

However, if you set that to False then your axis ticks will be all wrong. The method it uses to come up with a title is here:

https://github.com/pydata/xarray/blob/314912ce2715dbe9fb9fa12820559f9e2477d5d7/xarray/core/dataarray.py#L4855-L4882

It is basically combining all 1 dimensional coordinate variables which for Satpy is typically just the single crs coordinate. So you can do two things:

  1. Set ax.set_title after the imshow call and before showing/saving it.
  2. Delete the CRS coordinate with del scn["C01"].coords["crs"].
ustropics commented 1 day ago

Thanks! Both methods worked, I appreciate the quick reply.

djhoese commented 1 day ago

Glad it was easy enough to figure out!