I made equation and charts of Cobb-Douglas production function to test Sympy.

var() registers symbols to the glossary of Sympy at once.

.subs({}) can replace some symbols in a equation with numbers, while .evalf(subs={}) must take all of variables to convert it into number.

plotting.plot3d can render 3D surface as though a cross-section of a boulder.

from sympy import * import numpy as np from matplotlib.pylab import plt %matplotlib inline init_printing(use_latex=True) # Register symbols: var("L K Y A a") # Cobb-Douglass production function: Y = ( A*(L**a) ) * ( K**(1-a) ) # Assign number to A and a: Ys = Y.subs({A: 10, a:0.6}) # Plot 3D chart in which K and L are changed 0 to 10: plotting.plot3d(Ys, (K,0,10), (L,0,10))

The code above is simple and behaves frugally. But in many times I want to get data to visualize it.

So I also tested how to make data from Sympy equation. Lambdify() generates python function from Sympy function. By the function, I made 2D matrix data and heatmap chart.

# Turn sympy symbols into python function: Ys_func = lambdify((K,L), Ys, "numpy") # Make 2D permutation list with K=0~10 and L=0~10: K_n = np.linspace(0,10, 50) L_n = np.linspace(0,10, 50) result = [] for k in K_n: result_j = [] for l in L_n: result_j.append(Ys_func(k,l)) result.append(result_j) result = np.array(result) # Plot 2D heat map: plt.matshow(result)

Great post. I’m going to mention it at the next PyData Calgary meetup.

You can replace the nested for loop with a nested list comprehension:

result = np.array([[Ys_func(k,l) for l in L_n] for k in K_n])

Thank you for the suggestion about the code. It’s more optimal and simple way than mine.

And If the post will be mentioned at PyData meetup, I feel honored.