【问题标题】:How to draw enveloping line with a shaded area which incorporates a large number of data points?如何绘制包含大量数据点的阴影区域的包络线?
【发布时间】:2015-04-08 13:44:34
【问题描述】:

对于上图,我想用阴影区域画出类似于下图的包围线:

专家,有什么绝妙的想法吗?

【问题讨论】:

  • 好贴~哈哈~看完了
  • 这是一个持续存在的问题还是已通过链接回答?
  • 这个链接显然提供了一个很好的挖掘线索,但是,如果你能提供一个替代解决方案,那就太好了^^
  • 这听起来像“提供替代解决方案”应该是一个可以复制过去的代码?

标签: python matplotlib area envelope


【解决方案1】:

复制您的示例很容易,因为可以计算每个 x 的最小值和最大值并在它们之间进行填充。例如。

import matplotlib.pyplot as plt
import numpy as np

#dummy data
y = [range(20) + 3 * i for i in np.random.randn(3, 20)]
x = list(range(20))

#calculate the min and max series for each x
min_ser = [min(i) for i in np.transpose(y)]
max_ser = [max(i) for i in np.transpose(y)]

#initial plot
fig, axs = plt.subplots()
axs.plot(x, x)
for s in y:
    axs.scatter(x, s)

#plot the min and max series over the top
axs.fill_between(x, min_ser, max_ser, alpha=0.2)

给予

对于您显示的数据,这可能会出现问题,因为该系列并非在所有情况下都共享 x 值。如果是这种情况,那么您需要一些统计技术来以某种方式平滑系列。一种选择是使用像seaborn 这样的包,provides functions 会为您处理所有细节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    • 2022-01-17
    相关资源
    最近更新 更多