Class03 Answer:

Plot a straight line from first price to last price.

I remember plotting straight lines from Algebra Class.


This script should use Pandas to plot prices of GSPC for 2016.
This script should Plot a straight line from first price to last price.

import pandas as pd
import matplotlib.pyplot as plt

csvfile   = ''
cp_df     = pd.read_csv(csvfile).sort_values(['cdate'])
cp2016_sr = (cp_df.cdate > '2016') & (cp_df.cdate < '2017')
cp2016_df = cp_df[['cdate','cp']][cp2016_sr]

# Algebra and Geometry say a line is described by
# y = mx + b
# m, the slope is (y1-y0)/(x1-x0):

# Useful concept:
# If I use integers (starting at 0) as X-values,
# then my syntax is simpler.

# (x1-x0) is:
x1x0_i = len(cp2016_df)
# (y1-y0) is:
y1y0_f = cp2016_df.iloc[-1].cp-cp2016_df.iloc[0].cp

# m, the slope is (y1-y0)/(x1-x0):
m_f = y1y0_f / x1x0_i
# And b is the y intercept:
b_f = cp2016_df.iloc[0].cp

# My equation for straight line:
def yval(x_in):
    return m_f * x_in + b_f

# I should collect points to plot straight line:
yvals_l = [ yval(x_i) for x_i in range(len(cp2016_df))]

# Add the points to the DataFrame:
cp2016_df['sl'] = yvals_l

# To make a better plot I should convert X-values from integers to series of strings:
cpdate2016_df = cp2016_df.set_index(['cdate'])
# I should plot cp (closing price), and straight line:
cpdate2016_df.plot.line(title="GSPC 2016")


Class03 Lab About Blog Contact Class01 Class02 Class03 Class04 Class05 Class06 Class07 Class08 Class09 Class10 dan101 Forum Google Hangout Vboxen