Để vẽ một phân phối bình thường trong Python, bạn có thể sử dụng cú pháp sau: Mảng X xác định phạm vi cho trục x và plt.plot [] tạo ra đường cong cho phân phối bình thường với giá trị trung bình và độ lệch chuẩn được chỉ định.x array defines the range for the x-axis and the plt.plot[] produces the curve for the normal distribution with the specified mean and standard deviation.x array defines the range for the x-axis and the plt.plot[] produces the curve for the normal distribution with the specified mean and standard deviation. Các ví dụ sau đây cho thấy cách sử dụng các chức năng này trong thực tế. Mã sau đây cho thấy cách vẽ một đường cong phân phối bình thường duy nhất với giá trị trung bình là 0 và độ lệch chuẩn là 1:#x-axis ranges from -3 and 3 with .001 steps
x = np.arange[-3, 3, 0.001]
#plot normal distribution with mean 0 and standard deviation 1
plt.plot[x, norm.pdf[x, 0, 1]]
Ví dụ 1: Vẽ một bản phân phối bình thường duy nhất
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
#x-axis ranges from -3 and 3 with .001 steps
x = np.arange[-3, 3, 0.001]
#plot normal distribution with mean 0 and standard deviation 1
plt.plot[x, norm.pdf[x, 0, 1]]
Bạn cũng có thể sửa đổi màu sắc và chiều rộng của dòng trong biểu đồ:
plt.plot[x, norm.pdf[x, 0, 1], color='red', linewidth=3]
Ví dụ 2: Vẽ nhiều bản phân phối bình thường
Mã sau đây cho thấy cách vẽ nhiều đường cong phân phối bình thường với các phương tiện và độ lệch chuẩn khác nhau:
import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm #x-axis ranges from -5 and 5 with .001 steps x = np.arange[-5, 5, 0.001] #define multiple normal distributions plt.plot[x, norm.pdf[x, 0, 1], label='μ: 0, σ: 1'] plt.plot[x, norm.pdf[x, 0, 1.5], label='μ:0, σ: 1.5'] plt.plot[x, norm.pdf[x, 0, 2], label='μ:0, σ: 2'] #add legend to plot plt.legend[]
Hãy thoải mái sửa đổi màu sắc của các dòng và thêm nhãn tiêu đề và trục để làm cho biểu đồ hoàn thành:
import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm #x-axis ranges from -5 and 5 with .001 steps x = np.arange[-5, 5, 0.001] #define multiple normal distributions plt.plot[x, norm.pdf[x, 0, 1], label='μ: 0, σ: 1', color='gold'] plt.plot[x, norm.pdf[x, 0, 1.5], label='μ:0, σ: 1.5', color='red'] plt.plot[x, norm.pdf[x, 0, 2], label='μ:0, σ: 2', color='pink'] #add legend to plot plt.legend[title='Parameters'] #add axes labels and a title plt.ylabel['Density'] plt.xlabel['x'] plt.title['Normal Distributions', fontsize=14]
Tham khảo tài liệu matplotlib để biết giải thích chuyên sâu về hàm plt.plot [].plt.plot[] function.plt.plot[] function.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọcnumpy.random.rayleigh[] method, we can get the random samples from Rayleigh distribution and return the random samples.
Bàn luận
Đọcnumpy.random.rayleigh[] method, we can get the random samples from Rayleigh distribution and return the random samples.numpy.random.rayleigh[scale=1.0, size=None]
Với sự trợ giúp của phương thức numpy.random.rayleigh [], chúng ta có thể lấy các mẫu ngẫu nhiên từ phân phối Rayleigh và trả về các mẫu ngẫu nhiên.numpy.random.rayleigh[scale=1.0, size=None]Return the random samples as numpy array.
Chức năng phân phối RayleighReturn the random samples as numpy array.
Cú pháp: numpy.random.rayleigh [tỷ lệ = 1.0, size = none]numpy.random.rayleigh[] method, we are able to get the rayleigh distribution and return the random samples.
Python3
Trả về: Trả về các mẫu ngẫu nhiên dưới dạng mảng numpy.numpy.random.rayleigh[] method, we are able to get the rayleigh distribution and return the random samples.
Ví dụ 1 :
Trong ví dụ này, chúng ta có thể thấy rằng bằng cách sử dụng phương thức numpy.random.rayleigh [], chúng ta có thể nhận được phân phối Rayleigh và trả về các mẫu ngẫu nhiên.
plt.figure[]
nhập khẩu NUMPY dưới dạng NP
plt.show[]
Nhập matplotlib.pyplot như PLT
gfg = np.random.rayleigh [3.4, 50000]
Python3
Trả về: Trả về các mẫu ngẫu nhiên dưới dạng mảng numpy.numpy.random.rayleigh[] method, we are able to get the rayleigh distribution and return the random samples.
Ví dụ 1 :
Trong ví dụ này, chúng ta có thể thấy rằng bằng cách sử dụng phương thức numpy.random.rayleigh [], chúng ta có thể nhận được phân phối Rayleigh và trả về các mẫu ngẫu nhiên.
plt.figure[]
nhập khẩu NUMPY dưới dạng NP
plt.show[]
Nhập matplotlib.pyplot như PLT