在python中绘制函数

时间:2018-11-05 10:35:17

标签: python

Python对我来说是一种新语言,我必须绘制此函数:

Function

其中

k2

但是我不知道该怎么做!

import numpy as np

import matplotlib.pyplot as plt

我将绘制T(E) E> U E在(10-20)* 10 ^ -19之间,U在9 * 10 ^ -19之间; h = 1.34 * 10 ^ -34,m = 1.6 * 10 ^ -19; a = 2 * 10 ^ -9

1 个答案:

答案 0 :(得分:0)

好的,这就是你想要做的

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 = fig.add_subplot(111, projection='3d')

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

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

输出看起来像这样:

3d plot

编辑:

有新要求E in (10 - 20)*10^-19U = 9*10^-19

使用与以前相同的t(e, u)

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()

输出:

2d plot