0

I am plotting a double-slit diffraction experiment graph using Matplotlib in Python. What I want is to shift the graph so that the central maximum is at 0. (Right now the maximum of voltage occurs at x=2.7 mm). How can I do that?

Double slit diffraction experiment plot

image

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()
1
  • Please see the link for the image of my plot. Commented Mar 25, 2020 at 4:19

1 Answer 1

3

You can find the index of the maximum on the fly with numpy.argmax and then subtract from the x array accordingly:

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

Note that this will not work if there are two equal maxima.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.