1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        用 matplotlib 繪制 3D 時(shí)間序列動(dòng)態(tài)圖

        共 6118字,需瀏覽 13分鐘

         ·

        2021-09-19 16:49

        時(shí)間序列動(dòng)態(tài)圖是顯示時(shí)間演變的非常強(qiáng)大的工具,但 matplotlib 的默認(rèn)動(dòng)態(tài)圖很簡(jiǎn)單,不太適合用于比較多個(gè)時(shí)間序列。

        動(dòng)態(tài)圖被廣泛使用:從解釋神經(jīng)網(wǎng)絡(luò)如何訓(xùn)練,到顯示合成時(shí)間序列統(tǒng)計(jì)數(shù)據(jù)或波動(dòng)率異常時(shí)應(yīng)該選擇何種基金等問題。

        假設(shè)我們想知道在新冠病毒流行期間哪個(gè)股票市場(chǎng)的發(fā)展最好,怎樣使人們都可以直觀的判斷出來呢?我建議創(chuàng)建動(dòng)態(tài)圖,因?yàn)樗?jiǎn)明、更清晰。我們從 2D 開始,到 3D,最后用 3D 網(wǎng)格來表示。

        由于這篇文章的目的是改進(jìn)時(shí)間序列動(dòng)畫,我們將使用 GDP(國(guó)內(nèi)生產(chǎn)總值)最高的 10 個(gè)歐洲國(guó)家的股票指數(shù)演變作為數(shù)據(jù)。

        2019年GDP最高的歐洲國(guó)家(不包括俄羅斯和土耳其)是:

        #CountryCountry codeStock Index
        1GermanyGERDAX
        2United KingdomUKUKX
        3FranceFRCAC
        4ItalyITFTSEMIB
        5SpainESIBEX
        6NetherlandsNLAEX
        7SwitzerlandCHSMI
        8Poland*PLWIG
        9SwedenSEOMX
        10BelgiumBEBEL20

        為了比較 2020 年歐洲股票指數(shù)的漲跌,所有動(dòng)態(tài)圖都顯示了指數(shù)從 01/01/2019 到 29/01/2021 1 年(261 天)的的滾動(dòng)股價(jià) 。

        2D動(dòng)態(tài)圖

        第一種方法是創(chuàng)建所有滾動(dòng)股價(jià)的2D線圖:

        def update_lines_2D(num, data, columns, dates, cmap, lines, ax):    '''    Function that updates the lines of a plot in 2D    '''
        # get the slice current_slice = data[num:261+num, :] current_dates = dates[num:261+num]
        # for each index... for i in range(current_slice.shape[1]):
        # get the coordinates x = np.array(np.arange(current_slice.shape[0])) y = np.array(current_slice[:, i])
        # crete points and segments to color points = np.array([x, y]).T.reshape(-1, 1, 2) segments = np.concatenate([points[:-1], points[1:]], axis=1)
        # Create a continuous norm to map from data points to colors norm = plt.Normalize(-0.22, 0.22) lines[i].set_segments(segments) lines[i].set_array(y) lines[i].set_color(cmap(y[-1] * 2.5 + 0.5))
        # update the ticks and labels ax.set_xticklabels([dates[int(val)+num].strftime('%m/%y') for val in ax.get_xticks()[:-1]] + ['']) ax.legend(loc='center right', bbox_to_anchor=(1.32, 0.5), fancybox=True, facecolor=(.95,.95,.95,1), framealpha=1, shadow=False, frameon=True, ncol=1, columnspacing=0, prop={'family': 'DejaVu Sans Mono'})
        # return the lines return lines

        def init_lines_2D(): ''' Function that initiates the lines of a plot in 2D ''' for line in lines: line.set_array([]) return lines

        動(dòng)畫看起來很酷,但是當(dāng)線條在時(shí)間上重疊時(shí)很難比較索引,因?yàn)樗鼈兙哂邢嗤念伾珗D。

        這可以通過為每個(gè)索引選擇不同的顏色來解決,但會(huì)降低繪圖的可讀性。

        3D動(dòng)態(tài)圖

        在第二種方法中,添加了第三維。它在給定的 z 坐標(biāo)中分隔每個(gè)索引,創(chuàng)建以下動(dòng)畫:

        def update_lines_3D(num, data, columns, dates, cmap, lines, ax):    '''    Function that updates the lines of a plot in 2D    '''
        # get the slice current_slice = data[num:261+num, :] current_dates = dates[num:261+num]
        # for each index... for i in range(current_slice.shape[1]):
        # get the coordinates x = np.arange(current_slice.shape[0]) y = np.tile(i, current_slice.shape[0]) z = np.array(current_slice[:, i])
        # crete points and segments to color points = np.array([x, y, z]).T.reshape(-1, 1, 3) segments = np.concatenate([points[:-1], points[1:]], axis=1)
        # Create a continuous norm to map from data points to colors norm = plt.Normalize(-0.19, 0.19) lines[i].set_segments(segments) lines[i].set_array(z) lines[i].set_color(cmap(z[-1] * 2.5 + 0.5))
        # update the ticks and labels ax.set_xticklabels([dates[int(val)+num].strftime('%m/%y') for val in ax.get_xticks()[:-1]] + [''], rotation=0, fontdict={'verticalalignment': 'top', 'horizontalalignment': 'center'}) ax.legend(loc='center right', bbox_to_anchor=(1.1, 0.46), fancybox=True, facecolor=(.95,.95,.95,1), framealpha=1, shadow=False, frameon=True, ncol=1, columnspacing=0, prop={'family': 'DejaVu Sans Mono'})
        # return the lines return lines
        def init_lines_3D(): for line in lines: line.set_array([]) return lines

        這個(gè)動(dòng)畫看起來比2D版本更酷,但它有一個(gè)很大的缺點(diǎn):深度感完全喪失,不同項(xiàng)目進(jìn)行比較非常困難。不過我們可以添加一個(gè)網(wǎng)格。

        3D網(wǎng)格動(dòng)態(tài)圖

        最后,添加一條連接每個(gè)時(shí)間步長(zhǎng)的所有值的線,創(chuàng)建網(wǎng)格:

        def update_mesh_lines_3D(num, data, columns, dates, cmap, lines, mesh_lines, ax):    '''    Function that updates the lines of a plot in 2D    '''
        # get the slice# current_slice = data[num:261+num, :] current_slice = data[num:int(261/2)+num, :]
        # for each index... for i in range(current_slice.shape[1]):
        # get the coordinates x = np.arange(current_slice.shape[0]) y = np.tile(i, current_slice.shape[0]) z = np.array(current_slice[:, i])
        # crete points and segments to color points = np.array([x, y, z]).T.reshape(-1, 1, 3) segments = np.concatenate([points[:-1], points[1:]], axis=1)
        # Create a continuous norm to map from data points to colors norm = plt.Normalize(-0.19, 0.19) lines[i].set_segments(segments) lines[i].set_array(z) lines[i].set_color(cmap(z[-1] * 2.5 + 0.5))
        # counter to check the current mesh line counter = 0
        # for each day... for j in range(current_slice.shape[0]):
        if j % 1 == 0:
        # get the coordinates x = np.tile(j, current_slice.shape[1]) y = np.arange(current_slice.shape[1]) z = np.array(current_slice[j, :])
        # crete points and segments to color points = np.array([x, y, z]).T.reshape(-1, 1, 3) segments = np.concatenate([points[:-1], points[1:]], axis=1)
        # Set the values used for colormapping norm = plt.Normalize(-0.22, 0.22) mesh_lines[counter].set_segments(segments) mesh_lines[counter].set_array(z) counter += 1
        # update the ticks and labels ax.set_xticklabels([dates[int(val)+num].strftime('%m/%y') for val in ax.get_xticks()[:-1]] + [''], rotation=0, fontdict={'verticalalignment': 'top', 'horizontalalignment': 'center'}) ax.legend(loc='center right', bbox_to_anchor=(1.1, 0.46), fancybox=True, facecolor=(.95,.95,.95,1), framealpha=1, shadow=False, frameon=True, ncol=1, columnspacing=0, prop={'family': 'DejaVu Sans Mono'})
        # return the lines return lines
        def init_mesh_lines_3D(): for line in lines: line.set_array([]) return lines

        看看這個(gè)動(dòng)畫,網(wǎng)格確實(shí)有助于以更清晰的方式比較指數(shù)隨時(shí)間的變化情況,從而得出更好的結(jié)論。

        結(jié)論

        從3D網(wǎng)格圖中,可以得出以下結(jié)論:

        UKX(英國(guó))和IBEX(ES)是下跌前和復(fù)蘇期間最弱的指數(shù)。DAX (GER)、OMX (SE)、SMI (CH) 和 AEX (NL) 是下跌前和復(fù)蘇期間最強(qiáng)的指數(shù)。CAC (FR)、FTSEMIB (IT) 和 BEL20 (BE) 在秋季之前是最強(qiáng)的,它們有很小的恢復(fù)??纯?2D 動(dòng)態(tài)圖,人們可能會(huì)得出相同的結(jié)論,但會(huì)變得更難。

        掃描本文最下方二維碼獲取全部完整源碼和Jupyter Notebook 文件打包下載。

        長(zhǎng)按掃碼獲取完整源碼

        瀏覽 149
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久午夜无码鲁丝午夜精品 | 亚洲一区激情 | 亚洲午夜精品成人毛片 | 三级黄色操逼视频 | 韩国毛片一区二区三区 | 亚洲性视频 | 污污的网站免费在线观看 | 露出两个乳让男人玩 | 日本无码免费A片无码视频美人J | 女仆狂揉下部羞羞动漫 |