簡體   English   中英

有沒有辦法為 python 中的子圖優化此代碼?

[英]Is there a way to optimize this code for subplots in python?

我編寫了一個代碼,在按月匯總的數據中為每年的 .net 創建 8 個子圖。 我嘗試使用兩個 for 循環優化代碼,但我不知道如何將查詢部分集中在 pd df 中。 有沒有辦法以更好的方式重寫它或優化這段長代碼?

VF_data 只是一個 pandas dataframe,具有每年每月匯總的數字正值和負值。 其他列是月、年、日期。

謝謝大家!!

def plot_MTY(df, aggregate_col='NET'):   



plt.subplot(2, 4, 1)

VF_data=df.query("(YEAR == '2015')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 2)

VF_data=df.query("(YEAR == '2016')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 3)

VF_data=df.query("(YEAR == '2017')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 4)

VF_data=df.query("(YEAR == '2018')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 5)

VF_data=df.query("(YEAR == '2019')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 6)

VF_data=df.query("(YEAR == '2020')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 7)

VF_data=df.query("(YEAR == '2021')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.subplot(2, 4, 8)

VF_data=df.query("(YEAR == '2022')")

aggregated_target = aggregate_data(VF_data, 'DATES', aggregate_col)

plt.plot(aggregated_target, label = 'df', linestyle="-")

plt.axhline(y=0, color='b', linestyle='-')

locs, labels = plt.xticks()

plt.setp(labels, rotation=90)



plt.gcf().set_size_inches(15, 8)

plt.show()

您可以遍歷.groupby("YEAR")

下面是一些例子:

df = pd.DataFrame({
    "YEAR": ["2022", "2022", "2023", "2023"],
    "x":[1, 2, 3, 4],
    "y": [1, 2, 3, 4]
})

for i, (year, gr) in enumerate(df.groupby("YEAR")):
    plt.subplot(1, 2, i+1)
    plt.plot(gr["x"], gr["y"])

暫無
暫無

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

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