Skip to content

Commit 31179c6

Browse files
committed
Upgraded histogram vis to include all bands.
1 parent 45fab49 commit 31179c6

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

jupyter-notebooks/coastal-erosion-example/5_plotting_a_histogram.ipynb

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
]
6161
},
6262
{
63-
"attachments": {},
6463
"cell_type": "markdown",
6564
"id": "64f7325f",
6665
"metadata": {},
@@ -81,7 +80,7 @@
8180
"import numpy as np\n",
8281
"from matplotlib import pyplot as plt\n",
8382
"\n",
84-
"# This notebook explores a single 4 band (blue, green, red, NIR) PlanetScope scene in a UTM projection.\n",
83+
"# This notebook explores a single 4-band (blue, green, red, NIR) PlanetScope scene in a UTM projection.\n",
8584
"image_file = \"/content/20170831_172754_101c_3B_AnalyticMS.tif\"\n",
8685
"\n",
8786
"# Use Rasterio to open the image.\n",
@@ -95,13 +94,12 @@
9594
"metadata": {},
9695
"outputs": [],
9796
"source": [
98-
"# Load the 4 bands into 2d arrays\n",
97+
"# Load the 3 of the 4 bands into 2d arrays (excluding NIR for this example)\n",
9998
"# recall that we previously learned 4-band PlanetScope band order is BGRN.\n",
10099
"with rasterio.open(image_file) as src:\n",
101100
" blue = src.read(1)\n",
102101
" green = src.read(2)\n",
103-
" red = src.read(3)\n",
104-
" nir = src.read(4)"
102+
" red = src.read(3)"
105103
]
106104
},
107105
{
@@ -112,43 +110,46 @@
112110
"outputs": [],
113111
"source": [
114112
"# Define a new figure\n",
115-
"fig = plt.figure(figsize=(20,10))\n",
113+
"fig = plt.figure()\n",
116114
"\n",
117115
"# Give this new figure a subplot, which will contain the histogram itself\n",
118116
"ax = fig.add_subplot(111)\n",
119117
"\n",
120118
"# Add a title & (x,y) labels to the plot\n",
121-
"plt.title(\"Histogram Example\", fontsize=18, fontweight='bold')\n",
122-
"plt.xlabel(\"pixel values\", fontsize=14)\n",
119+
"plt.title(\"PlanetScope raster bands\", fontsize=18, fontweight='bold')\n",
120+
"plt.xlabel(\"Pixel values\", fontsize=14)\n",
123121
"plt.ylabel(\"Number of pixels\", fontsize=14)\n",
124122
"\n",
125-
"# let's plot non-null blue band values as an example\n",
126-
"# For the x-axis, we want to count every pixel that has a value\n",
127-
"x = blue[np.not_equal(blue, satdat.nodata)]\n",
123+
"# For the x-axis, we want to count every pixel that has a non-nul value\n",
124+
"b = blue[np.not_equal(blue, satdat.nodata)]\n",
125+
"g = green[np.not_equal(green, satdat.nodata)]\n",
126+
"r = red[np.not_equal(red, satdat.nodata)]\n",
127+
"\n",
128+
"# Combine all of the bands into a list, for plotting\n",
129+
"all_bands = [b, g, r]\n",
130+
"all_band_colours = ['b','g','r']\n",
128131
"\n",
129132
"# Define the number of bins to divide the data into\n",
130133
"bins = 50\n",
131134
"\n",
132-
"# Define a color for the histogram\n",
133-
"# You can use https://matplotlib.org/2.0.0/examples/color/named_colors.html as a reference\n",
134-
"color = 'royalblue'\n",
135-
"\n",
136-
"# call 'hist` with our x-axis, bins, and color details\n",
137-
"ax.hist(x,bins,color=color)\n",
135+
"# Call 'hist` with our x-axis, bins, and colour details\n",
136+
"for i in range(len(all_bands)):\n",
137+
" ax.hist(all_bands[i], bins, color=all_band_colours[i], alpha=0.4)\n",
138138
"\n",
139139
"# Save the generated figure to an external image file\n",
140-
"fig.savefig(\"histogram.png\", dpi=200, bbox_inches='tight', pad_inches=0.7)\n",
140+
"fig.savefig(\"histogram.png\", dpi=200, bbox_inches='tight', pad_inches=0)\n",
141141
"\n",
142142
"# Finally - let's take a look!\n",
143143
"plt.show()"
144144
]
145145
},
146146
{
147+
"attachments": {},
147148
"cell_type": "markdown",
148149
"id": "b70c572d",
149150
"metadata": {},
150151
"source": [
151-
"We can see that most of the blue band values in our image seem to be around the 7000 range. In our image, this corresponds to the value of the blue band when we are looking at flooded areas. It makes sense then that most of the blue band would correspond to water values!!"
152+
"We can see that most of the blue band values in our image seem to be around the 7000 range, then green and red peak at lower values. In our image, this corresponds to the value of the blue band when we are looking at flooded areas. It makes sense then that most of the blue band would correspond to water values!"
152153
]
153154
}
154155
],

0 commit comments

Comments
 (0)