备注
Go to the end 下载完整的示例代码。
将数据复制到同心椭圆上#
此示例演示在同心椭圆上绘制数据。绘制的数据是小行星Vesta的地形图。该地图实际上是一张图像,其定义在相对于半长轴为285公里、半短轴为229公里的椭圆的等矩形投影上。该图像被实时重新投影到具有匹配的同心圆投影上。
Traceback (most recent call last):
File "/pb1/repo/cartopy/examples/miscellanea/eccentric_ellipse.py", line 72, in <module>
main()
~~~~^^
File "/pb1/repo/cartopy/examples/miscellanea/eccentric_ellipse.py", line 60, in main
img, globe, crs, extent = vesta_image()
~~~~~~~~~~~^^
File "/pb1/repo/cartopy/examples/miscellanea/eccentric_ellipse.py", line 41, in vesta_image
img_handle = BytesIO(urlopen(url).read())
~~~~~~~^^^^^
File "/usr/lib/python3.13/urllib/request.py", line 189, in urlopen
return opener.open(url, data, timeout)
~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.13/urllib/request.py", line 489, in open
response = self._open(req, data)
File "/usr/lib/python3.13/urllib/request.py", line 506, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
'_open', req)
File "/usr/lib/python3.13/urllib/request.py", line 466, in _call_chain
result = func(*args)
File "/usr/lib/python3.13/urllib/request.py", line 1367, in https_open
return self.do_open(http.client.HTTPSConnection, req,
~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
context=self._context)
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.13/urllib/request.py", line 1322, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 110] Connection timed out>
from io import BytesIO
from urllib.request import urlopen
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
import cartopy.crs as ccrs
def vesta_image():
"""
Return an image of Vesta's topography.
Image credit: NASA/JPL-Caltech/UCLA/MPS/DLR/IDA/PSI
Returns
-------
img : numpy array
The pixels of the image in a numpy array.
img_proj : cartopy CRS
The rectangular coordinate system of the image.
img_extent : tuple of floats
The extent of the image ``(x0, y0, x1, y1)`` referenced in
the ``img_proj`` coordinate system.
"""
url = 'https://photojournal.jpl.nasa.gov/jpeg/PIA17037.jpg'
img_handle = BytesIO(urlopen(url).read())
raw_image = Image.open(img_handle)
# The image is extremely high-resolution, which takes a long time to
# plot. Sub-sampling reduces the time taken to plot while not
# significantly altering the integrity of the result.
smaller_image = raw_image.resize([raw_image.size[0] // 10,
raw_image.size[1] // 10])
img = np.asarray(smaller_image)
# We define the semimajor and semiminor axes, but must also tell the
# globe not to use the WGS84 ellipse, which is its default behaviour.
img_globe = ccrs.Globe(semimajor_axis=285000., semiminor_axis=229000.,
ellipse=None)
img_proj = ccrs.PlateCarree(globe=img_globe)
img_extent = (-180, 180,
-90, 90)
return img, img_globe, img_proj, img_extent
def main():
img, globe, crs, extent = vesta_image()
projection = ccrs.Geostationary(globe=globe)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=projection)
ax.imshow(img, transform=crs, extent=extent)
fig.text(.075, .012, "Image credit: NASA/JPL-Caltech/UCLA/MPS/DLR/IDA/PSI",
bbox={'facecolor': 'w', 'edgecolor': 'k'})
plt.show()
if __name__ == '__main__':
main()
Total running time of the script: (2 minutes 13.607 seconds)
Gallery generated by Sphinx-Gallery
_