第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如何將數(shù)據(jù)框中的列繪制為子圖

如何將數(shù)據(jù)框中的列繪制為子圖

千萬里不及你 2023-10-25 10:53:22
我在這里做錯了什么?我想創(chuàng)建新的數(shù)據(jù)框,df并使用日期作為每個新創(chuàng)建的數(shù)據(jù)框(Emins、FTSE、Stoxx 和 Nikkei)折線圖中的 x 軸。我有一個df從 data.xlsx 創(chuàng)建的名為數(shù)據(jù)框,它看起來像這樣:    Dates         ES1     Z 1     VG1     NK10   2005-01-04  -0.0126  0.0077 -0.0030  0.00521   2005-01-05  -0.0065 -0.0057  0.0007 -0.00952   2005-01-06   0.0042  0.0017  0.0051  0.00443   2005-01-07  -0.0017  0.0061  0.0010 -0.00094   2005-01-11  -0.0065 -0.0040 -0.0147  0.00703670    2020-09-16  -0.0046 -0.0065 -0.0003 -0.00093671    2020-09-17  -0.0083 -0.0034 -0.0039 -0.00863672    2020-09-18  -0.0024 -0.0009 -0.0009  0.00523673    2020-09-23  -0.0206  0.0102  0.0022 -0.00133674    2020-09-24  0.0021  -0.0136 -0.0073 -0.0116我df創(chuàng)建了 4 個新數(shù)據(jù)框,分別稱為 Eminis、FTSE、Stoxx 和 Nikkei。感謝您的幫助?。。。?nbsp;   import numpy as np    import matplotlib.pyplot as plt    plt.style.use('classic')        df = pd.read_excel('data.xlsx')    df = df.rename(columns={'Dates':'Date','ES1': 'Eminis', 'Z 1': 'FTSE','VG1': 'Stoxx','NK1': 'Nikkei','TY1': 'Notes','G 1': 'Gilts', 'RX1': 'Bunds','JB1': 'JGBS','CL1': 'Oil','HG1': 'Copper','S 1': 'Soybeans','GC1': 'Gold','WILLTIPS': 'TIPS'})    headers = df.columns    Eminis = df[['Date','Eminis']]    FTSE = df[['Date','FTSE']]    Stoxx = df[['Date','Stoxx']]    Nikkei = df[['Date','Nikkei']]        # create multiple plots via plt.subplots(rows,columns)    fig, axes = plt.subplots(2,2, figsize=(20,15))    x = Date    y1 = Eminis    y2 = Notes    y3 = Stoxx    y4 = Nikkei        # one plot on each subplot    axes[0][0].line(x,y1)    axes[0][1].line(x,y2)    axes[1][0].line(x,y3)    axes[1][1].line(x,y4)        plt.legends()    plt.show()
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經(jīng)驗 獲得超8個贊

優(yōu)雅的解決方案是:

  • 將DataFrame 中的Dates列設置為索引。

  • 創(chuàng)建一個具有所需數(shù)量的子圖(在您的情況下為 4)的圖形,調(diào)用plt.subplots。

  • 從 DataFrame 中繪制一個圖,傳遞:

    • ax -子圖的ax結(jié)果(這里是Axes對象的數(shù)組 ,而不是單個Axes),

    • subplots=True - 在單獨的子圖中繪制每一列。

執(zhí)行此操作的代碼是:

fig, a = plt.subplots(2, 2, figsize=(12, 6), tight_layout=True)

df.plot(ax=a, subplots=True, rot=60);

為了測試上面的代碼,我創(chuàng)建了以下 DataFrame:


np.random.seed(1)

ind = pd.date_range('2005-01-01', '2006-12-31', freq='7D')

df = pd.DataFrame(np.random.rand(ind.size, 4),

    index=ind, columns=['ES1', 'Z 1', 'VG1', 'NK1'])

并得到以下圖片:

https://img1.sycdn.imooc.com/6538834d00016e1908550420.jpg

由于我的測試數(shù)據(jù)是隨機的,我假設“7天”頻率,以使圖片不會太“混亂”。對于真實數(shù)據(jù),請考慮使用“7D”頻率和mean()聚合函數(shù)進行重采樣。



查看完整回答
反對 回復 2023-10-25
?
RISEBY

TA貢獻1856條經(jīng)驗 獲得超5個贊

  • 我認為更簡潔的選擇是不要制作許多數(shù)據(jù)幀,這會造成不必要的工作和復雜性。

  • 繪制數(shù)據(jù)就是為繪圖 API 塑造數(shù)據(jù)框

  • 在這種情況下,更好的選擇是使用 .dataframe 將寬格式轉(zhuǎn)換為長(整齊)格式.melt。

    • 這會將所有標簽放在一列中,并將值放在另一列中

  • 使用,它可以從長格式的數(shù)據(jù)幀?seaborn.relplot創(chuàng)建。FacetGrid

    • seaborn是 的高級 API?matplotlib,使繪圖變得更加容易。

  • 如果數(shù)據(jù)框包含許多股票,但只繪制少數(shù)股票,則可以使用布爾索引來選擇它們

fig, a = plt.subplots(2, 2, figsize=(12, 6), tight_layout=True)

df.plot(ax=a, subplots=True, rot=60);

為了測試上面的代碼,我創(chuàng)建了以下 DataFrame:


np.random.seed(1)

ind = pd.date_range('2005-01-01', '2006-12-31', freq='7D')

df = pd.DataFrame(np.random.rand(ind.size, 4),

? ? index=ind, columns=['ES1', 'Z 1', 'VG1', 'NK1'])

并得到以下圖片:

https://img1.sycdn.imooc.com/6538836a00010e1f06530659.jpg

我在這里做錯了什么?

  • 當前的實現(xiàn)效率低下,有許多不正確的方法調(diào)用和未定義的變量。

    • 如果需要,必須為每個子圖顯示圖例。

    • Date沒有定義為x = Date

    • y2 = Notes:Notes未定義

    • .line不是plt方法并導致AttributeError;?它應該是plt.plot

    • y1 - y4是 DataFrame,但傳遞給 y 軸的繪圖方法,這會導致TypeError: unhashable type: 'numpy.ndarray';?一列應作為 傳遞y。

    • .legends不是一種方法;它是.legend

fig, a = plt.subplots(2, 2, figsize=(12, 6), tight_layout=True)

df.plot(ax=a, subplots=True, rot=60);

為了測試上面的代碼,我創(chuàng)建了以下 DataFrame:


np.random.seed(1)

ind = pd.date_range('2005-01-01', '2006-12-31', freq='7D')

df = pd.DataFrame(np.random.rand(ind.size, 4),

? ? index=ind, columns=['ES1', 'Z 1', 'VG1', 'NK1'])

并得到以下圖片:


查看完整回答
反對 回復 2023-10-25
  • 2 回答
  • 0 關(guān)注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號