亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

慕沐林林 2021-12-17 14:50:54
假設我有 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-')...現在,我想提出這樣一個情節:我如何自動制作這些誤差線并僅通過一堆線條制作陰影,所有線條都具有相同的整體形狀但略有不同。
查看完整描述

2 回答

?
慕標琳琳

TA貢獻1830條經驗 獲得超9個贊

我不太清楚代碼示例中的錯誤變量如何與 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()

http://img1.sycdn.imooc.com//61bc337a00013bd605940246.jpg

我使用的平均值yfuncs作為基線變量。我提取yfuncs每個 x的最小值和最大值來計算誤差帶。我計算覆蓋與誤差帶相同范圍的誤差線。因此,誤差相對于平均值是不對稱的,這就是為什么它們在繪圖函數中作為二維數組輸入的原因。誤差帶用 繪制,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()

http://img1.sycdn.imooc.com//61bc33880001866305940246.jpg

查看完整回答
反對 回復 2021-12-17
?
www說

TA貢獻1775條經驗 獲得超8個贊

您只能使用 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()

如下所示: 

http://img1.sycdn.imooc.com//61bc339900014a6606010455.jpg

查看完整回答
反對 回復 2021-12-17
  • 2 回答
  • 0 關注
  • 514 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號