OSGeoLive-Notebooks/Rasterio/data_viz.ipynb

327 lines
7.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Basic data visualization\n",
"`© Copyright 2018, Mapbox`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import rasterio\n",
"from rasterio import plot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"#src = rasterio.open(r\"../tests/data/RGB.byte.tif\")\n",
"src = rasterio.open(r\"data/RGB.byte.tif\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### With a raster dataset, (band number) or numpy array we can display the raster"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"plot.show(src)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### We can also display a single band of a multiband image by passing a tuple (raster source, band)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"world = rasterio.open(r\"data/world.rgb.tif\")\n",
"plot.show((world, 2), cmap='viridis')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### side by side:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,7))\n",
"plot.show(src, ax=ax1)\n",
"plot.show((world, 2), cmap='viridis', ax=ax2)\n",
"fig.tight_layout()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Multiple bands side by side"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"fig, (axr, axg, axb) = plt.subplots(1,3, figsize=(21,7))\n",
"plot.show((src, 1), ax=axr, cmap='Reds', title='red channel')\n",
"plot.show((src, 2), ax=axg, cmap='Greens', title='green channel')\n",
"plot.show((src, 3), ax=axb, cmap='Blues', title='blue channel')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### How about the same thing but with the ability to navigate simultaneously?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"fig, (axr, axg, axb) = plt.subplots(1,3, figsize=(12, 4), sharex=True, sharey=True)\n",
"plot.show((src, 1), ax=axr, cmap='Reds', title='red channel')\n",
"plot.show((src, 2), ax=axg, cmap='Greens', title='green channel')\n",
"plot.show((src, 3), ax=axb, cmap='Blues', title='blue channel')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### If we want to see a histogram of the data we use the plot.show_hist function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"plot.show_hist(src)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Any of the optional pyplot histogram parameters can be passed to tweak the representation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"plot.show_hist(world, bins=50, lw=0.0, stacked=False, alpha=0.3, \n",
" histtype='stepfilled', title=\"World Histogram overlaid\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"plot.show_hist(world, bins=20, lw=2.0, stacked=True,\n",
" alpha=0.8, histtype='step', \n",
" title=\"World Histogram stacked\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### See matplotlib.pyplot.hist documentation at http://matplotlib.org/api/pyplot_api.html for a list of these parameters and available options"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,7))\n",
"plot.show(world, ax=ax1)\n",
"plot.show_hist(world, bins=50, lw=0.0, stacked=False, alpha=0.3, \n",
" histtype='stepfilled', title=\"World Histogram\")\n",
"fig.tight_layout()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Show contours from a raster"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, figsize=(12, 12))\n",
"plot.show((world, 1), cmap='Greys_r', interpolation='none', ax=ax)\n",
"ax.set_xlim(-50, 0)\n",
"ax.set_ylim(0, 40)\n",
"\n",
"plot.show((world, 1), contour=True, ax=ax)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### And of course there are many ways to easily customize this output ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"fig, ax = plt.subplots(1, figsize=(12, 12))\n",
"plot.show((world, 1), cmap='Greys_r', interpolation='none', ax=ax)\n",
"ax.set_xlim(-50, 0)\n",
"ax.set_ylim(0, 40)\n",
"\n",
"plot.show((world, 1), contour=True, ax=ax, \n",
" levels=[25, 125], colors=['white', 'red'], linewidths=4,\n",
" contour_label_kws=dict(fontsize=18, fmt=\"%1.0f\", inline_spacing=15, use_clabeltext=True))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Add a rasterio raster to a cartopy geoaxes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"import cartopy\n",
"import cartopy.crs as ccrs\n",
"\n",
"fig = plt.figure(figsize=(20, 12))\n",
"ax = plt.axes(projection=ccrs.InterruptedGoodeHomolosine())\n",
"\n",
"ax.set_global()\n",
"plot.show(world, origin='upper', transform=ccrs.PlateCarree(), interpolation=None, ax=ax)\n",
"\n",
"ax.coastlines()\n",
"ax.add_feature(cartopy.feature.BORDERS)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": []
},
{
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 1
}