OSGeoLive-Notebooks/Cartopy/cartopy-ticklabels.ipynb

106 lines
3.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tick Labels\n",
"-----------\n",
"\n",
"This example demonstrates adding tick labels to maps on rectangular\n",
"projections using special tick formatters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import cartopy.crs as ccrs\n",
"from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter\n",
"import matplotlib.pyplot as plt\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"def main():\n",
" fig = plt.figure(figsize=(8, 10))\n",
"\n",
" # Label axes of a Plate Carree projection with a central longitude of 180:\n",
" ax1 = fig.add_subplot(2, 1, 1,\n",
" projection=ccrs.PlateCarree(central_longitude=180))\n",
" ax1.set_global()\n",
" ax1.coastlines()\n",
" ax1.set_xticks([0, 60, 120, 180, 240, 300, 360], crs=ccrs.PlateCarree())\n",
" ax1.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())\n",
" lon_formatter = LongitudeFormatter(zero_direction_label=True)\n",
" lat_formatter = LatitudeFormatter()\n",
" ax1.xaxis.set_major_formatter(lon_formatter)\n",
" ax1.yaxis.set_major_formatter(lat_formatter)\n",
"\n",
" # Label axes of a Mercator projection without degree symbols in the labels\n",
" # and formatting labels to include 1 decimal place:\n",
" ax2 = fig.add_subplot(2, 1, 2, projection=ccrs.Mercator())\n",
" ax2.set_global()\n",
" ax2.coastlines()\n",
" ax2.set_xticks([-180, -120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree())\n",
" ax2.set_yticks([-78.5, -60, -25.5, 25.5, 60, 80], crs=ccrs.PlateCarree())\n",
" lon_formatter = LongitudeFormatter(number_format='.1f',\n",
" degree_symbol='',\n",
" dateline_direction_label=True)\n",
" lat_formatter = LatitudeFormatter(number_format='.1f',\n",
" degree_symbol='')\n",
" ax2.xaxis.set_major_formatter(lon_formatter)\n",
" ax2.yaxis.set_major_formatter(lat_formatter)\n",
"\n",
" plt.show()\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}