Reputation: 63
I want to plot the deflection of electron and all it shows no graph when i compile the program. This is my code so far and the comments represent the SI units.
import matplotlib.pyplot as plt
v=25300000 # m/s
E=1000 # V/m
d=10 # m
m=9*pow(10,-31) #kg
q=1.6*pow(10,-19) # C
for i in range(0,d):
y=(q*E*i*i)/(2*m*v*v)
plt.plot(i,y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
Upvotes: 2
Views: 308
Reputation: 10328
plt.plot
requires more than one point (unless you specify a marker), so
plt.plot(i,y)
will not produce a graph when i
and y
are individual values, only if they are arrays or array-like objects. You can either replace this with plt.scatter
,
import matplotlib.pyplot as plt
v=25300000 # m/s
E=1000 # V/m
d=10 # m
m=9*pow(10,-31) #kg
q=1.6*pow(10,-19) # C
for i in range(0,d):
y=(q*E*i*i)/(2*m*v*v)
plt.scatter(i,y)
plt.xlabel("x")
plt.ylabel("y")
plt.show()
Which will give you
or to make a list of the y
and x
values,
y = []
x = []
for i in range(0, d):
y.append((q*E*i*i)/(2*m*v*v))
x.append(i)
plt.plot(x,y)
Or, even better, make x
and y
numpy arrays:
import numpy as np
# ...
x = np.arange(0,10)
y = (q*E*x**2)/(2*m*v*v)
plt.plot(x,y)
Either of which will give you
Upvotes: 3
Reputation: 170
I think you are looking for a scatter plot. plt.plot
plots a curve instead. To make a scatter plot, you group the x and y coordinates into lists and call plt.scatter
:
import matplotlib.pyplot as plt
v=25300000 # m/s
E=1000 # V/m
d=10 # m
m=9*pow(10,-31) #kg
q=1.6*pow(10,-19) # C
x_list = []
y_list = []
for i in range(0,d):
y=(q*E*i*i)/(2*m*v*v)
x_list.append(i)
y_list.append(y)
plt.xlabel("x")
plt.ylabel("y")
plt.scatter(x_list, y_list)
plt.show()
Upvotes: 1