# Plot a function in python

Ok so here is what you want to do

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D  # for 3d plotting

# constants
h = 1.34e-34
m = 1.6e-19
a = 2e-9

# define our function (python standard uses lowercase for funcs/vars)
def t(e, u):
k2 = np.sqrt(2 * m * (e - u) / h**2)
return 1 / (1 + ((u**2 * np.sin(k2 * a / 2)**2) / (4 * e * (e - u))))

# this is just a general function I use which quickly creates values needed
# in 3D plotting
def grid(xs, ys, func):
"""
Returns a tuple of (x, y, func(x, y)) points for use in 3d plotting
using vectorized arithmetic for speed
"""
if xs.ndim != ys.ndim != 1:
raise ValueError(f'xs and ys must have 1 dimension, '
f'not xs.dim={xs.ndim}, ys.ndim={ys.ndim}')
x, y = np.meshgrid(xs, ys)
z = func(x, y)
return x, y, z

# create arrays in of the right range with some sensible discretisation
array_size = 100
es = np.linspace(10, 20, array_size)
us = np.linspace(1, 9, array_size)
x, y, z = grid(es, us, t)

# create the 3d plot
fig = plt.figure()

ax.set_xlabel('E')
ax.set_ylabel('U')
ax.set_zlabel('T')

ax.plot_surface(x, y, z)
plt.show()


The output looks like this: ## Edit:

With new requirements E in (10 - 20)*10^-19 and U = 9*10^-19

Using the same t(e, u) as before:

es = np.linspace(10e-19, 20e-19, array_size)
u = 9e-19
z = t(es, u)

fig, ax = plt.subplots()

ax.set_xlabel('E')
ax.set_ylabel('T(E)')

ax.plot(es, z)
plt.show()


outputs: 