简体   繁体   English

如何绘制这些数据?

[英]How can I plot this data?

I am trying to plot this data: 我正在尝试绘制此数据:

h = 1
m = 1

E1 = (((h**2)/(2*m)) * ((((1*np.pi)/2)+((1*np.pi)/2))**2))
E2 = (((h**2)/(2*m)) * ((((2*np.pi)/2)+((2*np.pi)/2))**2))
E3 = (((h**2)/(2*m)) * ((((3*np.pi)/2)+((3*np.pi)/2))**2))
E4 = (((h**2)/(2*m)) * ((((4*np.pi)/2)+((4*np.pi)/2))**2))

k1 = ((((1*np.pi)/2)+((1*np.pi)/2))**2)
k2 = ((((2*np.pi)/2)+((2*np.pi)/2))**2)
k3 = ((((3*np.pi)/2)+((3*np.pi)/2))**2)
k4 = ((((4*np.pi)/2)+((4*np.pi)/2))**2)


E = list[E1, E2, E3, E4]
k = list[k1, k2, k3, k4]

plt.scatter(k,E)
plt.show()

The list function doesn't seem to work for this. list功能似乎对此不起作用。 I don't think it can get the pre-defined values. 我认为它无法获得预定义的值。 Using np.array also doesn't seem to work. 使用np.array似乎也不起作用。

The way you define your lists is the issue. 问题是您定义列表的方式。

Try: 尝试:

E = [E1, E2, E3, E4]
k = [k1, k2, k3, k4]

Or if you want to use numpy: 或者,如果您想使用numpy:

E = np.array([E1, E2, E3, E4])
k = np.array([k1, k2, k3, k4])

Not an answer, just a suggestion but I wanted to write out enough code that wouldn't fit in a comment. 不是答案,只是建议,但我想写出足够多的代码,这些代码不适合注释。

Instead of writing out four nearly identical lines, you can define a function: 您可以定义一个函数,而不必写出几乎相同的四行:

def E(n, h=1.0, m=1.0):
    return (((h**2)/(2*m)) * ((((n*np.pi)/2)+((n*np.pi)/2))**2))

Note that it accepts values for h and m as arguments, but if none are provided, it will use 1.0 as default for both. 请注意,它接受hm值作为参数,但是如果未提供任何值,则它将两者的默认值都设为1.0 This can even be simplified further, just by cleaning up the notation a bit (eg, you have something/2 + something/2 , which is equivalent to something , see my comment on your question) 甚至可以通过简单地清除表示法来进一步简化(例如,您有something/2 + something/2 ,相当于something ,请参阅我对您问题的评论)

def E(n, h=1.0, m=1.0):
    return (.5*h**2/m) * (n*np.pi)**2

And similarly for k : 同样对于k

def k(n):
    return (n*np.pi)**2

The best thing about this, is that you can now do this all at once without manually building that list: 最好的事情是,您现在可以一次完成所有操作,而无需手动构建该列表:

>>> ns = np.arange(1,5)  # this is np.array([1,2,3,4])
>>> E(ns)
array([  4.9348022 ,  19.7392088 ,  44.4132198 ,  78.95683521])
>>> k(ns)
array([   9.8696044 ,   39.4784176 ,   88.82643961,  157.91367042])

Obviously this precise code won't work for you given the parenthesis typo, but I hope it helps with using numpy! 显然,鉴于括号中的拼写错误,这种精确的代码对您不起作用,但我希望它对使用numpy有帮助! This is the entire code at once: 这是一次完整的代码:

import numpy as np
import matplotlib.pyplot as plt

def E(n, h=1.0, m=1.0):
    return (.5*h**2/m) * (n*np.pi)**2

def k(n):
    return (n*np.pi)**2

ns = np.arange(1, 5)
plt.scatter(k(ns), E(ns))
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM