簡體   English   中英

在線條圖上繪制錯誤陰影帶 - python

[英]Draw error shading bands on line plot - python

假設我有 25 行這樣的:

x = np.linspace(0, 30, 60)
y = np.sin(x/6*np.pi)
error = np.random.normal(0.1, 0.02, size=y.shape)
y1 = y+ np.random.normal(0, 0.1, size=y.shape)
y2= y+ np.random.normal(0, 0.1, size=y.shape)
plt.plot(x, y, 'k-')
plt.plot(x, y1, 'k-')
plt.plot(x, y2,'k-')
.
.
.

現在,我想做一個這樣的情節: 在此處輸入圖片說明 . 我如何自動制作這些誤差線並僅通過一堆線條制作陰影,所有線條都具有相同的整體形狀但略有不同。

我不太清楚代碼示例中的錯誤變量如何與 y 變量的變化相關。 所以在這里我舉了一個例子,說明如何根據 25 個 y 變量的隨機變化計算和繪制誤差帶,我使用這些相同的變化在帶頂部創建 y 誤差線。 相同的邏輯適用於 x 軸上的變化/錯誤。

讓我們首先創建一些隨機數據,看看 25 條相似線的線圖是什么樣的:

import numpy as np                 # v 1.19.2
import matplotlib.pyplot as plt    # v 3.3.2

rng = np.random.default_rng(seed=1)

x = np.linspace(0, 5*np.pi, 50)
y = np.sin(x)
# error = np.random.normal(0.1, 0.02, size=x.shape) # I leave this out
nb_yfuncs = 25
ynoise = rng.normal(1, 0.1, size=(nb_yfuncs, y.size))
yfuncs = nb_yfuncs*[y] + ynoise

fig, ax = plt.subplots(figsize=(10,4))
for yfunc in yfuncs:
    plt.plot(x, yfunc, 'k-')

plt.show()

random_sine_functions


我使用yfuncs的平均值作為基線變量。 我提取每個 x 的yfuncs的最小值和最大值來計算誤差帶。 我計算覆蓋與誤差帶相同范圍的誤差線。 因此,誤差相對於平均值是不對稱的,這就是為什么它們在繪圖函數中作為二維數組輸入的原因。 誤差帶用fill_between繪制,誤差線用errorbar 下面是代碼的樣子:

ymean = yfuncs.mean(axis=0)
ymin = yfuncs.min(axis=0)
ymax = yfuncs.max(axis=0)
yerror = np.stack((ymean-ymin, ymax-ymean))

fig, ax = plt.subplots(figsize=(10,4))
plt.fill_between(x, ymin, ymax, alpha=0.2, label='error band')
plt.errorbar(x, ymean, yerror, color='tab:blue', ecolor='tab:blue',
             capsize=3, linewidth=1, label='mean with error bars')
plt.legend()

plt.show()

sine_functions_error_bars_and_band

您只能使用 matplot lib 執行此操作,如下所示:

def plot_with_error_bands(x: np.ndarray, y: np.ndarray, yerr: np.ndarray,
                          xlabel: str, ylabel: str,
                          title: str,
                          curve_label: Optional[str] = None,
                          error_band_label: Optional[str] = None,
                          color: Optional[str] = None, ecolor: Optional[str] = None,
                          linewidth: float = 1.0,
                          style: Optional[str] = 'default',
                          capsize: float = 3.0,
                          alpha: float = 0.2,
                          show: bool = False
                          ):
    """
    note:
        - example values for color and ecolor:
            color='tab:blue', ecolor='tab:blue'
        - capsize is the length of the horizontal line for the error bar. Larger number makes it longer horizontally.
        - alpha value create than 0.2 make the error bands color for filling it too dark. Really consider not changing.
        - sample values for curves and error_band labels:
            curve_label: str = 'mean with error bars',
            error_band_label: str = 'error band',
    refs:
        - for making the seaborn and matplot lib look the same see: https://stackoverflow.com/questions/54522709/my-seaborn-and-matplotlib-plots-look-the-same
    """
    if style == 'default':
        # use the standard matplotlib
        plt.style.use("default")
    elif style == 'seaborn' or style == 'sns':
        # looks idential to seaborn
        import seaborn as sns
        sns.set()
    elif style == 'seaborn-darkgrid':
        # uses the default colours of matplot but with blue background of seaborn
        plt.style.use("seaborn-darkgrid")
    elif style == 'ggplot':
        # other alternative to something that looks like seaborn
        plt.style.use('ggplot')

    # ax = plt.gca()
    # fig = plt.gcf(
    # fig, axs = plt.subplots(nrows=1, ncols=1, sharex=True, tight_layout=True)
    plt.errorbar(x=x, y=y, yerr=yerr, color=color, ecolor=ecolor,
                 capsize=capsize, linewidth=linewidth, label=curve_label)
    plt.fill_between(x=x, y1=y - yerr, y2=y + yerr, alpha=alpha, label=error_band_label)
    plt.grid(True)
    if curve_label or error_band_label:
        plt.legend()
    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)

    if show:
        plt.show()

例如

def plot_with_error_bands_test():
    import numpy as np  # v 1.19.2
    import matplotlib.pyplot as plt  # v 3.3.2

    # the number of x values to consider in a given range e.g. [0,1] will sample 10 raw features x sampled at in [0,1] interval
    num_x: int = 30
    # the repetitions for each x feature value e.g. multiple measurements for sample x=0.0 up to x=1.0 at the end
    rep_per_x: int = 5
    total_size_data_set: int = num_x * rep_per_x
    print(f'{total_size_data_set=}')
    # - create fake data set
    # only consider 10 features from 0 to 1
    x = np.linspace(start=0.0, stop=2*np.pi, num=num_x)

    # to introduce fake variation add uniform noise to each feature and pretend each one is a new observation for that feature
    noise_uniform: np.ndarray = np.random.rand(rep_per_x, num_x)
    # same as above but have the noise be the same for each x (thats what the 1 means)
    noise_normal: np.ndarray = np.random.randn(rep_per_x, 1)
    # signal function
    sin_signal: np.ndarray = np.sin(x)
    cos_signal: np.ndarray = np.cos(x)
    # [rep_per_x, num_x]
    y1: np.ndarray = sin_signal + noise_uniform + noise_normal
    y2: np.ndarray = cos_signal + noise_uniform + noise_normal

    y1mean = y1.mean(axis=0)
    y1err = y1.std(axis=0)
    y2mean = y2.mean(axis=0)
    y2err = y2.std(axis=0)

    plot_with_error_bands(x=x, y=y1mean, yerr=y1err, xlabel='x', ylabel='y', title='Custom Seaborn')
    plot_with_error_bands(x=x, y=y2mean, yerr=y2err, xlabel='x', ylabel='y', title='Custom Seaborn')
    plt.show()

如下所示: 在此處輸入圖片說明

如果您想使用 seaborn,請查看以下問題:如何使用 Seaborn 誤差帶顯示純矩陣 [樣本,X_Range] 的誤差帶?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM