matplotlib is a very powerful plotting library for making amazing visualizations for publications, personal use, or even web and desktop applications. matplotlib
can create almost any two dimensional visualization you can think of, including histograms, scatter plots, bivariate plots, and image displays. For some inspiration, check out the matplotlib
example gallery which includes the source code required to generate each example.
A great resource for learning matplotlib
is available from J.R. Johansson.
One part of matplotlib
that may be initially confusing is that matplotlib
contains two main methods of making plots - the object-oriented method, and the state-machine method.
While the library can be either used in an object-oriented manner (i.e., you create an object representing the figure, then the figure can spawn objects representing the axes, etc.), the most familiar usage of matplotlib
for MATLAB users is the pyplot
state-machine environment:
from the matplotlib usage FAQ:
Pyplot’s state-machine environment behaves similarly to MATLAB and should be most familiar to users with MATLAB experience.
A very good overview of the difference between the two usages is provided by Jake Vanderplas. Specifically,
In general, you should only use the Pyplot
state-machine environment when plotting data interactively or when developing visualizations for your data. The object-oriented API, while more complicated, is a much more powerful way of creating plots and should be used when developing more complicated visualizations.
As this is a brief introduction to matplotlib
, we will be using the Pyplot
state-machine method for creating visualizations.
We will begin by reading our example image into a NumPy memory array as shown in Chapter 3
With the data read in and NDVI calculated, let's make some plots.
Basic plotting¶First thing to do is to import matplotlib
into our namespace. I will be using a special feature of the IPython utility which allows me to "inline" matplotlib
figures by entering the %matplotlib inline
command. You might also want to try the nbagg
backend designed specifically for Jupyter notebooks as this backend allows you to interact (pan, zoom, etc) with the plot.
With matplotlib
imported, we can summon up a figure and make our first plot:
One typical thing that we might want to do would be to plot one band against another. In order to do this, we will need to transform, or flatten
, our 2 dimensional arrays of each band's values into 1 dimensional arrays:
We have retained the number of entries in each of these raster bands, but we have flattened them from 2 dimensions into 1.
Now we can plot them. Since we just want points, we can use scatter
for a scatterplot. Since there are no lines in a scatterplot, it has a slightly different syntax.
If we wanted the two axes to have the same limits, we can calculate the limits and apply them
Plotting 2D arrays - images¶With so much data available to look at, it can be hard to understand what is going on with the mess of points shown above. Luckily our datasets aren't just a mess of points - they have a spatial structure.
To show the spatial structure of our images, we could make an image plot of one of our bands using imshow
to display an image on the axes:
Well, it looks like there is something going on - maybe a river in the center and some bright vegetation to the bottom left of the image. What's lacking is any knowledge of what the colors mean.
Luckily, matplotlib
can provide us a colorbar.
If we want a greyscale image, we can manually specify a colormap:
Plotting 3D arrays - multispectral images¶Greyscale images are nice, but the most information we can receive comes from looking at the interplay among different bands. To accomplish this, we can map different spectral bands to the Red, Green, and Blue channels on our monitors.
Before we can do this, the matplotlib
imshow
help tells us that we need to normalize our bands into a 0 - 1 range. To do so, we will perform a simple linear scale fitting 0 reflectance to 0 and 80% reflectance to 1, clipping anything larger or smaller.
Remember:
If we are going from a Int16 datatype (e.g., reflectance scaled by 10,000x) to a decimal between 0 and 1, we will need to use a Float!
Wrapup¶
We seen how matplotlib can be combined with NumPy and GDAL to easily visualize and explore our remote sensing data. In the next chapter (link to webpage or Notebook) we will cover how to use GDAL's companion library - OGR - to open and read vector data.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4