Marc
Marc

Reputation: 167

Can someone explain why my Python plot won't show anything?

Here is my code:

import math
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import numpy as np

#variables
n = 50

#integral
denominator = integrate.quad(lambda S: math.exp(-10*S)*(10*S**n),0,300)

#function
S = np.linspace(0,300,0.01)
y_i = (np.exp(-10*S)*(10*S**n))/denominator[0]

plt.plot(S,y_i)

I realise it's a very simplistic question. I'm trying to integrate the denominator across 0 and 300, and then plot the function y_i with the definite integral as the denominator of y_i. However when I plot it, the graph shown appears empty, when I am expecting a probability distribution, peaking at 5.

Can anyone help?

Upvotes: 0

Views: 190

Answers (1)

TuanDT
TuanDT

Reputation: 1677

Firstly, you need plt.show() at the end of your code snippet. Secondly and more importantly, you misused np.linspace. Run it in interactive mode gives:

>>> import numpy as np
>>> S = np.linspace(0,300,0.01)
>>> S
array([], dtype=float64)

According to the documentation , the third argument, if not specified by name, is taken as the number of samples to generate. In this case, none (or a more disturbing interpretation is that you only want 0.01 sample). If by that last 0.01 in that line, you mean the step size, maybe you are looking for something like S = np.linspace(0,300,int(300.0/0.01)) or as @kameranis pointed out: S = np.arange(0, 300, 0.01).

Resulting figure:

enter image description here

Upvotes: 1

Related Questions