填充seaborn / matplotlib中两个正态分布之间的重叠区域

2024-01-05

我想填充两个正态分布之间重叠的区域。我有x最小值和最大值,但我不知道如何设置y边界。

我看过plt文档 https://matplotlib.org/gallery/lines_bars_and_markers/fill_between_demo.html and 一些例子 https://matplotlib.org/users/recipes.html。我认为这个相关问题 https://stackoverflow.com/questions/47645291/how-to-fill-area-under-curve-in-seaborn-distribution-plot and this one https://stackoverflow.com/questions/16417496/matplotlib-fill-between-multiple-lines接近,但没有运气。这是我到目前为止所拥有的。

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

pepe_calories = np.array([361, 291, 263, 284, 311, 284, 282, 228, 328, 263, 354, 302, 293,
       254, 297, 281, 307, 281, 262, 302, 244, 259, 273, 299, 278, 257,
       296, 237, 276, 280, 291, 278, 251, 313, 314, 323, 333, 270, 317,
       321, 307, 256, 301, 264, 221, 251, 307, 283, 300, 292, 344, 239,
       288, 356, 224, 246, 196, 202, 314, 301, 336, 294, 237, 284, 311,
       257, 255, 287, 243, 267, 253, 257, 320, 295, 295, 271, 322, 343,
       313, 293, 298, 272, 267, 257, 334, 276, 337, 325, 261, 344, 298,
       253, 302, 318, 289, 302, 291, 343, 310, 241])



modern_calories = np.array([310, 315, 303, 360, 339, 416, 278, 326, 316, 314, 333, 317, 357,
       304, 363, 387, 279, 350, 367, 321, 366, 311, 308, 303, 299, 363,
       335, 357, 392, 321, 361, 285, 321, 290, 392, 341, 331, 338, 326,
       314, 327, 320, 293, 333, 297, 315, 365, 408, 352, 359, 312, 300,
       263, 358, 345, 360, 336, 378, 315, 354, 318, 300, 372, 305, 336,
       286, 296, 413, 383, 328, 418, 388, 416, 371, 313, 321, 321, 317,
       402, 290, 328, 344, 330, 319, 309, 327, 351, 324, 278, 369, 416,
       359, 381, 324, 306, 350, 385, 335, 395, 308])

ax = sns.distplot(pepe_calories, fit_kws={"color":"blue"}, kde=False,
        fit=stats.norm, hist=None, label="Pepe's");
ax = sns.distplot(modern_calories, fit_kws={"color":"orange"}, kde=False,
        fit=stats.norm, hist=None, label="Modern");

# Get the two lines from the axes to generate shading
l1 = ax.lines[0]
l2 = ax.lines[1]

# Get the xy data from the lines so that we can shade
x1 = l1.get_xydata()[:,0]
y1 = l1.get_xydata()[:,1]
x2 = l2.get_xydata()[:,0]
y2 = l2.get_xydata()[:,1]

x2min = np.min(x2)
x1max = np.max(x1)

ax.fill_between(x1,y1, where = ((x1 > x2min) & (x1 < x1max)), color="red", alpha=0.3)
#> <matplotlib.collections.PolyCollection at 0x1a200510b8>

plt.legend()
#> <matplotlib.legend.Legend at 0x1a1ff2e390>
plt.show()

有任何想法吗?

创建于 2018-12-01reprexpy 包 https://github.com/crew102/reprexpy

import reprexpy
print(reprexpy.SessionInfo())
#> Session info --------------------------------------------------------------------
#> Platform: Darwin-18.2.0-x86_64-i386-64bit (64-bit)
#> Python: 3.6
#> Date: 2018-12-01
#> Packages ------------------------------------------------------------------------
#> matplotlib==2.1.2
#> numpy==1.15.4
#> reprexpy==0.1.1
#> scipy==1.1.0
#> seaborn==0.9.0

在收集 pdf 数据时get_xydata很聪明,你现在受到 matplotlib 的渲染/分割算法的支配。拥有x1 and x2跨越不同的范围也可以进行比较y1 and y2难的。

您可以通过自己拟合法线来避免这些问题,而不是 出租sns.distplot做吧。然后你就可以更好地控制你的价值观 寻找。

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
norm = stats.norm

pepe_calories = np.array([361, 291, 263, 284, 311, 284, 282, 228, 328, 263, 354, 302, 293,
       254, 297, 281, 307, 281, 262, 302, 244, 259, 273, 299, 278, 257,
       296, 237, 276, 280, 291, 278, 251, 313, 314, 323, 333, 270, 317,
       321, 307, 256, 301, 264, 221, 251, 307, 283, 300, 292, 344, 239,
       288, 356, 224, 246, 196, 202, 314, 301, 336, 294, 237, 284, 311,
       257, 255, 287, 243, 267, 253, 257, 320, 295, 295, 271, 322, 343,
       313, 293, 298, 272, 267, 257, 334, 276, 337, 325, 261, 344, 298,
       253, 302, 318, 289, 302, 291, 343, 310, 241])



