Vẽ biểu đồ phân phối chuẩn trên biểu đồ python

Biểu đồ được sử dụng để biểu thị dữ liệu liên tục. Điều này trái ngược với các biểu đồ thanh biểu thị dữ liệu phân loại

  • Mặc dù biểu đồ biểu đồ và biểu đồ thanh trông giống nhau, nhưng chúng không giống nhau và bạn nên sử dụng các hàm khác nhau trong Python để vẽ chúng (lần lượt là _______41 và
    import matplotlib.pyplot as plt
    
    # Plot
    ax = plt.axes()
    ax.plot(x, normal_pdf)
    ax.set_title('Probability Density Function of a Normal Distribution')
    ax.set_ylabel('Probability Density')
    ax.set_xlabel('IQ Score')
    0)
  • Từ quan điểm toán học, nếu dữ liệu liên tục được hiển thị trên biểu đồ thanh thì các thanh sẽ mỏng vô hạn. Điều này là do có vô số số giữa hai điểm trên trục số, do đó, số lượng giá trị có thể có mà các điểm dữ liệu trong tập hợp dữ liệu liên tục của bạn có thể có cũng là vô hạn. Bạn sẽ cần có một biểu đồ thanh với số lượng thanh vô hạn để bao gồm tất cả các giá trị có thể có mà bạn có thể gặp phải
  • Biểu đồ giải quyết vấn đề này bằng cách sử dụng thùng. tất cả các số nằm trong một phạm vi nhất định được nhóm lại với nhau và hiển thị trong cùng một thanh

Ô thanh có danh mục trong khi biểu đồ có thùng

Về mặt khái niệm, biểu đồ giống như sự kết hợp của biểu đồ đường và biểu đồ thanh. dữ liệu có thể được hiển thị dưới dạng một đường liên tục đang được nhóm thành các thanh. Đây là một ví dụ bắt nguồn từ một ví dụ được hiển thị trên chính trang web Matplotlib minh họa liên kết này giữa biểu đồ đường, biểu đồ thanh và biểu đồ

Bài kiểm tra chỉ số thông minh (IQ) là một bài đánh giá trong đó, theo thiết kế, kết quả của mọi người tuân theo phân phối chuẩn với giá trị trung bình là 100 và độ lệch chuẩn là 15. Do đó, sự phân bố điểm số của mọi người sẽ có xu hướng thành một đường liên tục vì số lượng người được kiểm tra có xu hướng vô hạn (giả sử rằng các bài kiểm tra cho điểm chính xác vô hạn), và do đó, nó có thể được biểu diễn trên biểu đồ như vậy. Loại biểu đồ này được gọi là hàm mật độ xác suất (PDF) và hàm để tạo ra nó được cung cấp bởi gói "thống kê" của Scipy

from scipy.stats import norm
import numpy as np

# Create a normal distribution probability density function (PDF) with mean 100,
# standard deviation 15 and running from 45 to 155. It will consist of 1,000
# points to ensure it looks smooth when plotted.
x = np.linspace(45, 155, 1000)
mu, sigma = 100, 15
normal_pdf = norm.pdf(x, loc=mu, scale=sigma)

Điều này sau đó có thể được vẽ bằng gói Matplotlib. Hàm

import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
1 vẽ đồ thị dữ liệu x
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
2 dựa trên dữ liệu y
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
3 và do có 1.000 điểm được vẽ đồ thị nên đường kết quả có vẻ mượt mà (mặc dù về mặt kỹ thuật, nó được tạo thành từ 999 đoạn thẳng). Các hàm
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
4,
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
5 và
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
6 sau đó tạo tiêu đề ô và nhãn trục

import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')

Tuy nhiên, khi chúng tôi được cung cấp một mẫu hữu hạn điểm số của mọi người, chúng tôi chỉ có thể tính gần đúng phân phối này bằng cách đặt điểm số vào 'thùng', nhóm các điểm số có độ lớn tương tự lại với nhau và biểu thị điểm số trên biểu đồ dưới dạng các thanh thẳng đứng có chiều cao bằng số

Đối với ví dụ này, chúng tôi sẽ tạo 1.000 số bằng cách lấy ngẫu nhiên các giá trị từ phân phối chuẩn của giá trị trung bình 100 và độ lệch chuẩn 15. Điều này sẽ cung cấp cho chúng tôi 1.000 điểm giả định từ các bài kiểm tra IQ. Các chức năng từ gói Numpy sẽ được sử dụng để làm điều này

import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed(20210823)
# Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
mu, sigma = 100, 15
scores = np.random.normal(loc=mu, scale=sigma, size=1000)

# Print the first 10 scores just so we can see them
print(scores[:10])
from scipy.stats import norm
import numpy as np

# Create a normal distribution probability density function (PDF) with mean 100,
# standard deviation 15 and running from 45 to 155. It will consist of 1,000
# points to ensure it looks smooth when plotted.
x = np.linspace(45, 155, 1000)
mu, sigma = 100, 15
normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
0

Bây giờ chúng ta có thể sử dụng hàm

import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
7 từ Matplotlib để nhóm 1.000 điểm này vào các thùng và vẽ chúng. Theo mặc định, nó sẽ tạo 10 thùng có chiều rộng bằng nhau

from scipy.stats import norm
import numpy as np

# Create a normal distribution probability density function (PDF) with mean 100,
# standard deviation 15 and running from 45 to 155. It will consist of 1,000
# points to ensure it looks smooth when plotted.
x = np.linspace(45, 155, 1000)
mu, sigma = 100, 15
normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
2

Lưu ý định dạng Latex đang được sử dụng trong tiêu đề;

Tiếp theo, chúng ta có thể chỉ ra rằng biểu đồ thực sự xấp xỉ một phân phối bình thường bằng cách lấy hàm mật độ xác suất được tạo trước đó và vẽ nó lên trên cùng. Tuy nhiên, tệp PDF này sẽ có tỷ lệ hoàn toàn khác với biểu đồ. cái trước có tổng diện tích bên dưới là 1 trong khi cái sau đạt gần 250 ở điểm cao nhất. Để có thể hiển thị chúng cùng nhau, chúng ta cần chuẩn hóa biểu đồ bằng cách đặt đối số từ khóa

import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
9 thành
import numpy as np

# Set a seed for the random number generator so we get the same random numbers each time
np.random.seed(20210823)
# Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
mu, sigma = 100, 15
scores = np.random.normal(loc=mu, scale=sigma, size=1000)

# Print the first 10 scores just so we can see them
print(scores[:10])
0 khi gọi hàm
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
7. Điều này chuyển đổi biểu đồ thành mật độ xác suất. kích thước của các thanh được thay đổi sao cho tổng diện tích của chúng là 1. Chúng tôi cũng sẽ sử dụng 20 thùng cho biểu đồ lần này, chỉ để làm cho nó trông đẹp hơn một chút

from scipy.stats import norm
import numpy as np

# Create a normal distribution probability density function (PDF) with mean 100,
# standard deviation 15 and running from 45 to 155. It will consist of 1,000
# points to ensure it looks smooth when plotted.
x = np.linspace(45, 155, 1000)
mu, sigma = 100, 15
normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
7

Biểu đồ có thể được tùy chỉnh bằng cách sử dụng các đối số từ khóa của nó (xem tại đây để biết tài liệu về tất cả các tùy chọn)

  • import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    2 thay đổi loại biểu đồ. biểu đồ thanh, biểu đồ thanh xếp chồng lên nhau, biểu đồ bước hoặc biểu đồ bước đầy
  • Màu sắc và độ dày có thể được đặt bằng
    import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    3,
    import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    4 và
    import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    5 (hoặc tốc ký của chúng.
    import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    6,
    import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    7 và
    import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    8)
  • import numpy as np
    
    # Set a seed for the random number generator so we get the same random numbers each time
    np.random.seed(20210823)
    # Randomly sample a normal distribution (of mean 100 and standard deviation 15) 1,000 times
    mu, sigma = 100, 15
    scores = np.random.normal(loc=mu, scale=sigma, size=1000)
    
    # Print the first 10 scores just so we can see them
    print(scores[:10])
    9 đặt tên sẽ được sử dụng trong truyền thuyết
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    00 kiểm soát những yếu tố nào trong cốt truyện ở phía trước những yếu tố khác. Giá trị thấp hơn cho
    from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    00 có nghĩa là nó sẽ được vẽ đầu tiên (nghĩa là nó sẽ ở phía sau, bên dưới các phần tử khác)

Các chức năng khác được sử dụng bên dưới bao gồm

  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    02 để tạo một đường thẳng đứng trên các trục
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    03 để thêm lưới
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    04 để kiểm soát thủ công kích thước của các trục
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    05 để thêm một huyền thoại

Các chức năng trên có thể được tùy chỉnh bằng cách sử dụng các đối số từ khóa Matplotlib chung

  • ________ 106 hoặc ______ 107 đặt màu (ví dụ: ________ 108 cho màu xanh lam)
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    09 hoặc
    from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    20 đặt kiểu đường kẻ (ví dụ:
    from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    21 cho nét đứt)
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    22 đặt độ trong suốt
  • from scipy.stats import norm
    import numpy as np
    
    # Create a normal distribution probability density function (PDF) with mean 100,
    # standard deviation 15 and running from 45 to 155. It will consist of 1,000
    # points to ensure it looks smooth when plotted.
    x = np.linspace(45, 155, 1000)
    mu, sigma = 100, 15
    normal_pdf = norm.pdf(x, loc=mu, scale=sigma)
    23 đặt vị trí của huyền thoại
import matplotlib.pyplot as plt

# Plot
ax = plt.axes()
ax.plot(x, normal_pdf)
ax.set_title('Probability Density Function of a Normal Distribution')
ax.set_ylabel('Probability Density')
ax.set_xlabel('IQ Score')
0

Mặc dù các tiêu đề của các ô trên đã sử dụng chế độ toán học của Latex để nhận các chữ cái Hy Lạp mu và sigma, nhưng chức năng bổ sung của Latex (và kiểm soát nhiều hơn về kích thước và chất lượng của hình) có thể được mở khóa bằng cách sử dụng mã sau