Reader small image

You're reading from  Mastering Matplotlib 2.x

Product typeBook
Published inNov 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781789617696
Edition1st Edition
Languages
Right arrow
Author (1)
Benjamin Walter Keller
Benjamin Walter Keller
author image
Benjamin Walter Keller

Benjamin Walter Keller is currently a PhD candidate at McMaster University and gained his BSc in physics with a minor in computer science from the University of Calgary in 2011. His current research involves numerical modeling of galaxy evolution over cosmological timescales. As an undergraduate at the U of C, he worked on stacking radio polarization to examine faint extragalactic sources. He also worked in the POSSUM Working Group 2 to determine the requirements for stacking applications for the Australian SKA Pathfinder (ASKAP) radio telescope. He is particularly interested in questions involving stellar feedback (supernovae, stellar winds, and so on) and its impact on galaxies and their surrounding intergalactic medium.
Read more about Benjamin Walter Keller

Right arrow

3D and Geospatial Plots

From the previous chapter, we learned about how to plot on non-Cartesian axes and how to plot high dimensional vector field data. This chapter explains how to add 3D axes and plotting on the 3D axes. The significance of this chapter is the basemap method, where we choose between different kinds of map projections.

In this chapter, we will learn about the following:

  • How to set up and manipulate 3D axes
  • The different kinds of 3D plots that Matplotlib provides
  • How to use the basemap class to generate geospatial plots
  • How to apply these plots on map projections
  • How to add geography to the plots

Plotting with 3D axes

We will begin by importing numpy, matplotlib, and pyplot, as shown here:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib notebook
# Set up figure size and DPI for screen demo
plt.rcParams['figure.figsize'] = (6,4)
plt.rcParams['figure.dpi'] = 150

How to add 3D axes to a figure

We will use the Matplotlib inline magic in Jupyter Notebook to create our plotting setup. Begin by making a new figure and add some axes to this figure. Add a subplot, as shown in the following code, along with the projection= '3D' keyword argument:

# Make a Axes3D
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d...

Looking at various 3D plot types

This section describes how to add line and scatter plots and how to add 3D contour types.

How to rotate the camera in 3D plots

We have a parametric spiral curve that ascends along the different axes which is described in the following points:

  1. We have spiral curve along the z axis, as shown in the following code:
# Line plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x,y,z)

Hence, here we see our spiral in the 3D plot:

  1. If we take a look at the view_init method, we have two keyword arguments: elevation, which is given by elev,...

The basemap methods

Before we get started, let's think a little bit about map projections. We have already seen the visualized data showing population density in the United States:

It is known that the earth is a sphere, but your screen is not a sphere, it's actually a flat Euclidean plane. So, translating points from the surface of a sphere onto the points on the surface of this flat plane is actually non-trivial. We can't unroll a sphere into a flat plane without tearing or distorting that sphere, and so, most of the time when you're dealing with a map, you're actually looking at a projection.

But Mercator is only one of many projections. There are other alternative projections, and even projections that don't necessarily end up giving you square edges for your globe, as shown here:

One of the key things when setting up a map is choosing which...

Plotting on map projections

This section describes how to plot on the map projections and how to draw a day/night terminator.

How to add simple points and lines to our plots

We can see, from the preceding map, that we have Europe in the middle. We will plot some data points and some curves with lines all over Europe. We will begin by putting down a cross or a point. So the latitude and longitude of Heidelberg is 8.7 degrees east and 49.5 degrees north.

We will start with the scatter method. So the scatter behaves just like we are used to it behaving from the standard Cartesian Euclidean projections. Input the code as follows:

# Projecting with and without latlon
m = Basemap(width=1.2e7,height=9e6,projection='lcc&apos...

Adding geography

This section describes how to add coastline and water features along with adding political boundaries.

How to add coastline and water features

We will begin by drawing coastlines using the following code:

# Just coastlines
m = Basemap(projection='ortho',lon_0=-114, lat_0=51)
m.drawcoastlines()
plt.show()

In the previous sections, we have seen that basemap can generate a sort of background image of coastlines of the globe, but we haven't seen how this is done. It is done simply by using the coastlines method in the preceding snippet. Using this gives us the coastlines, as shown in the following output:

In our preceding output, we see that the coastlines are fairly coarse. We can change how coarse...

Summary

In this chapter, we learned how to plot on 3D axes and how to make various different kinds of plots within those 3D axes. We also studied how to use basemap to generate map projections between the curved surface of the earth and the flat surface of our screens, how to plot onto these map projections and, finally, how to use basemap to add geographical features to the plots that we produced.

In the next chapter, we will learn about interactive plotting, whereby we can make plots that are no longer static images, but actual interactive applications.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Matplotlib 2.x
Published in: Nov 2018Publisher: PacktISBN-13: 9781789617696
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Benjamin Walter Keller

Benjamin Walter Keller is currently a PhD candidate at McMaster University and gained his BSc in physics with a minor in computer science from the University of Calgary in 2011. His current research involves numerical modeling of galaxy evolution over cosmological timescales. As an undergraduate at the U of C, he worked on stacking radio polarization to examine faint extragalactic sources. He also worked in the POSSUM Working Group 2 to determine the requirements for stacking applications for the Australian SKA Pathfinder (ASKAP) radio telescope. He is particularly interested in questions involving stellar feedback (supernovae, stellar winds, and so on) and its impact on galaxies and their surrounding intergalactic medium.
Read more about Benjamin Walter Keller