Module 5 Assignment

A few things you should keep in mind when working on assignments:

  1. 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.

  2. 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).

  3. Do not change the title (i.e. file name) of this notebook.

  4. Make sure that you save your work (in the menubar, select FileSave and CheckPoint)

In [ ]:
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

Problem 1: Plotting a Function

Plot the output of $x^2 + 3 - log(x)$ which is saved in the below variable called $y$.

In addition:

  1. Title the plot "Plot of $y = x^2 + 3 - log(x)$"

  2. Label the x-axis as "x"

  3. Label the y-axis as "y"

In [ ]:
# generate x values to calculate the function at
x = np.linspace(0, 2, num=100)

# calculate y
y = x**2 + 3 - np.log(x)
In [ ]:
fig, ax = plt.subplots()

### YOUR CODE HERE
In [ ]:
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)$")

Problem 2: Plotting Multiple Functions

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:

  1. Title the plot "Plot of $y = x^2 + 3 - log(x)$ and $y = x^2 + 3 - log(x)$"

  2. Label the x-axis as "x"

  3. Label the y-axis as "y"

In [ ]:
# generate x values to calculate the function at
x = np.linspace(0, 2, num=100)

# calculate y
y2 = x**2 + 3 + np.log(x)
In [ ]:
fig, ax = plt.subplots()

### YOUR CODE HERE
In [ ]:
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)

Problem 3: Adding Legends

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:

  1. Title the plot "Plot of $y = x^2 + 3 - log(x)$ and $y = x^2 + 3 + log(x)$"

  2. Label the x-axis as "x"

  3. Label the y-axis as "y"

  4. Change the color of the plot for y2 to red

  5. 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.

In [ ]:
fig, ax = plt.subplots()

### YOUR CODE HERE
In [ ]:
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.

In [ ]:
df = pd.read_csv('dow_jones_index.data')
df.head()

Problem 4: Plotting the Distribution of Days Until Next Dividend for Stocks in the Dow Jones Index Using Rugplot

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:

  • Create a 1-d rugplot of the column called "days_to_next_dividend"
  • Remove the numbers on the y-axis
  • Set a title for your plot
  • The function should return an axes object (we have created this variable for you already)
In [ ]:
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
In [ ]:
rp = rug_plot(df)
plt.show()
plt.close()
In [ ]:
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.")

Problem 5: Comparing the Current Week's Open to Next Week's Open in the Dow Jones Index Using a Comparative Histogram.

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:

  • Create a 1-d Comparative Histogram (Not Multiple Histograms) of columns open and next_weeks_open
  • Create a label for the y axis and the x axis
  • Set a title for your plot
  • Create a legend
  • The function should return an axes object (we have created this variable for you already)
In [ ]:
def plot_compare_hist(df):
    fig, ax = plt.subplots()
    
    ### YOUR CODE HERE
    
    return ax
In [ ]:
ch = plot_compare_hist(df)
In [ ]:
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.