You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
significance of the number 14 in calls to contour and tricontour? #12762
What is the significance of the number 14 in calls to contour and tricontour? (I know that this is a lucky number of have at table, but am wondering if there is some other reason for this value).
You're asking about ax.contour(x, y, z, 14), right? This is the "nominal" number of levels to use in the contouring. Unfortunately, the documentation is wrong with respect to this argument, as we've just found out.
Hence I'd call it "nominal" for now. As can be seen from the colorbar there are 15 contour regions, separated by 14 inner edges. If you were to choose a different number of levels, e.g. 9 instead of 14, you'd see 9 contoured regions in the plot
If you need full control over the number of levels, or even the actual numeric values, you may plug in a list of edge values, e.g. ax.contour(xi, yi, zi, numpy.linspace(-.5, +.5, 21)) (21 edges, hence 20 contour regions; but not all of them are present in the plot, because the data does not range up to 0.5).
As to why the this example takes precisely 14 here, I can't remember. In a previous version of it, there were 15 levels being used. Then the example got lost. Then I revived it and for a reason I cannot recall, used 14 levels.
It shouldn't actually matter, but if anyone has an argument for why possibly a different number should be used here, we can also change it.
I've doing something very similar in the attached script. (The main
difference is that the contour plot is overlaid on a map).
In line 98, if I include the `levels=20` argument, the code dies with an
inexplicable error (see below). If I omit that argument, there's no error
message and I get some output.
Any advice will be appreciated.
Phillip
python cartopy_filled_contour2.py
Traceback (most recent call last):
File "cartopy_filled_contour2.py", line 98, in <module>
ax.contourf(lons, lats, data, transform=projection, levels=20,
cmap='nipy_spectral')
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\cartopy\mpl\geoaxes.py",
line 1145, in contourf
result = matplotlib.axes.Axes.contourf(self, *args, **kwargs)
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\__init__.py",
line 1892, in inner
return func(ax, *args, **kwargs)
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\axes\_axes.py",
line 5829, in contourf
contours = mcontour.QuadContourSet(self, *args, **kwargs)
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\contour.py",
line 864, in __init__
self._process_args(*args, **kwargs)
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\contour.py",
line 1429, in _process_args
x, y, z = self._contour_args(args, kwargs)
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\contour.py",
line 1520, in _contour_args
self._contour_level_args(z, args)
File "C:\Program
Files\Enthought\Canopy\edm\envs\User\lib\site-packages\matplotlib\contour.py",
line 1179, in _contour_level_args
if self.filled and len(self.levels) < 2:
TypeError: object of type 'int' has no len()
"""
cartopy_filled_contour.py
OVERVIEW
This script generates a map that shows (1) coastlines of land masses, (2) major
political boundaries, and (3) an overlaid filled contour plot of dummy data.
NOTE
If one has requested the plotting of coastlines, political boundaries, etc.,
Cartopy needs access to those files, which must be either (a) available in a
configured local or network folder or (b) accessible via the Internet.
In case (b), Cartopy uses urllib2 to download files from the Internet to the
configured folder. For this to work, one must configure the proxy by setting the
HTTP_PROXY environment variable to the following:
http://s117250:password@westproxy.northgrum.com:80
Once the files have been downloaded, Cartopy will use the downloaded files. One
can zip up this data and transfer to a machine that isn't connected to the
Internet.
The following URLs provide helpful information:
(1) https://scitools.org.uk/cartopy/docs/v0.16/cartopy.html
(2) https://stackoverflow.com/questions/32365410/location-of-stored-offline-data-for-cartopy
The instructions in (2) for discovering the local/network data directory are
almost right, but needed a slight tweak. The corrected instructions follow:
In [1]: from cartopy import config
In [2]: config['data_dir']
Out[2]: 'P:\\.local\\share\\cartopy' (example)
It is probably better to configure a common folder to be shared by all users,
but I'm using the local folder for the moment.
REVISION HISTORY
Version 1.0, 2018-11-06, Phillip M. Feldman: I created the initial version of
this script by making minor modifications to code from the following URL:
https://scitools.org.uk/cartopy/docs/v0.16/gallery/waves.html
"""
import matplotlib.pyplot as plt
from numpy import cos, linspace, meshgrid, pi, rad2deg, sin
import pdb
from cartopy import crs, feature
from CustomImporter import Cimport
files= Cimport('files')
def sample_data(shape=(73, 145)):
"""
Return ``lons``, ``lats`` and ``data`` of some fake data.
"""
nlats, nlons= shape
lats= linspace(-pi / 2, pi / 2, nlats)
lons= linspace(0, 2 * pi, nlons)
lons, lats= meshgrid(lons, lats)
wave= 0.75 * (sin(2 * lats) ** 8) * cos(4 * lons)
mean= 0.5 * cos(2 * lats) * ((sin(2 * lats)) ** 2 + 2)
lats= rad2deg(lats)
lons= rad2deg(lons)
data= wave + mean
return lons, lats, data
if True:
# This one works:
projection=crs.PlateCarree()
else:
# This one doesn't:
projection=crs.Mercator(min_latitude=-70., max_latitude=75.)
projection_name= str(projection).split()[0].split('.')[-1]
fig= plt.figure(figsize=[11,7], dpi=120, facecolor=[1,1,1])
ax= fig.add_subplot(1, 1, 1, projection=projection)
lons, lats, data= sample_data()
ax.contourf(lons, lats, data, transform=projection, levels=20, cmap='nipy_spectral')
# Add costlines of land masses:
ax.coastlines()
# Add major political boundaries:
ax.add_feature(feature.BORDERS, linewidth=1, edgecolor='black')
ax.set_global()
# Squeeze out wasted space at the left- and right-hand edges of the figure:
fig.subplots_adjust(left=0.05, right=0.95)
# Specify y=1.04 to prevent the title from being jammed against the frame of the
# plot:
plt.title('Demo of Filled Contour Plot Overlaid on {}-projection Map'
.format(projection_name), fontsize=16, y=1.04)
# Construct a name to be used for the saved plot file by combining the name of
# the script that is currently executing and the name of the projection:
outfile= files.extension(__file__, '') + ' ' + projection_name + '.png'
plt.savefig(outfile, dpi=120)
plt.show()
I cannot reproduce the error. But you did not mention which versions you are using.
In general, I guess even if you use a Mercator projection for the axes, the artist transform should still be a PlateCarree, as the data is defined in the 0, 360 / -90 90 range.
I made the proposed change (using `crs.PlateCarree()` for the transform),
although I don't fully understand this.
In any case, the result is the same as before. Here are the versions that
I'm using:
In [3]: matplotlib.__version__
Out[3]: '2.0.0'
In [4]: cartopy.__version__
Out[4]: '0.14.2'
On Fri, Nov 9, 2018 at 5:54 AM Elan Ernest ***@***.***> wrote:
I cannot reproduce the error. But you did not mention which versions you
are using.
In general, I guess even if you use a Mercator projection for the axes,
the artist transform should still be a PlateCarree, as the data is
defined in the 0, 360 / -90 90 range.
ax= fig.add_subplot(1, 1, 1, projection=crs.Mercator())
ax.contourf(lons, lats, data, transform=crs.PlateCarree(), levels=20, cmap='nipy_spectral')
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#12762 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABBBxL7Va5w2bOuTCG4hcOo-Urp_q5s8ks5utYkmgaJpZM4YRjfF>
.
I see clear now. 😎
In matplotlib < 3, levels did not actually take an integer although the documentation of 2.2.3 claimed that to be the case. So in 2.2.3 or below you need
One should do something on 2.2.x to get it's behavior and doc in sync.
If #11917 does only add the support for using levels as kwarg, then I'm +1 on backporting. If on the other hand, it also changes the figure generated with the positional arguement form, we should just update the docs.
Activity
ImportanceOfBeingErnest commentedon Nov 7, 2018
You're asking about
ax.contour(x, y, z, 14), right? This is the "nominal" number of levels to use in the contouring. Unfortunately, the documentation is wrong with respect to this argument, as we've just found out.Hence I'd call it "nominal" for now. As can be seen from the colorbar there are 15 contour regions, separated by 14 inner edges. If you were to choose a different number of levels, e.g. 9 instead of 14, you'd see 9 contoured regions in the plot
If you need full control over the number of levels, or even the actual numeric values, you may plug in a list of edge values, e.g.
ax.contour(xi, yi, zi, numpy.linspace(-.5, +.5, 21))(21 edges, hence 20 contour regions; but not all of them are present in the plot, because the data does not range up to 0.5).As to why the this example takes precisely 14 here, I can't remember. In a previous version of it, there were 15 levels being used. Then the example got lost. Then I revived it and for a reason I cannot recall, used 14 levels.
It shouldn't actually matter, but if anyone has an argument for why possibly a different number should be used here, we can also change it.
Phillip-M-Feldman commentedon Nov 9, 2018
ImportanceOfBeingErnest commentedon Nov 9, 2018
I cannot reproduce the error. But you did not mention which versions you are using.
In general, I guess even if you use a
Mercatorprojection for the axes, the artist transform should still be aPlateCarree, as the data is defined in the 0, 360 / -90 90 range.Phillip-M-Feldman commentedon Nov 9, 2018
ImportanceOfBeingErnest commentedon Nov 9, 2018
This has been fixed by #11917. It'll work fine with matplotlib 3.0.0 or higher.
#11917 has not been backported to the 2.x branch. Would that make sense in view of this?
ImportanceOfBeingErnest commentedon Nov 10, 2018
I see clear now. 😎
In matplotlib < 3,
levelsdid not actually take an integer although the documentation of 2.2.3 claimed that to be the case. So in 2.2.3 or below you needwhile in 3.0.0 or higher you can equally do
So, one could either change the documentation in the 2.x branch, or backport #11917 or forget about it, since the examples are all correct.
timhoffm commentedon Nov 18, 2018
I don't see so clearly 😃.
One should do something on 2.2.x to get it's behavior and doc in sync.
If #11917 does only add the support for using
levelsas kwarg, then I'm +1 on backporting. If on the other hand, it also changes the figure generated with the positional arguement form, we should just update the docs.anntzer commentedon Jul 3, 2019
Remilestoning as 2.2.x (#12762 (comment)) given that on master the example already reads
levels=14.timhoffm commentedon Jul 21, 2019
This has already been backported in #11917.