## cdi-graphics-305

# Load numpy and pyplot
import numpy as np
import matplotlib.pyplot as plt

# Key functions
def h_(p): return -np.log(p)/np.log(2) # entropy of an event of prob p
def ln(x): return np.log(x)            # natural log.

# hline draws a horizontal line fron (a,h) to (b,h)
# The default width and dashing style are 1 and 'k-'
def hline(a,b,h,lw=1,dash='k-'):
    x = np.arange(a,b+0.0001,b-a)
    return plt.plot(x,h+0*x,dash, lw=lw)

# vline draws a vertical line fron (d,a) to (d,b)
# The default width and dashing style are 1 and 'k-'
def vline(a,b,d,lw=1, dash='k-'):
    y = np.arange(a,b+0.0001,b-a)
    return plt.plot(d+0*y,y,dash, lw=lw)

# To tick and label the vertical axis
def htick(X, T, run=0.03, lw='1'):
    plt.plot([X[0]-run, X[0]+run], [X[1],X[1]], lw=lw, color='black')
    write((X[0]-2*run,X[1]), T, ha='right', fs='13')
    return None

# To tick and label the horizontal axis
def vtick(X, T, run=0.03, lw='1'):
    plt.plot([X[0], X[0]], [X[1]-run,X[1]+run], lw=lw, color='black')
    write((X[0],X[1]-3*run), T, fs='13')
    return None

# Utility to write text T at position X with
# four parameters with default values
def write(X,T,ha='center', va = 'center', fs='16', rot=0.0):
    return plt.text(X[0],X[1], T, horizontalalignment=ha,verticalalignment=va, fontsize=fs, rotation=rot)

#  Close all plt canvasses
plt.close("all")

#---------------
# 1. Graphing h_(p) (see T2.3)
#---------------

# Set up graphical window
plt.figure("h_(p)", figsize=(3.5,8)) 

# graph of h_
x = np.arange(0.055,1.01,0.01)
plt.plot(x, h_(x),'r-', lw=2)

# range of axis
xmin = -0.5; xmax = 1.1
ymin = -0.25; ymax = 4.5

plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)

# decorations
hline(0,1.05,0) # horizontal axis
vline(0,4.25,0) # vertical axis

hline(0,0.5,1)  # to mark h-coordinate of (0.5,1)
vline(0,1,0.5)  # to mark v-coordinate of (0.5,1)

p=0.3; q=h_(p)
hline(0,p,q,dash='g:')   # to mark h-coordinate of (p,q)
vline(0,q,p,dash='g:')   # to mark v-coordinate of (p,q)
write((p,-0.1), '$p$')
write((0.5,-0.1), '1/2', fs='13')
write((1,-0.1), '1', fs='13')
write((-0.1,h_(p)),'$-\\log_2(p)$', ha='right')

plt.plot([0.5], [h_(0.5)], 'o', [p], [q], 'bo')  # two distinguished points on graph


for k in range(1,5):    # 4 ticks on v-axis
    htick((0,k), str(k))

# do not diplay axis
plt.axis('off')

# save the image to the file h_.png, with 
plt.savefig("D:\\Docencia\\2014b\\png\\h_.png", bbox_inches='tight')


#---------------
# 2. Graphing ln(x) and tangent at x=1 (see T2.10)
#---------------

# Set up graphical window
plt.figure("ln(x) and tangent at x=1", figsize=(9,7)) 

# graph of ln(x)
x = np.arange(0.25,2.5,0.01)
plt.plot(x, ln(x),'b-', lw=2)
# graph of x-1 (tangent)
x = np.arange(0,2.1,2)
plt.plot(x, x-1,'b-', lw=1)

# range of axis
xmin = -0.25; xmax = 3
ymin = -1.40; ymax = 1.25

plt.xlim(xmin,xmax)
plt.ylim(ymin,ymax)

# decorations
hline(-0.25,3,0) # horizontal axis
vline(-1.5,1.25,0) # vertical axis

for k in (-1,1):    # 2 ticks on v-axis
    htick((0,k), str(k))
for k in (1,2):    # 2 ticks on h-axis
    vtick((k,0), str(k))

write((1.8,0.9), '$y=x-1$', ha='right')
write((2.25,0.6), '$y=\\ln(x)}$')


plt.plot([1], [0], 'ro')  # one distinguished point on graph

# long text
write((1.0, -1.0), 
'''The tangent to $y=\\ln(x)$ at $x=1$ has
equation $y=x-1$. The derivative of
$(x-1)-\\ln(x)$ is $1-1/x$ and so it is
negative for $0<x<1$, 0 for $x=1$
and positive for $x>1$. This implies
that $\\ln(x)<x-1$ for $x \\neq 1$.''', fs='14', ha='left')

# do not diplay axis
plt.axis('off')

# save the image to the file h_.png, with 
plt.savefig("D:\\Docencia\\2014b\\png\\ln.png", bbox_inches='tight')

#Show everything
plt.show()