Pages

Saturday, September 24, 2016

Cleaner Code

In light of Gene Callahan's comment yesterday, I have posted some cleaner code below. Those who are not programmers may not understand the process of passing objects through multiple methods (i.e., "plotLines (. . .)). If you don't get it, don't worry about it. Those who prefer tighter organization should use this code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

pp = PdfPages('SupplyAndDemandFloor.pdf')
def supplyAndDemandFloor(supply, demand, floor, equilibrium):
    
    fig = plt.figure(dpi=128, figsize=(10,6))
    plotLines(supply, demand, equilibrium, floor)        
    frame = plt.gca()        
    plt.title('Pizza Market', fontsize = 32)
    placeText()
    setupAxes(frame)  
    pp.savefig(fig)
    pp.close()

def placeText():
    #plt.text(x,y,text,fontsize)
    plt.text(-500, 10000, "$p$", fontsize=24)
    plt.text(-550, 7900, "$p_s$", fontsize=24)
    plt.text(-550, 5000, "$p_e$", fontsize=24)
    plt.text(8200, 8800,"$S$", fontsize = 24)
    plt.text(8200, 2000,"$D$", fontsize = 24)
    plt.text(1800, -650, "$Q_d$", fontsize=24)
    plt.text(7800, -650, "$Q_s$", fontsize=24)
    plt.text(4800, -650, "$Q_e$", fontsize=24)
    plt.text(10000, -650, "$Q$", fontsize=24)

def plotLines(supply, demand, equilibrium, floor):
    # plt.plot((x1,x2), (y1,y2), linestyle/color, linewidth)
    plt.plot((2000, 2000), (0, 8000), 'r--', linewidth=1.5)  
    plt.plot((8000, 8000), (0, 8000), 'r--', linewidth=1.5)  
    plt.plot((5000, 5000), (0, 5000), 'k--', linewidth=1.5)      
    plt.plot(supply,  'k-', linewidth=3)
    plt.plot(demand, 'k-', linewidth=3)
    plt.plot(equilibrium, 'k--', linewidth=1.5)
    plt.plot(floor, 'r--',label= "Price Floor", linewidth=1.5)

def setupAxes(frame):
    frame.axes.get_xaxis().set_visible(False)
    frame.axes.get_yaxis().set_visible(False)
    plt.xlabel("Labor", fontsize=20)
    plt.ylabel("Wage", fontsize = 20)
    plt.tick_params(axis='both', which='major', labelsize=16)

Supply = np.arange(10000)
Demand = np.arange(10000,0, -1)
priceFloor = np.arange(1, 10000)
priceFloor[priceFloor > 0] = 8000
priceEquilibrium = np.arange(1, 5000)
priceEquilibrium[priceEquilibrium > 0] = 5000
supplyAndDemandFloor(Supply, Demand, priceFloor, priceEquilibrium)

No comments:

Post a Comment