数字格式 python
Number format python
我想要用列表中的值显示图的图例。但我得到的是元素索引而不是值本身。我不知道如何解决它。我指的是 plt.plot
行。感谢您的帮助。
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random(1000)
y = np.random.random(1000)
n = len(x)
d_ij = []
for i in range(n):
for j in range(i+1,n):
a = np.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
d_ij.append(a)
epsilon = np.linspace(0.01,1,num=10)
sigma = np.linspace(0.01,1,num=10)
def lj_pot(epsi,sig,d):
result = []
for i in range(len(d)):
a = 4*epsi*((sig/d[i])**12-(sig/d[i])**6)
result.append(a)
return result
for i in range(len(epsilon)):
for j in range(len(sigma)):
a = epsilon[i]
b = sigma[j]
plt.cla()
plt.ylim([-1.5, 1.5])
plt.xlim([0, 2])
plt.plot(sorted(d_ij),lj_pot(epsilon[i],sigma[j],sorted(d_ij)),label = 'epsilon = %d, sigma =%d' %(a,b))
plt.legend()
plt.savefig("epsilon_%d_sigma_%d.png" % (i,j))
plt.show()
您的代码有点不符合 Python 风格,所以我尽我所知尝试清理它。 numpy.random.random
和 numpy.random.uniform(0, 1)
are basically the same,但是,后者还允许您传递您想要的 return 数组的 shape
,在这种情况下一个包含 1000 行和两列的数组 (1000, 2)
。然后我使用一些魔法将 return 数组的两个列分别分配给同一行中的 x
和 y
。
numpy.hypot
顾名思义,计算x
和y
的连号。它还可以为具有相同大小的数组的每个条目执行此操作,从而为您节省 for 循环,您应该在 Python 中尝试避免这种情况,因为它们非常慢。
您使用 plt
进行所有绘图,只要您只有一个数字就可以,但根据 Python 之一,我建议尽可能明确关键概念:
explicit is better than implicit.
我建议您通读 this guide,尤其是名为“有状态与无状态方法”的部分。我相应地更改了您的命令。
像您所做的那样,使用列表中项目的索引循环遍历列表的项目也是非常不符合 Python 规范的 (for i in range(len(list)): item = list[i]
)。您可以直接引用该项目 (for item in list:
)。
最后,我将您的格式化字符串更改为更方便的 f-strings。阅读 here.
import matplotlib.pyplot as plt
import numpy as np
def pot(epsi, sig, d):
result = 4*epsi*((sig/d)**12 - (sig/d)**6)
return result
# I am not sure why you would create the independent variable this way,
# maybe you are simulating something. In that case, the code below is
# simpler than your version and should achieve the same.
# x, y = zip(*np.random.uniform(0, 1, (1000, 2)))
# d = np.array(sorted(np.hypot(x, y)))
# If you only want to plot your pot function then creating the value range
# like this is just fine.
d = np.linspace(0.001, 1, 1000)
epsilons = sigmas = np.linspace(0.01, 1, num=10)
fig, ax = plt.subplots()
ax.set_xlim([0, 2])
ax.set_ylim([-1.5, 1.5])
line = None
for epsilon in epsilons:
for sigma in sigmas:
if line is None:
line = ax.plot(
d, pot(epsilon, sigma, d),
label=f'epsilon = {epsilon}, sigma = {sigma}'
)[0]
fig.legend()
else:
line.set_data(d, pot(epsilon, sigma, d))
# plt.savefig(f"epsilon_{epsilon}_sigma_{sigma}.png")
fig.show()
我想要用列表中的值显示图的图例。但我得到的是元素索引而不是值本身。我不知道如何解决它。我指的是 plt.plot
行。感谢您的帮助。
import matplotlib.pyplot as plt
import numpy as np
x = np.random.random(1000)
y = np.random.random(1000)
n = len(x)
d_ij = []
for i in range(n):
for j in range(i+1,n):
a = np.sqrt((x[i]-x[j])**2+(y[i]-y[j])**2)
d_ij.append(a)
epsilon = np.linspace(0.01,1,num=10)
sigma = np.linspace(0.01,1,num=10)
def lj_pot(epsi,sig,d):
result = []
for i in range(len(d)):
a = 4*epsi*((sig/d[i])**12-(sig/d[i])**6)
result.append(a)
return result
for i in range(len(epsilon)):
for j in range(len(sigma)):
a = epsilon[i]
b = sigma[j]
plt.cla()
plt.ylim([-1.5, 1.5])
plt.xlim([0, 2])
plt.plot(sorted(d_ij),lj_pot(epsilon[i],sigma[j],sorted(d_ij)),label = 'epsilon = %d, sigma =%d' %(a,b))
plt.legend()
plt.savefig("epsilon_%d_sigma_%d.png" % (i,j))
plt.show()
您的代码有点不符合 Python 风格,所以我尽我所知尝试清理它。 numpy.random.random
和 numpy.random.uniform(0, 1)
are basically the same,但是,后者还允许您传递您想要的 return 数组的 shape
,在这种情况下一个包含 1000 行和两列的数组 (1000, 2)
。然后我使用一些魔法将 return 数组的两个列分别分配给同一行中的 x
和 y
。
numpy.hypot
顾名思义,计算x
和y
的连号。它还可以为具有相同大小的数组的每个条目执行此操作,从而为您节省 for 循环,您应该在 Python 中尝试避免这种情况,因为它们非常慢。
您使用 plt
进行所有绘图,只要您只有一个数字就可以,但根据 Python 之一,我建议尽可能明确关键概念:
explicit is better than implicit.
我建议您通读 this guide,尤其是名为“有状态与无状态方法”的部分。我相应地更改了您的命令。
像您所做的那样,使用列表中项目的索引循环遍历列表的项目也是非常不符合 Python 规范的 (for i in range(len(list)): item = list[i]
)。您可以直接引用该项目 (for item in list:
)。
最后,我将您的格式化字符串更改为更方便的 f-strings。阅读 here.
import matplotlib.pyplot as plt
import numpy as np
def pot(epsi, sig, d):
result = 4*epsi*((sig/d)**12 - (sig/d)**6)
return result
# I am not sure why you would create the independent variable this way,
# maybe you are simulating something. In that case, the code below is
# simpler than your version and should achieve the same.
# x, y = zip(*np.random.uniform(0, 1, (1000, 2)))
# d = np.array(sorted(np.hypot(x, y)))
# If you only want to plot your pot function then creating the value range
# like this is just fine.
d = np.linspace(0.001, 1, 1000)
epsilons = sigmas = np.linspace(0.01, 1, num=10)
fig, ax = plt.subplots()
ax.set_xlim([0, 2])
ax.set_ylim([-1.5, 1.5])
line = None
for epsilon in epsilons:
for sigma in sigmas:
if line is None:
line = ax.plot(
d, pot(epsilon, sigma, d),
label=f'epsilon = {epsilon}, sigma = {sigma}'
)[0]
fig.legend()
else:
line.set_data(d, pot(epsilon, sigma, d))
# plt.savefig(f"epsilon_{epsilon}_sigma_{sigma}.png")
fig.show()