A few things you should keep in mind when working on assignments:
Make sure you fill in any place that says YOUR CODE HERE
. Do not write your answer in anywhere else other than where it says YOUR CODE HERE
. Anything you write anywhere else will be removed or overwritten by the autograder.
Before you submit your assignment, make sure everything runs as expected. Go to menubar, select Kernel, and restart the kernel and run all cells (Restart & Run all).
Do not change the title (i.e. file name) of this notebook.
Make sure that you save your work (in the menubar, select File → Save and CheckPoint)
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as mpl
import matplotlib.pyplot as plt
from nose.tools import assert_equal, assert_almost_equal
from nose.tools import assert_equal, assert_is_instance, assert_is_not
%matplotlib inline
Plot the output of $x^2 + 3 - log(x)$ which is saved in the below variable called $y$.
In addition:
Title the plot "Plot of $y = x^2 + 3 - log(x)$"
Label the x-axis as "x"
Label the y-axis as "y"
# generate x values to calculate the function at
x = np.linspace(0, 2, num=100)
# calculate y
y = x**2 + 3 - np.log(x)
fig, ax = plt.subplots()
### YOUR CODE HERE
assert_equal(ax.get_xlabel(), 'x')
assert_equal(ax.get_ylabel(), 'y')
assert_equal(ax.get_title(), "Plot of $y = x^2 + 3 - log(x)$")
Plot the output of $x^2 + 3 - log(x)$ which is saved in the variable $y$ and plot the out of $x^2 + 3 + log(x)$ which is saved in the below variable called $y2$.
In addition:
Title the plot "Plot of $y = x^2 + 3 - log(x)$ and $y = x^2 + 3 - log(x)$"
Label the x-axis as "x"
Label the y-axis as "y"
# generate x values to calculate the function at
x = np.linspace(0, 2, num=100)
# calculate y
y2 = x**2 + 3 + np.log(x)
fig, ax = plt.subplots()
### YOUR CODE HERE
assert_equal(len(ax.lines), 2)
if len(ax.lines[0].get_data()) != 2:
assert_equal(ax.lines[0].get_data()[1],99)
else:
assert_equal(min(ax.lines[0].get_data()[1]),3.8465735928827005)
In the above plot it's not clear which line corresponds to which function. To fix this issue we can use a legend. Make a new plot showing both $y = x^2 + 3 - log(x)$ and $ y = x^2 + 3 + log(x)$.
Furthermore:
Title the plot "Plot of $y = x^2 + 3 - log(x)$ and $y = x^2 + 3 + log(x)$"
Label the x-axis as "x"
Label the y-axis as "y"
Change the color of the plot for y2 to red
Add a legend so that the line for y2 is labeled "adding log(x)" and the line for y is labeled "subracting log(x)". IMPORTANT: Make sure you add the legend text in the same command as making the plot. See the corresponding notebook if you're confused. This is the generally preferred practice for labeling the legend.
fig, ax = plt.subplots()
### YOUR CODE HERE
assert_equal(ax.get_title(), "Plot of $y = x^2 + 3 - log(x)$ and $y = x^2 + 3 + log(x)$")
if ax.lines[1].get_color() == 'red':
assert_equal(ax.lines[1].get_color(), 'red')
else:
assert_equal(ax.lines[0].get_color(), 'red')
if ax.legend().get_texts()[1].get_text() == 'adding log(x)':
assert_equal(ax.legend().get_texts()[1].get_text(), 'adding log(x)')
else:
assert_equal(ax.legend().get_texts()[0].get_text(), 'adding log(x)')
For the next few problems we will use the dow jones index data, which is loaded in the below cell.
df = pd.read_csv('dow_jones_index.data')
df.head()
For this problem we have loaded in weekly stock data from dow jones index, it is stored in a variable called df and the first 5 rows of data are shown in a table above.
Your task is to finish the plot function below. This function should do the following:
def rug_plot(df):
'''
df - dataframe containg dow jones weekly data
function returns axes object
'''
fig, ax = plt.subplots(figsize=(10, 1.5))
### YOUR CODE HERE
return ax
rp = rug_plot(df)
plt.show()
plt.close()
assert_is_instance(rp, mpl.axes.Axes, msg='Return a Axes object.')
assert_is_not(len(rp.title.get_text()), 0, msg="Your plot doesn't have a title.")
assert_is_not(rp.xaxis.get_label_text(), '', msg="Change the x-axis label to something more descriptive.")
assert_equal(rp.yaxis.get_label_text(), '', msg="The y-axis should not have a label.")
assert_equal(rp.yaxis.get_ticklabels(), [], msg="Remove ticks from y-axis label.")
For this problem we will continue to use the weekly stock data from dow jones index, which is still stored in a variable called df . We want to compare the open column with next_weeks_open column.
Your task is to finish the plot_compare_hist function below. This function should do the following:
def plot_compare_hist(df):
fig, ax = plt.subplots()
### YOUR CODE HERE
return ax
ch = plot_compare_hist(df)
assert_is_instance(ch, mpl.axes.Axes, msg='Return a Axes object.')
assert_is_not(len(ch.title.get_text()), 0, msg="Your plot doesn't have a title.")
assert_is_not(ch.xaxis.get_label_text(), '', msg="Change the x-axis label to something more descriptive.")
assert_is_not(ch.yaxis.get_label_text(), '', msg="Change the y-axis label to something more descriptive.")
assert_is_instance(ch.legend_, mpl.legend.Legend, msg="Your plot doesn't have a Legend")
© 2017: Robert J. Brunner at the University of Illinois.
This notebook is released under the Creative Commons license CC BY-NC-SA 4.0. Any reproduction, adaptation, distribution, dissemination or making available of this notebook for commercial use is not allowed unless authorized in writing by the copyright holder.