简体   繁体   English

有没有办法为 python 中的子图优化此代码?

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

I wrote a code to create 8 subplots for the.net of each year in my data aggregated by month.我编写了一个代码,在按月汇总的数据中为每年的 .net 创建 8 个子图。 I tried to optimize the code using two for loops but I dont know how to hundle the query part in the pd df.我尝试使用两个 for 循环优化代码,但我不知道如何将查询部分集中在 pd df 中。 Is there a way to rewrite it in a better way or optimize this long code?有没有办法以更好的方式重写它或优化这段长代码?

The VF_data is just a pandas dataframe with numerical Positive and negative values aggregated per month per year. VF_data 只是一个 pandas dataframe,具有每年每月汇总的数字正值和负值。 Other columns are month, year, date.其他列是月、年、日期。

Thank you all in advance!!谢谢大家!!

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()

You can loop through .groupby("YEAR")您可以遍历.groupby("YEAR")

Below some example:下面是一些例子:

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