modern_calories = np.array([310, 315, 303, 360, 339, 416, 278, 326, 316, 314, 333, 317, 357,
       304, 363, 387, 279, 350, 367, 321, 366, 311, 308, 303, 299, 363,
       335, 357, 392, 321, 361, 285, 321, 290, 392, 341, 331, 338, 326,
       314, 327, 320, 293, 333, 297, 315, 365, 408, 352, 359, 312, 300,
       263, 358, 345, 360, 336, 378, 315, 354, 318, 300, 372, 305, 336,
       286, 296, 413, 383, 328, 418, 388, 416, 371, 313, 321, 321, 317,
       402, 290, 328, 344, 330, 319, 309, 327, 351, 324, 278, 369, 416,
       359, 381, 324, 306, 350, 385, 335, 395, 308])


pepe_params = norm.fit(pepe_calories)
modern_params = norm.fit(modern_calories)

xmin = min(pepe_calories.min(), modern_calories.min())
xmax = max(pepe_calories.max(), modern_calories.max())
x = np.linspace(xmin, xmax, 100)

pepe_pdf = norm(*pepe_params).pdf(x)
modern_pdf = norm(*modern_params).pdf(x)
y = np.minimum(modern_pdf, pepe_pdf)

fig, ax = plt.subplots()
ax.plot(x, pepe_pdf, label="Pepe's", color='blue')
ax.plot(x, modern_pdf, label="Modern", color='orange')
ax.fill_between(x, y, color='red', alpha=0.3)
plt.legend()
plt.show()

如果,比方说,sns.distplot(或其他一些绘图函数)制作了一个您不想重现的绘图,那么您可以使用来自的数据get_xydata这边走:

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

pepe_calories = np.array([361, 291, 263, 284, 311, 284, 282, 228, 328, 263, 354, 302, 293,
       254, 297, 281, 307, 281, 262, 302, 244, 259, 273, 299, 278, 257,
       296, 237, 276, 280, 291, 278, 251, 313, 314, 323, 333, 270, 317,
       321, 307, 256, 301, 264, 221, 251, 307, 283, 300, 292, 344, 239,
       288, 356, 224, 246, 196, 202, 314, 301, 336, 294, 237, 284, 311,
       257, 255, 287, 243, 267, 253, 257, 320, 295, 295, 271, 322, 343,
       313, 293, 298, 272, 267, 257, 334, 276, 337, 325, 261, 344, 298,
       253, 302, 318, 289, 302, 291, 343, 310, 241])



modern_calories = np.array([310, 315, 303, 360, 339, 416, 278, 326, 316, 314, 333, 317, 357,
       304, 363, 387, 279, 350, 367, 321, 366, 311, 308, 303, 299, 363,
       335, 357, 392, 321, 361, 285, 321, 290, 392, 341, 331, 338, 326,
       314, 327, 320, 293, 333, 297, 315, 365, 408, 352, 359, 312, 300,
       263, 358, 345, 360, 336, 378, 315, 354, 318, 300, 372, 305, 336,
       286, 296, 413, 383, 328, 418, 388, 416, 371, 313, 321, 321, 317,
       402, 290, 328, 344, 330, 319, 309, 327, 351, 324, 278, 369, 416,
       359, 381, 324, 306, 350, 385, 335, 395, 308])

ax = sns.distplot(pepe_calories, fit_kws={"color":"blue"}, kde=False,
        fit=stats.norm, hist=None, label="Pepe's");
ax = sns.distplot(modern_calories, fit_kws={"color":"orange"}, kde=False,
        fit=stats.norm, hist=None, label="Modern");

# Get the two lines from the axes to generate shading
l1 = ax.lines[0]
l2 = ax.lines[1]

# Get the xy data from the lines so that we can shade
x1, y1 = l1.get_xydata().T
x2, y2 = l2.get_xydata().T

xmin = max(x1.min(), x2.min())
xmax = min(x1.max(), x2.max())
x = np.linspace(xmin, xmax, 100)
y1 = np.interp(x, x1, y1)
y2 = np.interp(x, x2, y2)
y = np.minimum(y1, y2)
ax.fill_between(x, y, color="red", alpha=0.3)

plt.legend()
plt.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

填充seaborn / matplotlib中两个正态分布之间的重叠区域 的相关文章

随机推荐

  • 使用 Google Analytics 进行 Javascript 重定向

    我需要帮助弄清楚如何在包含 Analytics 代码的同时成功重定向 我有一个子域设置http buuf fractalsystems org http buuf fractalsystems org 子域实际上只是一个子文件夹http f
  • 将“memberOf”属性添加到 ApacheDS

    我正在尝试在 Apache Directory 中模拟 Active Directory 的 memberOf 属性 我已将 memberOf 的以下条目添加到我的 LDIF 文件中 dn m oid 1 3 6 1 4 1 18060 0
  • 检查 url 是否包含 http:// 或 https:// [重复]

    这个问题在这里已经有答案了 可能的重复 检查 url 是否包含 http 或 https https stackoverflow com questions 7334491 check if the url is contains the
  • 如何在加载时打开 React Native Maps 标记的标注

    我希望在安装屏幕组件时打开所有标记的所有标注 目前 它仅在单击标记时打开 如何在功能组件中使用 useRef 来执行此操作 const markerRef useRef React createRef return
  • 使用 C++17 Constexpr 查找数组

    我正在尝试编写一个 constexpr find 函数 它将返回包含特定值的 std array 的索引 下面的函数似乎工作正常 除非包含的类型是const char include
  • 哪个班级设计比较好? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 哪个类设计更好 为什么 public class User public String UserName public String
  • 在 OS X 上以管理员权限运行脚本

    我已经尽力在 Stack Overflow 和互联网上找到许多脚本问题的解决方案 但我似乎找不到我需要的解决方案 我想要做的是创建一个更加自动化且点击次数更少的解决方案来删除系统上的所有移动缓存用户帐户 我一直在登录并手动转到用户帐户 然后
  • 如何设置 clojureScript 项目以使用规范并在运行时测试 clojure.core 函数?

    Clojure 1 9 推出specs https clojure org guides spec clojure core 库中的函数现在有规范 如何设置 clojurescript 项目以使用规范并在运行时测试 clojure core
  • 我可以采取什么措施来加快 S3 上传/更新速度?

    今天我一整天都在尝试向 s3 上传一些小东西 500 个目录中约有 20k 个文件 总计约 3GB 对于名为 简单存储服务 的服务来说 这是绝对合理的 我可以平均以大约 500k s 1mb s 1 8 到 3 6 GB h 之间 的速度上
  • Java 中最好的企业购物车是什么? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 未针对早午餐编译供应商 CSS 文件

    我对 b 有疑问 电子邮件受保护 cdn cgi l email protection不编译 Bower Component CSS 文件 如同在 Brunch 中分离应用程序和供应商 CSS https stackoverflow com
  • 在 C 中创建数组时出现分段错误

    我最近迁移到一台新笔记本电脑 HP dv6119tx 英特尔酷睿 i5 4 GB RAM 它安装了 Windows 7 Home Premium 64 位 我正在尝试创建一个类型的数组int长度为 10 6 的 C Dev C 我曾经在我的
  • 在 React Native 中使用 PanResponder 锁定移动

    使用本机反应泛响应器 https facebook github io react native docs panresponder html 当屏幕触摸坐标超出一定值范围时 如何阻止移动 例如 如何防止用户将组件移动到屏幕上某个 y 位置
  • 比较堆转储 (HPROF) 文件

    是否可以比较两个 HPROF 文件 如何 根据我的发现 您只能比较对象的直方图 为此 请转到 直方图 视图 然后单击 与另一个堆转储比较 并选择另一个 hprof 文件 Here is screenshot
  • 获取孩子的所有孩子等等

    我使用 MongoDb 作为数据库 我想要所有孩子的孩子等等 让我们假设 A 有 B 和 C 孩子 B 有 D 和 E 孩子 D 有 F 和 G 孩子 所以当我查询子节点时A 我将所有孩子作为输出 例如 B C D E F G C Cust
  • 检查一个数据帧的值是否按确切顺序存在于另一个数据帧中

    我有 1 个数据数据框和多个 参考 数据框 我正在尝试自动检查数据帧的值是否与参考数据帧的值匹配 重要的是 这些值的顺序也必须与参考数据帧中的值相同 这些列是重要的列 但我的真实数据集包含更多列 下面是一个玩具数据集 Dataframe g
  • 1个月后自动将列表数据从一个列表复制到另一个列表

    我列出了在提交信息路径表单后动态存储数据的列表 我想在任何数据创建日期 30 天后存档此数据 你能建议我该怎么做吗 看看我可以通过工作流程做到这一点 但我如何设置条件 在创建任何列表后 30 天完成后 它将自动复制到其他列表中 首先我想问为
  • 如何防止XSS攻击

    渗透测试团队告诉我 以下 URL 正在引发 XSS 攻击 这是我的 download msg jsp 代码
  • 存储过程参数默认值

    我正在尝试创建一个带有默认参数的存储过程 在我的查询中我会这样做 DECLARE mydate DATETIME DECLARE MT DATETIME DECLARE MY DATETIME SELECT mydate GETDATE S
  • 填充seaborn / matplotlib中两个正态分布之间的重叠区域

    我想填充两个正态分布之间重叠的区域 我有x最小值和最大值 但我不知道如何设置y边界 我看过plt文档 https matplotlib org gallery lines bars and markers fill between demo