-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
significance of the number 14 in calls to contour
and tricontour
?
#12762
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
You're asking about 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. 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. |
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()
On Tue, Nov 6, 2018 at 5:36 PM Elan Ernest ***@***.***> wrote:
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
<https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.contour.html> is
wrong with respect to this argument, as we've just found out
<#12729>.
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
[image: image]
<https://user-images.githubusercontent.com/23121882/48104387-98d36380-e233-11e8-93b6-d4a75117c384.png>
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, 11)) (11 edges, hence 10 contour
regions).
[image: image]
<https://user-images.githubusercontent.com/23121882/48104588-7beb6000-e234-11e8-981a-ea3177834139.png>
As to why the this example takes precisely 14 here, I can't remember. In a previous
version
<https://matplotlib.org/2.1.2/gallery/images_contours_and_fields/tricontour_vs_griddata.html>
of it, there were 15 levels being used. Then the example got lost. Then I revived
it <#11180> 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.
—
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/ABBBxFOzgHF_TFkPZq3zkFBG7tQ6aq0Gks5usjkMgaJpZM4YRjfF>
.
"""
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
|
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. 😎
while 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. |
I don't see so clearly 😃. One should do something on 2.2.x to get it's behavior and doc in sync. |
Remilestoning as 2.2.x (#12762 (comment)) given that on master the example already reads |
This has already been backported in #11917. |
I'm looking at the page at this URL:
https://matplotlib.org/gallery/images_contours_and_fields/irregulardatagrid.html#sphx-glr-gallery-images-contours-and-fields-irregulardatagrid-py
What is the significance of the number 14 in calls to
contour
andtricontour
? (I know that this is a lucky number of have at table, but am wondering if there is some other reason for this value).The text was updated successfully, but these errors were encountered: