我正在用Python中的Matplotlib绘制双缝衍射实验图。我想要的是移动图形,使中心最大值为0。(目前电压的最大值出现在x=2.7 mm处)。我该怎么做呢?
双缝衍射实验图
plt.style.use('ggplot')
x = df['Position']
y = df['Voltage']
plt.figure(figsize=(8,6))
plt.plot(x,y, marker='.', markersize=14, linewidth=3.5, color='#5f5f5f')
plt.ylim(0,4)
plt.xlabel('Position[mm]', fontsize=15)
plt.ylabel('Voltage[V]', fontsize=15)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
plt.title('Position v.s. Voltage Plot for Two Slit Diffraction', fontsize=18)
plt.savefig('2slit')
plt.show()
发布于 2020-03-25 12:53:23
您可以使用numpy.argmax
动态找到最大值的索引,然后相应地从x
数组中减去:
import numpy as np
#...
x = df['Position'].values
y = df['Voltage'].values
shift = x[np.argmax(y)]
plt.plot(x - shift,y, marker='.', markersize=14, linewidth=3.5, color='#5f5f5f')
请注意,如果有两个相等的最大值,这将不起作用。
https://stackoverflow.com/questions/60842934
复制相似问题