By piccolo


2019-08-13 20:12:49 8 Comments

I am trying to plot a truncated Gaussian distribution (using scipy) with a mean of 0.5, and a standard distribution of 1.0. The distribution is truncated to be only in the interval (0,1).

x = np.linspace(0,1,100)
dist=truncnorm(a=0,b=1,loc=0.5, scale = 1.0)
plt.plot(x, dist.pdf(x), 'k-', lw=2, label='normalised truncated Gaussian')

However I get this instead:

enter image description here

Everything after x=0.5 seems normal but below that you get a sudden dip to zero. However the distribution should only be zero outside of (0,1). What is going on and how do I fix it?

1 comments

@Brennan 2019-08-13 20:53:44

You are telling it to plot that way with loc which shifts the plot.

dist=truncnorm(a=0,b=1,loc=0.5, scale = 1.0) should be dist=truncnorm(a=0,b=1, scale = 1.0) to get the standard plot.

From the source code on truncnorm():

For a uniform distribution MLE, the location is the minimum of the data, and the scale is the maximum minus the minimum. (Line 6570)

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Save plot to image file instead of displaying it using Matplotlib

  • 2012-03-08 17:38:10
  • Homunculus Reticulli
  • 1343493 View
  • 984 Score
  • 19 Answer
  • Tags:   python matplotlib plot

10 Answered Questions

16 Answered Questions

[SOLVED] How to put the legend out of the plot

1 Answered Questions

3 Answered Questions

[SOLVED] scipy, lognormal distribution - parameters

1 Answered Questions

1 Answered Questions

[SOLVED] Log Normal Random Variables with Scipy

  • 2015-02-24 16:10:05
  • FooBar
  • 1569 View
  • 7 Score
  • 1 Answer
  • Tags:   python scipy

Sponsored Content