++++Notebook converted from Jupyter for blog publishing.
04-Matplotlib Exercises
# CODE HERE# CODE HEREPart Two: Plotting E=mc^2
Now that we have the arrays E and m, we can plot this to see the relationship between Energy and Mass.
TASK: Import what you need from Matplotlib to plot out graphs:
TASK: Recreate the plot shown below which maps out E=mc^2 using the arrays we created in the previous task. Note the labels, titles, color, and axis limits. You don't need to match perfectly, but you should attempt to re-create each major component.
# CODE HERE# DON'T RUN THE CELL BELOW< THAT WILL ERASE THE PLOT!Part Three (BONUS)
Can you figure out how to plot this on a logarthimic scale on the y axis? Place a grid along the y axis ticks as well. We didn't show this in the videos, but you should be able to figure this out by referencing Google, StackOverflow, Matplotlib Docs, or even our "Additional Matplotlib Commands" notebook. The plot we show here only required two more lines of code for the changes.
# CODE HERE# DONT RUN THE CELL BELOW! THAT WILL ERASE THE PLOT!Task Two: Creating plots from data points
In finance, the yield curve is a curve showing several yields to maturity or interest rates across different contract lengths (2 month, 2 year, 20 year, etc. ...) for a similar debt contract. The curve shows the relation between the (level of the) interest rate (or cost of borrowing) and the time to maturity, known as the "term", of the debt for a given borrower in a given currency.
The U.S. dollar interest rates paid on U.S. Treasury securities for various maturities are closely watched by many traders, and are commonly plotted on a graph such as the one on the right, which is informally called "the yield curve".
For this exercise, we will give you the data for the yield curves at two separate points in time. Then we will ask you to create some plots from this data.
Part One: Yield Curve Data
We've obtained some yeild curve data for you from the US Treasury Dept. (opens in a new tab). The data shows the interest paid for a US Treasury bond for a certain contract length. The labels list shows the corresponding contract length per index position.
TASK: Run the cell below to create the lists for plotting.
labels = ['1 Mo','3 Mo','6 Mo','1 Yr','2 Yr','3 Yr','5 Yr','7 Yr','10 Yr','20 Yr','30 Yr']
july16_2007 =[4.75,4.98,5.08,5.01,4.89,4.89,4.95,4.99,5.05,5.21,5.14]
july16_2020 = [0.12,0.11,0.13,0.14,0.16,0.17,0.28,0.46,0.62,1.09,1.31]TASK: Figure out how to plot both curves on the same Figure. Add a legend to show which curve corresponds to a certain year.
# CODE HERE# DONT RUN THE CELL BELOW! IT WILL ERASE THE PLOT!TASK: The legend in the plot above looks a little strange in the middle of the curves. While it is not blocking anything, it would be nicer if it were outside the plot. Figure out how to move the legend outside the main Figure plot.
# CODE HERE# DONT RUN THE CELL BELOW! IT WILL ERASE THE PLOT!TASK: While the plot above clearly shows how rates fell from 2007 to 2020, putting these on the same plot makes it difficult to discern the rate differences within the same year. Use .suplots() to create the plot figure below, which shows each year's yield curve.
# CODE HERE# DONT RUN THE CELL BELOW! IT WILL ERASE THE PLOT!BONUS CHALLENGE TASK: Try to recreate the plot below that uses twin axes. While this plot may actually be more confusing than helpful, its a good exercise in Matplotlib control.
# CODE HERE# DONT RUN THE CELL BELOW! IT ERASES THE PLOT!