Stem plots#
import numpy as np
import matplotlib.pyplot as plt
You have probably used the matplotlib plot types plot, hist and scatter frequently; another type is stem, which is useful for indicating the magnitude of various points along a number line. As with a scatter plot, the data does not need to be ordered, and missing values are easy to handle.
\(\text{Task 3.1:}\)
Run the cell below and play with the values to make sure you are comfortable with the stem plot type. Do you see how it can handle two values with the same index?
value = [2, 7, 5, 1, 8]
index = [0, 2, 2, 6, 4]
plt.plot(index, value, 'o')
plt.stem(index, value);
Bringing it all together: Earthquake analysis#
To put the ideas above into practice, consider the following data, in the form of three lists, that describes the largest earthquakes in the world since 2000, that are also located on the ring of fire (Source: Wikipedia).
year = [2003, 2011, 2013,
2006, 2022,
2017, 2019, 2001, 2010, 2015,
2014, 2022, 2016,
2000, 2007, 2012, 2005, 2004]
magn = [8.3, 9.1, 8.3,
8.3, 7.6,
8.2, 8.0, 8.4, 8.8, 8.3,
8.2, 7.6, 7.9,
8.0, 8.4, 8.6, 8.6, 9.1]
site = ['Japan, Hokkaidō',
'Japan, Honshu',
'Russia, Sea of Okhotsk',
'Russia, Kuril Islands',
'Mexico, Michoacán',
'Mexico, Chiapas',
'Peru, Loreto',
'Peru, Arequipa',
'Chile, Concepción',
'Chile, Coquimbo',
'Chile, Iquique',
'Papua New Guinea, Morobe',
'Papua New Guinea, New Ireland',
'Papua New Guinea',
'Indonesia, Sumatra',
'Indonesia, Indian Ocean',
'Indonesia, Simeulue',
'Indonesia, Sumatra']
We can visualize the data in the following figure. Can you find an easy way to recreate this plot?
\(\text{Task 3.2:}\)
Using the tools described in this assignment, recreate the plot above. It doesn’t have to be perfect Note that to avoid cluttering the figure, the detailed earthquake information is only plotted for years that are a multiple of 5 (i.e., 2000, 2005, etc.).
The code cell below already contains the required code to generate the svg file.
Hints: create a for loop using 3 iterables (x, y and z) based on one of these iterable tools: range, enumerate or zip. Use the modulo operator % to choose the 5-year multiples. An outline of the solution in pseudo-code is also provided here, each of which should be one line in your code (except item 5):
1 for each iterable item
2 if the year is a multiple of 5
3 create the label (string)
4 annotate the plot (i.e., annotate(...))
5 include any additional formatting (more than one line)
YOUR_CODE_LINES_HERE
plt.savefig('my_earthquake.svg') # Don't remove this line
By Robert Lanzafame, Delft University of Technology. CC BY 4.0, more info on the Credits page of Workbook.