Economics#

Plotting graphs#

import numpy as np
import pandas as pd
from bokeh.io import output_notebook, show
from bokeh.palettes import Spectral4
from bokeh.plotting import figure, show
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 3
      1 import numpy as np
      2 import pandas as pd
----> 3 from bokeh.io import output_notebook, show
      4 from bokeh.palettes import Spectral4
      5 from bokeh.plotting import figure, show

ModuleNotFoundError: No module named 'bokeh'
output_notebook()
Loading BokehJS ...
p = figure(width=600, height=300)
x=np.arange(0,6,0.01)
y=x-3*(x**(1/3))
p.line(x, y, line_width=2, alpha=0.9, color = Spectral4[2], legend_label=r"Graph of f")
p.line(-x, -y, line_width=2, alpha=0.9, color = Spectral4[2])

p.title.text = 'Graph'
p.xaxis.axis_label = r'$$x\rightarrow$$'
p.yaxis.axis_label = r'$$y\rightarrow$$'
p.legend.click_policy="mute"

show(p)
x = np.arange(0.0,10.0,0.01)


def f1(x):
    return (-1)*( (x*(x >=(2*x - 4))) + ((2*x - 4)*(x <(2*x - 4)) ))


def f2(x):
    return 3


def f3(x):
    return ( (x*(x <=(2*x - 4))) + ((2*x - 4)*(x >(2*x - 4)) ))


df = pd.DataFrame(x)
df.columns = ["x"]
df["y1"] = f1(x)
df["y2"] = f2(x)
df["y3"] = f3(x)
p = figure(width=400, height=400)


for i in range(1,4):
    p.line(df.iloc[:,0],df.iloc[:,i], line_width=2, alpha=0.9, color = Spectral4[i-1], legend_label="f{}".format(i))

p.title.text = 'Graphs'
p.xaxis.axis_label = r'$$x\rightarrow$$'
p.yaxis.axis_label = r'$$y\rightarrow$$'
p.legend.location = "bottom_left"
p.legend.click_policy="mute"

show(p)
pX = [1,1,2]
pY = [1,2,1]
M = [2,4,10]
p = figure(width=400, height=400)
p.title.text = 'Budget Lines'

for m, px, py, color in zip(M, pX, pY, Spectral4):
    p.line([m/px,0], [0,m/py], line_width=2, alpha=0.8, color=color,
           muted_alpha=0.2, muted_color=color, legend_label="(pX={}, pY={}, M={})".format(px,py,m))

p.legend.click_policy="mute"

show(p)
pX = np.arange(0.7,3,0.01)
pY = 1
M = 1

# u1 = xy
def x1(pX, pY, M):
    return M/(2*pX)

# u2 = min(x,y)
def x2(pX, pY, M):
    return M/(pX+pY)

# u3 = x+y
def x3(pX, pY, M):
    return (M/pX)*(pX <= pY)


df = pd.DataFrame(pX)
df.columns = ["pX"]
df["xd1"] = x1(pX, pY, M)
df["xd2"] = x2(pX, pY, M)
df["xd3"] = x3(pX, pY, M)
p = figure(width=400, height=400)


for i in range(1,4):
    p.line(df.iloc[:,i], df.iloc[:,0], line_width=2, alpha=0.9, color = Spectral4[i-1], legend_label="u{}".format(i))

p.title.text = 'Demand for X'
p.xaxis.axis_label = r'$$x^d\rightarrow$$'
p.yaxis.axis_label = r'$$p_X\rightarrow$$'
p.legend.click_policy="mute"

show(p)
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider


pX = list(np.arange(0.1,3,0.01))
pY = 1
M = 5
x = [M/(2*pX) for pX in pX]

source = ColumnDataSource(data=dict(pX=list(pX), x=list(x)))

plot = figure(plot_width=400, plot_height=400, x_range=(0,3), y_range=(0, 3))
plot.line('x', 'pX', source=source, line_width=3, line_alpha=0.6)

slider = Slider(start=0.5, end=8, value=5, step=.1, title="Income")

update_curve = CustomJS(args=dict(source=source, slider=slider), code="""
    var data = source.data;
    var M = slider.value;
    var pX = data['pX']
    var x = data['x']
    function demand(p, m) {
        return m/(2*p);
    }
    for (var i = 0; i < pX.length; i++) {
        x[i] = demand(pX[i], M)
    }
    
    // necessary becasue we mutated source.data in-place
    source.change.emit();
""")
slider.js_on_change('value', update_curve)
plot.title.text = 'Demand for X'
plot.xaxis.axis_label = r'$$x^d\rightarrow$$'
plot.yaxis.axis_label = r'$$p_X\rightarrow$$'

show(column(slider, plot))
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider


pX = list(np.arange(0.01,3,0.01))
pY = 1
M = 5
x = [(M/pX)*(pX <= pY) for pX in pX]

source = ColumnDataSource(data=dict(x=list(x),pX=list(pX)))

plot = figure(plot_width=400, plot_height=400, x_range=(-0.1,10), y_range=(0, 3))
plot.line('x','pX',  source=source, line_width=3, line_alpha=0.6)

slider = Slider(start=0.5, end=8, value=5, step=.1, title="Income")

update_curve = CustomJS(args=dict(source=source, slider=slider), code="""
    var data = source.data;
    var M = slider.value;
    var x = data['x']
    var pX = data['pX']
    var q = 1
    function demand(p, m) {
        return (m/(p))*(p<=q);
    }
    for (var i = 0; i < pX.length; i++) {
        x[i] = demand(pX[i], M)
    }
    
    // necessary becasue we mutated source.data in-place
    source.change.emit();
""")
slider.js_on_change('value', update_curve)
plot.title.text = 'Demand for X'
plot.xaxis.axis_label = r'$$x^d\rightarrow$$'
plot.yaxis.axis_label = r'$$p_X\rightarrow$$'

show(column(slider, plot))