簡體   English   中英

如何使用 pandas 和 seaborn 進行 plot 多個時間序列

[英]How to plot multiple times series using pandas and seaborn

我有 dataframe 的數據,人們幫助整理了這些數據。

它看起來像這樣(忽略索引,我只是抽樣):

    uni                             score   year
18  Arden University Limited        78.95   2020
245 The University of Manchester    71.35   2022
113 Darlington College              93.33   2020
94  City of Wolverhampton College   92      2017
345 The Royal Veterinary College    94      2018
118 Darlington College              62      2018

有更多數據 - https://github.com/elksie5000/uni_data/blob/main/uni_data_combined.csv - 但我的觀點是 set_index on year 然后按 uni 以及更大的組過濾,按均值/中值匯總。

最終目的是觀察一組大學並跟蹤一段時間內的指標。

我設法創建了一個簡單的 function 到 plot 一個簡單的 function 到 plot 數據,因此:

#Create a function to plot the data
def plot_uni(df, uni, query):
    print(query)
    df['query'] = df[uni].str.contains(query)
    subset = df[df['query']].set_index("year")
    subset.sort_index().plot()

在此處輸入圖像描述 我也可以使用 plot 的整體意思:

df.groupby("year").mean()['score'].plot()

我想要做的是 plot 兩者在一起。

在此處輸入圖像描述 理想情況下,我還希望能夠在一個 plot 中包含多行 plot 並指定顏色。 例如,全國分數是紅色的,一條特定的線是藍色的,而其他地塊是灰色的。

有任何想法嗎?

更新:

@Corralien 和@Johannes Schöck 的回答都有效。 就是不知道怎么改圖例。

您可以使用第一次調用 plot 返回的軸到plot ,並在您的 function 中重復使用它:

def plot_uni(df, uni, query, ax):  # <- HERE
    print(query)
    df['query'] = df[uni].str.contains(query)
    subset = df[df['query']].set_index("year")
    subset.sort_index().plot(ax=ax)  # <- HERE

# General plot
ax = df.groupby("year")['score'].mean().plot()

plot_uni(df, 'uni', 'College', ax)  # other plots
plot_uni(df, 'uni', 'University', ax)  # and so on

如果你使用 matplotlib.pyplot 方式來繪圖而不是 pandas 內置接口,你可以通過重復調用 plt.plot(data) 來簡單地添加更多行。 調用所有數據后,執行 plt.show() 以生成 output。

import matplotlib.pyplot as plt

def plot_uni(df, uni, query):
    print(query)
    df['query'] = df[uni].str.contains(query)
    subset = df[df['query']].set_index("year")
    plt.plot(subset.sort_index())

# Here goes some iterator that calls plot_uni
plt.show()

暫無
暫無

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

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