-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Plotting on map projection much slower on v0.6.1 than 0.6.0 #657
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
Comments
We changed the default plot type from "imshow" in xray v0.6 to "pcolormesh" in xray v0.6.1. Does setting that manually make a difference? e.g., |
Thanks for the quick answer. changing to |
If I remove the projection stuff |
Yes, the difference in speed is definitely due to cartopy, which handles the projections. @pelson may be able to clarify whether imshow is always slower than pcolormesh when using cartopy, or if that is specific to particular projections. We changed the default from imshow to pcolormesh in #608. I would not be opposed to changing the default back, given that we already product essentially equivalent plots with both methods. We did add a performance note to our (currently broken) docs: http://xray.readthedocs.org/en/latest/plotting.html#two-dimensions @jhamman and/or @clarkfitzg may have some opinions here. |
I guess it's OK to leave it as is, as long as the performance loss is documented. I like the automatic 30 seconds to generate a plot in a Notebook is definitely a fun killer. Maybe I should still upgrade to 0.6.1 and explain to my students that they should use |
There is definitely scope for being smarter with cartopy's pcolormesh. There isn't an issue for it yet, but would be happy if you opened up a performance related issue in cartopy. pcolormesh will always be slower than imshow, but in most cases, not an order of magnitude slower! |
Hi again, I've made a self-contained example below. I have no clue about how cartopy works (blind xray user, sorry :-() and was not able to remove the dependency to xray without getting different plots for imshow() and pcolormesh()... If one of the xray gurus could help me to remove the xray part of the code I could open an issue in cartopy. I wonder if the problem comes from the fact that ERA-Interim lons are spanning 0-360? import matplotlib.pyplot as plt
import xray
import numpy as np
import cartopy.crs as ccrs
import time
nlats, nlons = (241, 480)
lats = np.linspace(90, -90, nlats)
lons = np.linspace(0, 360-0.75, nlons)
l1, l2 = np.meshgrid(lons, lats)
data = xray.DataArray(l1 + l2, [('latitude', lats), ('longitude', lons)])
start_time = time.time()
fig = plt.figure()
ax = plt.axes(projection=ccrs.Robinson())
data.plot.imshow(ax=ax, transform=ccrs.PlateCarree())
ax.coastlines()
plt.savefig('imshow.png')
print("imshow: {:.2f} s".format(time.time() - start_time))
start_time = time.time()
fig = plt.figure()
ax = plt.axes(projection=ccrs.Robinson())
data.plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree())
ax.coastlines()
plt.savefig('pcolormesh.png')
print("pcolormesh: {:.2f} s".format(time.time() - start_time)) imshow: 3.09 s |
Thanks @fmaussion - I've raised it in SciTools/cartopy#700. Thanks for putting together the self contained example - it's fine to have xray as a dependency on that. FWIW you would do something like |
Thanks @pelson ! So now this is when it becomes funny: I've been able to make three similar plots using xray's imshow, pcolormesh and cartopy's pcolormesh. Xray's pcolormesh takes twice the time as cartopy's: import matplotlib.pyplot as plt
from matplotlib.colors import Normalize
import xray
import numpy as np
import cartopy.crs as ccrs
import time
nlats, nlons = (241, 480)
lats = np.linspace(90, -90, nlats)
lons = np.linspace(0, 360-0.75, nlons)
l1, l2 = np.meshgrid(lons, lats)
data = xray.DataArray(l1 + l2, [('latitude', lats), ('longitude', lons)])
cmap = plt.get_cmap('viridis')
norm = Normalize(vmin=0, vmax=data.max().values)
start_time = time.time()
fig = plt.figure()
ax = plt.axes(projection=ccrs.Robinson())
data.plot.imshow(ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False, cmap=cmap, vmin=0)
ax.coastlines()
plt.savefig('imshow_xray.png')
print("imshow xray: {:.2f} s".format(time.time() - start_time))
start_time = time.time()
fig = plt.figure()
ax = plt.axes(projection=ccrs.Robinson())
data.plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(), add_colorbar=False, cmap=cmap, vmin=0)
ax.coastlines()
plt.savefig('pcolormesh_xray.png')
print("pcolormesh xray: {:.2f} s".format(time.time() - start_time))
start_time = time.time()
fig = plt.figure()
ax = plt.axes(projection=ccrs.Robinson())
ax.pcolormesh(lons, lats, l1 + l2, transform=ccrs.PlateCarree(), cmap=cmap, norm=norm)
ax.coastlines()
plt.savefig('pcolormesh_cartopy.png')
print("pcolormesh cartopy: {:.2f} s".format(time.time() - start_time)) imshow xray: 3.06 s |
This is surprising! But good to know. |
Was someone able to reproduce this or is it just me? |
Yes, I'm seeing the same thing. Very weird -- I'll see if I can profile it. On Wed, Nov 18, 2015 at 8:28 AM, Fabien Maussion [email protected]
|
I can't verify right now but it may have something to do with using masked arrays under the hood. There are no nan's in your example but xray still is converting the array to a masked_array before plotting. I bet plotting with pcolormesh is slower with masked arrays than with numpy arrays. |
Changing the input to |
Profiling for each version of pcolormesh: https://gist.github.com/shoyer/73e3841827fe1eb08d00 Switching a masked array doesn't seem to make the non-xray version any slower... |
See the number of calls: faster.txt: slower.txt: |
I had little time to spend on this lately but I'll try to get back to it in the next days. Any idea on where it could come from? I found it quite hard to debug because of the many decorators... From the profiling and the number of function calls above there seems to be something quite big happening in between xray and cartopy. Could it be something as trivial as a double function call or something? |
OK, on the current master and with a cartopy installed from conda-forge I am not able to reproduce the problem any more:
I don't really understand what happened in between... The factor 4 between (note: on my laptop I'm getting faster plotting results in a virtualenv configured with |
Strange... thanks for checking again. |
The following code snippet produces an average of ERA-Interim temperature on a map:
I've been very careful to check that my environments are exact same (mpl 1.4.3, numpy 1.10.1, cartopy 0.13.0).
See the output for V0.6.0 and 0.6.1 (output from the latest master is similar to 0.6.1):
0.6.0:
0.6.1:
The first warning seems related to recent numpy. Note that a second warning appeared with xray V0.6.1.
It's interesting to mention that the bottleneck clearly is in the rendering (
plt.savefig('t_xray.png')
). Removing this line will make xray V0.6.1 faster than xray V0.6.0.The text was updated successfully, but these errors were encountered: