Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRS data is being printed to title of image #2977

Closed
ustropics opened this issue Nov 12, 2024 · 5 comments
Closed

CRS data is being printed to title of image #2977

ustropics opened this issue Nov 12, 2024 · 5 comments

Comments

@ustropics
Copy link

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
Copy link
Member

djhoese commented Nov 12, 2024

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
Copy link
Member

djhoese commented Nov 12, 2024

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
Copy link
Member

djhoese commented Nov 12, 2024

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
Copy link
Author

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

@djhoese
Copy link
Member

djhoese commented Nov 12, 2024

Glad it was easy enough to figure out!

@djhoese djhoese closed this as completed Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants