Plot uniform distribution in python

I am a real beginner concerning coding and would like to know how to plot a uniform distribution between two points using python. Any help would be greatly appreciated!

Preview

34.7k10 gold badges88 silver badges110 bronze badges

asked Mar 30, 2014 at 13:33

4

This will do the work.

import numpy as np
import matplotlib.pyplot as plt

data = np.random.uniform(0,1,1000) # You are generating 1000 points between 0 and 1.
count, bins, ignored = plt.hist(data, 20, facecolor='green') 

plt.xlabel('X~U[0,1]')
plt.ylabel('Count')
plt.title("Uniform Distribution Histogram (Bin size 20)")
plt.axis([0, 1, 0, 100]) # x_start, x_end, y_start, y_end
plt.grid(True)

plt.show(block = False)

Plot uniform distribution in python

answered Aug 18, 2017 at 5:41

Plot uniform distribution in python

aerinaerin

18.1k27 gold badges93 silver badges130 bronze badges

You can use numpy.random.uniform(low=initial,high=final,size=size) for this type of distribution.
Initial and final are your parameters that define the limits of your distribution. You can specify the size of the distribution you want to generate also as a parameter within the function. Hope that helps!

import numpy
import matplotlib.pyplot as plt
array = numpy.random.uniform(low=0,high=5,size=100)
plt.plot(array)
plt.show()

Plot uniform distribution in python

answered Nov 30, 2014 at 1:53

Plot uniform distribution in python

AryaArya

1594 silver badges17 bronze badges

1

Uniform Distribution describes an experiment where there is an random outcome that lies between certain bounds. The bounds of the outcome are defined by the parameters, a and b, which are the minimum and maximum values. All intervals of the same length on the distribution has equal probability.

The probability density function (pdf) of uniform distribution is defined as:

Where, a and b are lower and upper boundaries of output interval respectively.

An uniform distribution has mean (a+b)/2 and variance (b-a)2/12.

The cumulative distribution function (cdf) evaluated at x, is the probability that the random variable (X) will take a value less than or equal to x. The cdf of uniform distribution is defined as:

Plot uniform distribution in python

The NumPy random.uniform() function returns random samples from a uniform distribution.

Syntax

numpy.random.uniform(low=0.0, high=1.0, size=None)


Parameters

low Optional. Specify the lower boundary of the output interval. float or array_like of floats. Default is 0.0.
high Optional. Specify the upper boundary of the output interval. float or array_like of floats. Must be non-negative. Default is 1.0.
size Optional. Specify output shape. int or tuple of ints. If the given shape is (m, n, k), then m * n * k samples are drawn. If size is None (default), a single value is returned if low and high are both scalars. Otherwise, np.broadcast(low, high).size samples are drawn.


Return Value

Returns samples from the parameterized uniform distribution. ndarray or scalar.

Example: Values from uniform distribution

In the example below, random.uniform() function is used to create a matrix of given shape containing random values drawn from specified uniform distribution.

import numpy as np

size = (5,3)

sample = np.random.uniform(0, 1, size)
print(sample)

The possible output of the above code could be:

[[0.93041354 0.50856806 0.46275855]
 [0.31003963 0.10581335 0.64064579]
 [0.58998769 0.44103259 0.44828118]
 [0.22591024 0.10336945 0.74814414]
 [0.95073832 0.70598443 0.4166474 ]]

Plotting uniform distribution

Example: Density plot

Matplotlib is a plotting library for the Python which can be used to plot the probability density function (pdf) of uniform distribution using hist() function.

import matplotlib.pyplot as plt
import numpy as np

#fixing the seed for reproducibility
#of the result
np.random.seed(10)

size = 10000
#drawing 10000 sample from 
#uniform distribution
sample = np.random.uniform(0, 1, size)
bin = np.arange(-1,2,0.05)

plt.hist(sample, bins=bin, edgecolor='blue') 
plt.title("Uniform Distribution") 
plt.show()

The output of the above code will be:

Plot uniform distribution in python


Example: Comparing pdfs

Multiple probability density functions can be compared graphically using Seaborn kdeplot() function. In the example below, pdf of three uniform distributions (each with different low and high) are compared.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

#fixing the seed for reproducibility
#of the result
np.random.seed(10)

size = 10000
#plotting 10000 sample from 
#different uniform distribution
sns.kdeplot(np.random.uniform(0, 1, size))
sns.kdeplot(np.random.uniform(0, 2, size))
sns.kdeplot(np.random.uniform(0, 3, size))

plt.legend(["a = 0, b = 1", 
            "a = 0, b = 2", 
            "a = 0, b = 3"])
plt.show()

The output of the above code will be:

Plot uniform distribution in python


Example: Comparing cdfs

Multiple cumulative distribution functions can be compared graphically using Seaborn ecdfplot() function. In the example below, cdf of three uniform distributions (each with different low and high) are compared.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

#fixing the seed for reproducibility
#of the result
np.random.seed(10)

size = 1000
#plotting 1000 sample from 
#different uniform distribution
sns.ecdfplot(np.random.uniform(0, 1, size))
sns.ecdfplot(np.random.uniform(0, 2, size))
sns.ecdfplot(np.random.uniform(0, 3, size))

plt.legend(["a = 0, b = 1", 
            "a = 0, b = 2", 
            "a = 0, b = 3"])
plt.show()

The output of the above code will be:

Plot uniform distribution in python

How do you find the uniform distribution in Python?

uniform() is a Uniform continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution. size : [tuple of ints, optional] shape or random variates.

How do you implement uniform distribution in Python?

You can use numpy. random. uniform(low=initial,high=final,size=size) for this type of distribution.

What is a uniform distribution graph?

In statistics, uniform distribution refers to a type of probability distribution in which all outcomes are equally likely. A deck of cards has within it uniform distributions because the likelihood of drawing a heart, a club, a diamond, or a spade is equally likely.

What is Numpy uniform?

numpy.random.uniform(low=0.0, high=1.0, size=None) Draw samples from a uniform distribution. Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform.