I'm trying to use the following code to plot the function p_n
which is a function of N:
import matplotlib.pyplot as plt
def factorial(N):
fact = 1
for num in range(2, N 1):
fact *= num
return fact
a=1/(2**N)
b=factorial(N)
c=factorial(N-25)
d=factorial(25)
p_n=a*((b)/(c*d))
y = p_n
x = N
plt.title("Factorial Graph")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.plot(x, y, color="blue")
plt.show()
When I run this code, I just get a blank screen. I'm not sure why this is happening.
CodePudding user response:
As other users have commented, x and y are single values as yo uhave defined them.
I would recommend defining p_n
as a function, and using list comprehension or arrays for x and y. For example;
def p_n(N):
a=1/(2**N)
b=factorial(N)
c=factorial(N-25)
d=factorial(25)
return a*((b)/(c*d))
N = 10
x = list(range(N))
y = [p_n(x_val) for x_val in x]
Then pass x
and y
to plt.plot(...)
as you did before. You should get the following result: