Marc Marc - 7 months ago 31
Python Question

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

n = 50

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

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


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?


Firstly, you need 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