Plotting of 1-dimensional Gaussian distribution function in python

Plotting of 1-dimensional Gaussian distribution function in python

To plot a 1-dimensional Gaussian distribution function in Python, you can use libraries like NumPy and Matplotlib. Here's how you can create and plot a Gaussian distribution:

import numpy as np
import matplotlib.pyplot as plt

# Parameters of the Gaussian distribution
mean = 0  # Mean (center)
std_deviation = 1  # Standard Deviation (spread)

# Generate x values (e.g., a range of values from -5 to 5)
x = np.linspace(-5, 5, 100)

# Calculate the Gaussian distribution values for each x
gaussian_values = (1 / (std_deviation * np.sqrt(2 * np.pi))) * np.exp(-(x - mean)**2 / (2 * std_deviation**2))

# Create a plot
plt.figure(figsize=(8, 6))
plt.plot(x, gaussian_values, label='Gaussian Distribution')

# Add labels and title
plt.xlabel('X')
plt.ylabel('Probability Density')
plt.title('1-Dimensional Gaussian Distribution')

# Show a legend
plt.legend()

# Show the plot
plt.grid(True)
plt.show()

In this code:

  1. We specify the parameters of the Gaussian distribution: the mean (mean) and the standard deviation (std_deviation).

  2. We create an array of x values using np.linspace to cover a range of values from -5 to 5. You can adjust the range and the number of points as needed.

  3. We calculate the corresponding Gaussian distribution values using the formula for the probability density function (PDF) of a Gaussian distribution.

  4. We create a plot using Matplotlib, setting the x-values as x and the Gaussian values as gaussian_values.

  5. We add labels to the axes, a title, and a legend.

  6. Finally, we display the plot using plt.show().

Running this code will generate a plot of the 1-dimensional Gaussian distribution with the specified mean and standard deviation. You can adjust the mean and std_deviation to visualize different Gaussian distributions.

Examples

  1. How to plot a standard 1-dimensional Gaussian distribution function in Python?

    • Description: This query seeks to visualize the standard Gaussian distribution, also known as the normal distribution, which has a mean of 0 and a standard deviation of 1.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      
      x = np.linspace(-3, 3, 100)
      y = np.exp(-0.5 * x**2) / np.sqrt(2 * np.pi)
      
      plt.plot(x, y)
      plt.title('Standard 1D Gaussian Distribution')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.grid(True)
      plt.show()
      
  2. How to plot a 1-dimensional Gaussian distribution function with custom mean and standard deviation in Python?

    • Description: This query explores plotting a Gaussian distribution with user-defined mean and standard deviation values for specific applications.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      
      mean = 2
      std_dev = 0.5
      
      x = np.linspace(-1, 5, 100)
      y = np.exp(-0.5 * ((x - mean) / std_dev)**2) / (std_dev * np.sqrt(2 * np.pi))
      
      plt.plot(x, y)
      plt.title('1D Gaussian Distribution (Mean=2, Std Dev=0.5)')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.grid(True)
      plt.show()
      
  3. How to visualize the cumulative distribution function (CDF) of a 1-dimensional Gaussian distribution in Python?

    • Description: This query focuses on plotting the cumulative distribution function (CDF) of a Gaussian distribution to understand the probability of a random variable being less than or equal to a given value.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      from scipy.stats import norm
      
      x = np.linspace(-3, 3, 100)
      cdf = norm.cdf(x)
      
      plt.plot(x, cdf)
      plt.title('Cumulative Distribution Function of 1D Gaussian')
      plt.xlabel('x')
      plt.ylabel('Cumulative Probability')
      plt.grid(True)
      plt.show()
      
  4. How to overlay multiple 1-dimensional Gaussian distribution functions in one plot using Python?

    • Description: This query aims to overlay multiple Gaussian distribution functions with different parameters onto a single plot for comparison or visualization purposes.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      
      x = np.linspace(-5, 5, 100)
      
      mean1, std_dev1 = 0, 1
      y1 = np.exp(-0.5 * ((x - mean1) / std_dev1)**2) / (std_dev1 * np.sqrt(2 * np.pi))
      
      mean2, std_dev2 = 2, 0.5
      y2 = np.exp(-0.5 * ((x - mean2) / std_dev2)**2) / (std_dev2 * np.sqrt(2 * np.pi))
      
      plt.plot(x, y1, label='Gaussian 1 (Mean=0, Std Dev=1)')
      plt.plot(x, y2, label='Gaussian 2 (Mean=2, Std Dev=0.5)')
      plt.title('Overlay of 1D Gaussian Distributions')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.legend()
      plt.grid(True)
      plt.show()
      
  5. How to plot the probability density function (PDF) of a truncated 1-dimensional Gaussian distribution in Python?

    • Description: This query explores visualizing the probability density function (PDF) of a truncated Gaussian distribution within a specific range using Python.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      from scipy.stats import truncnorm
      
      lower_bound, upper_bound = 0, 2
      mean, std_dev = 1, 0.5
      
      x = np.linspace(lower_bound, upper_bound, 100)
      y = truncnorm.pdf(x, (lower_bound - mean) / std_dev, (upper_bound - mean) / std_dev, loc=mean, scale=std_dev)
      
      plt.plot(x, y)
      plt.title('PDF of Truncated 1D Gaussian (0 to 2)')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.grid(True)
      plt.show()
      
  6. How to plot the probability density function (PDF) and cumulative distribution function (CDF) of a 1-dimensional Gaussian distribution together in Python?

    • Description: This query aims to plot both the probability density function (PDF) and cumulative distribution function (CDF) of a Gaussian distribution in a single plot for comparison.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      from scipy.stats import norm
      
      x = np.linspace(-3, 3, 100)
      pdf = norm.pdf(x)
      cdf = norm.cdf(x)
      
      fig, ax1 = plt.subplots()
      
      color = 'tab:blue'
      ax1.set_xlabel('x')
      ax1.set_ylabel('PDF', color=color)
      ax1.plot(x, pdf, color=color)
      ax1.tick_params(axis='y', labelcolor=color)
      
      ax2 = ax1.twinx()
      color = 'tab:red'
      ax2.set_ylabel('CDF', color=color)
      ax2.plot(x, cdf, color=color)
      ax2.tick_params(axis='y', labelcolor=color)
      
      fig.tight_layout()
      plt.title('PDF and CDF of 1D Gaussian Distribution')
      plt.grid(True)
      plt.show()
      
  7. How to plot a 1-dimensional Gaussian distribution with shaded regions indicating probability intervals in Python?

    • Description: This query investigates plotting a Gaussian distribution with shaded regions representing specific probability intervals, providing visual clarity for understanding probabilities.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      from scipy.stats import norm
      
      x = np.linspace(-3, 3, 100)
      y = norm.pdf(x)
      
      plt.plot(x, y, 'k-', linewidth=2)
      
      # Shading regions for probability intervals
      plt.fill_between(x, y, where=(x <= -1), color='red', alpha=0.3, label='P(x <= -1)')
      plt.fill_between(x, y, where=((x > -1) & (x <= 1)), color='blue', alpha=0.3, label='-1 < x <= 1')
      plt.fill_between(x, y, where=(x > 1), color='green', alpha=0.3, label='P(x > 1)')
      
      plt.title('1D Gaussian Distribution with Probability Intervals')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.legend()
      plt.grid(True)
      plt.show()
      
  8. How to plot the probability density function (PDF) of a 1-dimensional Gaussian distribution with varying mean and standard deviation in Python?

    • Description: This query explores plotting the probability density function (PDF) of Gaussian distributions with different mean and standard deviation values for comparative analysis.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      from scipy.stats import norm
      
      x = np.linspace(-5, 5, 100)
      
      mean_values = [0, 1, -1]
      std_dev_values = [0.5, 1, 1.5]
      
      for mean, std_dev in zip(mean_values, std_dev_values):
          pdf = norm.pdf(x, loc=mean, scale=std_dev)
          plt.plot(x, pdf, label=f'Mean={mean}, Std Dev={std_dev}')
      
      plt.title('PDF of 1D Gaussian Distribution with Different Parameters')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.legend()
      plt.grid(True)
      plt.show()
      
  9. How to plot a 1-dimensional Gaussian distribution function with logarithmic scale in Python?

    • Description: This query aims to visualize a Gaussian distribution with a logarithmic scale, useful for visualizing distributions with a wide range of values or exponential tails.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      
      x = np.linspace(0.01, 10, 100)
      y = np.exp(-0.5 * np.log(x)**2) / (x * np.sqrt(2 * np.pi))
      
      plt.plot(x, y)
      plt.title('1D Gaussian Distribution with Logarithmic Scale')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.grid(True)
      plt.show()
      
  10. How to plot the probability density function (PDF) of a truncated 1-dimensional Gaussian distribution with specified bounds in Python?

    • Description: This query explores plotting the probability density function (PDF) of a truncated Gaussian distribution within defined bounds to visualize distributions with limited ranges.
    • Code:
      import numpy as np
      import matplotlib.pyplot as plt
      from scipy.stats import truncnorm
      
      lower_bound, upper_bound = -1, 2
      mean, std_dev = 0, 1
      
      x = np.linspace(lower_bound, upper_bound, 100)
      y = truncnorm.pdf(x, (lower_bound - mean) / std_dev, (upper_bound - mean) / std_dev, loc=mean, scale=std_dev)
      
      plt.plot(x, y)
      plt.title('PDF of Truncated 1D Gaussian (-1 to 2)')
      plt.xlabel('x')
      plt.ylabel('Probability Density')
      plt.grid(True)
      plt.show()
      

More Tags

rotation apache-storm symfony2-easyadmin todataurl sap-fiori fragment-identifier pojo rpgle react-native-flexbox kaggle

More Python Questions

More Internet Calculators

More Cat Calculators

More Geometry Calculators

More Investment Calculators