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

change land color to transparent #290

Closed
Marston opened this issue Oct 4, 2021 · 8 comments
Closed

change land color to transparent #290

Marston opened this issue Oct 4, 2021 · 8 comments

Comments

@Marston
Copy link

Marston commented Oct 4, 2021

I would like to plot the coastlines but not to have the data over the land masked out. Can I change the land fill to transparent?

@Marston Marston closed this as completed Oct 4, 2021
@Marston
Copy link
Author

Marston commented Oct 4, 2021

Figured it out. Use coast=True

@lukelbd lukelbd added the support label Oct 5, 2021
@lukelbd
Copy link
Collaborator

lukelbd commented Oct 6, 2021

Cool. Your title also made me realize it might be useful to change the opacity alpha of different geographic features so I added a bunch of rc settings: rc['land.alpha'], rc['ocean.alpha'], rc['coast.alpha'], etc.

Here's an example (below the land color is black with opacity of 20% -- you could also pass landalpha to ax.format):

import proplot as pplt
fig = pplt.figure()
ax = fig.subplot(proj='cyl', land=True, coast=True, landalpha=0.2)

tmp

@lukelbd lukelbd added the feature label Oct 6, 2021
@Marston
Copy link
Author

Marston commented Oct 7, 2021

@lukelbd
I am not able to pass landalpha to the subplot or subplots routines. Are the changes you made pushed to the code repo and released as an update? Otherwise, this is not working for me. On another note, I keep getting this error that I don't understand:
C:\Users\AppData\Local\Temp/ipykernel_3600/530739346.py:28: ProPlotWarning: Unexpected object AxesImage(70.1389,1356.25;1923.61x616.806) passed to _apply_edgefix. em = eax.pcolorfast(ev, cmap='gist_ncar_r', vmin=0.5, vmax=50.0)
In addition, I getting as default discrete colorbars which does not work when I'm plotting data that are symmetric around zero. Proplot doesn't show the white color for 0s and values close to it for the colormap: seismic, e.g.
How can I remove the discrete colorbar feature?

`
import proplot as pplt

fig = pplt.figure(figsize=(20,20))

eax, iax, dax = fig.subplots(nrows=3, proj='pcarree', land=True)
eax.format(lonlim=(lonMin, lonMax), latlim=(latMin, latMax), coast=True, labels=True)
iax.format(lonlim=(lonMin, lonMax), latlim=(latMin, latMax), coast=True, labels=True)
dax.format(lonlim=(lonMin, lonMax), latlim=(latMin, latMax), coast=True, labels=True)

em = eax.pcolorfast(ev, cmap='gist_ncar_r', vmin=0.5, vmax=50.0)
im = iax.pcolorfast(ib, cmap='gist_ncar_r', vmin=0.5, vmax=50.0)
dm = dax.pcolorfast(dif, cmap='RdBu_r', vmin=-20, vmax=20)

eax.set_title('Eval 210930 Proxy')
iax.set_title('Baseline 210930 Proxy')
dax.set_title('Eval - Baseline 210930 Proxy')

cbar = eax.colorbar(em, loc='b', label='dBZ', length=0.5)
cbar = iax.colorbar(im, loc='b', label='dBZ', length=0.5)
cbar = dax.colorbar(dm, loc='b', label='dBZ', length=0.5) `

baseline-eval-transform-small

@lukelbd
Copy link
Collaborator

lukelbd commented Oct 7, 2021

Yep the landalpha feature is not yet in a release -- it's just on the "master" branch (which itself can be installed with pip install git+https://github.com/lukelbd/proplot). Releases happen when there's enough changes to justify it.

Thanks for alerting me to that pcolorfast issue, that was a one-line fix (ad1f00f). It will no longer trigger warnings in the next version (note that warning message was harmless -- just annoying/unnecessary).

That second issue is some really weird/insiduous problem with cartopy's projection transforms. Someone else emailed me with the same issue -- it's also possible to encounter this with just cartopy + matplotlib (i.e. without proplot). For now, if you try using a different map projection, or if you try tweaking the edge coordinates of your dataset (for example, setting the maximum latitude to 89 instead of 90, or setting the maximum longitude to 179 instead of 180), you should be able to fix it. But the behavior is pretty unpredictable, seems to depend on the dataset and possibly even the figure size.

@lukelbd
Copy link
Collaborator

lukelbd commented Oct 7, 2021

Also, just some advice, not sure I'd recommend making a 20 inch x 20 inch figure. If you're having issues with your figure showing up too small in the display window, it might be better to increase the dpi with something like pplt.rc['figure.dpi'] = 300 or passing e.g. pplt.figure(..., dpi=300) . That way the labels are still readable.

@Marston
Copy link
Author

Marston commented Oct 8, 2021

OK. I have a conda env and doing pip install always corrupts my env. I'll wait for the release.
I understood that the warning was not critical. I just wanted to understand what it was saying. What was the cause?
As for the third issue, I tested a cartopy= matplotlib and didn't get the problem with proplot.

`
titles = ['Eval 210930 Proxy','Baseline 210930 Proxy','Eval - Baseline 210930 Proxy']
fig, (ex, ix, dx) = plt.subplots(nrows=3, figsize=(12,12), subplot_kw={'projection':ccrs.PlateCarree()})

for ax in [ex, ix, dx]:
ax.coastlines()
ax.gridlines()
ax.label_outer()

Plot the data

shrink = 1.0
cmap = 'nipy_spectral_r'#'gist_ncar_r' #'gist_stern_r'
ev.plot(ax=ex, transform=ccrs.PlateCarree(),cmap=cmap, vmin=0.1, vmax=20, cbar_kwargs={'shrink': shrink})
ib.plot(ax=ix, transform=ccrs.PlateCarree(),cmap=cmap, vmin=0.1, vmax=20, cbar_kwargs={'shrink': shrink})
dif.plot(ax=dx, transform=ccrs.PlateCarree(),cmap='seismic', vmin=-15, vmax=15, cbar_kwargs={'shrink': shrink})

for ax, title in zip([ex, ix, dx],titles):
ax.set_title(title)

ev.close()
ib.close()
dif.close()
`

The above code produces the proper treatment of the dataset in this fig:

baseline-eval-m-c

Thanks for the figsize tip.

@lukelbd
Copy link
Collaborator

lukelbd commented Oct 10, 2021

Yeah pip install sometimes messes up the conda environment, but it's pretty safe with proplot since the dependencies are unusually simple (literally just matplotlib). The warning was caused by proplot trying to apply the "edgefix" feature to AxesImage objects returned by pcolorfast, despite the fact that AxesImage objects don't have "edges" that need to be fixed (see the edgefix parameter under the pcolor docs for details, and this stackoverflow post for a demonstration).

As for the contour issue, it looks like your example with just matplotlib + cartopy might still be presenting the problem -- except your colormap is different, so the background is getting colored by white instead of light blue. I've been able to reproduce this problem in just cartopy + matplotlib, so it is indeed a cartopy problem:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import proplot as pplt
N = 10
state = np.random.RandomState(23)
fig = plt.figure(figsize=(5, 2.5))
ax = fig.add_subplot(projection=ccrs.Mollweide())
ax.coastlines()
x = np.linspace(-180, 180, N)
y = np.linspace(-90, 90, N)
z = state.rand(N, N) * 10 - 5
m = ax.contourf(
    x, y, z,
    transform=ccrs.PlateCarree(),
    cmap='NegPos',
    vmin=-5,
    vmax=5,
)
fig.colorbar(m, ax=ax)

bug

I think I'll submit a bug report to cartopy with this example.

@lukelbd
Copy link
Collaborator

lukelbd commented Oct 10, 2021

Turns out there's already an active cartopy issue: SciTools/cartopy#1076. I added the above example to the thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants