【Python】Matplotlib在概率密度曲线图的基础上绘制积分区域
生活随笔
收集整理的這篇文章主要介紹了
【Python】Matplotlib在概率密度曲线图的基础上绘制积分区域
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前文
文章鏈接——《Matplotlib在直方圖中添加概率密度曲線和數(shù)學(xué)表達(dá)式》
Matplotlib補(bǔ)充代碼
from matplotlib.patches import Polygonint_x = np.linspace(mu-2*sigma, mu+2*sigma, 1000) int_y = ((1/(np.power(2*np.pi, 0.5)*sigma))*np.exp(-0.5*np.power((int_x-mu)/sigma, 2))) area = [(mu-2*sigma, 0), *zip(int_x, int_y), (mu+2*sigma, 0)]poly = Polygon(area, facecolor="gray", edgecolor="k", alpha=0.6, closed=False) ax.add_patch(poly)ax.text(0.45, 0.2, r"$\int_{\mu-2\sigma}^{\mu+2\sigma} y\mathrmvt6mr5xx$", fontsize=20, transform=ax.transAxes)ax.set_xlabel("體重") ax.set_ylabel("概率密度") ax.set_title(r"體重的直方圖:$\mu=60.0$, $\sigma=2.0$", fontsize=16)成品圖
Matplotlib完整代碼
# -*- coding:utf-8 -*-import numpy as np import matplotlib.pyplot as plt import matplotlib as mplmpl.rcParams["font.sans-serif"] = ["KaiTi"] mpl.rcParams["axes.unicode_minus"] = Falsemu = 60.0 sigma = 2.0 x = mu + sigma*np.random.randn(500)bins = 50fig, ax = plt.subplots(1, 1)n, bins, patches = ax.hist(x, bins, density=True, histtype="bar", facecolor="#99FF33", edgecolor="#00FF99", alpha=0.75)y = ((1/(np.power(2*np.pi, 0.5)*sigma))*np.exp(-0.5*np.power((bins-mu)/sigma, 2)))ax.plot(bins, y, color="#7744FF", ls="--", lw=2)ax.grid(ls=":", lw=1, color="gray", alpha=0.2)ax.text(54, 0.2, r"$y=\frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(x-\mu)^2}{2\sigma^2}}$",{"color": "#FF5511", "fontsize": 20})from matplotlib.patches import Polygonint_x = np.linspace(mu-2*sigma, mu+2*sigma, 1000) int_y = ((1/(np.power(2*np.pi, 0.5)*sigma))*np.exp(-0.5*np.power((int_x-mu)/sigma, 2))) area = [(mu-2*sigma, 0), *zip(int_x, int_y), (mu+2*sigma, 0)]poly = Polygon(area, facecolor="gray", edgecolor="k", alpha=0.6, closed=False) ax.add_patch(poly)ax.text(0.45, 0.2, r"$\int_{\mu-2\sigma}^{\mu+2\sigma} y\mathrmvt6mr5xx$", fontsize=20, transform=ax.transAxes)ax.set_xlabel("體重") ax.set_ylabel("概率密度") ax.set_title(r"體重的直方圖:$\mu=60.0$, $\sigma=2.0$", fontsize=16)plt.show()總結(jié)
以上是生活随笔為你收集整理的【Python】Matplotlib在概率密度曲线图的基础上绘制积分区域的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【数据结构与算法】大根堆和优先队列的Ja
- 下一篇: Doxygen with Graphvi