简体   繁体   English

在循环中更改 plot 轴限制

[英]Changing plot axes limit in a loop

I am working on dataframe. I am plotting temperature vs time.我正在研究 dataframe。我正在绘制温度与时间的关系图。 Usually my temperature will be within 20 and 25,rarely it goes out of that range.通常我的体温会在 20 和 25 之间,很少超出这个范围。 I want to plots in such a way that when temperature is within 20 and 25, my Y axis limit should be within 20 and 25 or else it should be between 0 and 50.我想以这样的方式绘图,当温度在 20 到 25 之间时,我的 Y 轴限制应该在 20 到 25 之间,否则它应该在 0 到 50 之间。

My current code looks like that我当前的代码看起来像那样

listofDF = [df_i for (_, df_i) in df.groupby(pd.Grouper(key="filename", freq="1D"))] #for dividing daraframe on basis of one day

for df  in ((listofDF)):
    if len(df)!= 0:
        y = df[' Temp']                 
        x = df['time']
        plot(x,y)
        plt.ylim(20,30)

I want something like this我想要这样的东西

listofDF = [df_i for (_, df_i) in df.groupby(pd.Grouper(key="filename", freq="1D"))] #for dividing daraframe on basis of one day

for df  in ((listofDF)):
    if len(df)!= 0:
        y = df[' Temp']                 
        x = df['time']
        plot(x,y)
        if y.between(20,25):
           plt.ylim(20,25)
        else:
           plt.ylim(0,50) 

I tried this code and I got error as " The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().".我尝试了这段代码,但出现错误“系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()”。 Can anyone please help me谁能帮帮我吗

It throws that error because y.between returns an array with booleans including True if the value is between your boundaries.它会抛出该错误,因为y.between返回一个包含布尔值的数组,如果该值在您的边界之间,则包括 True 。 You are using the if statement to check the whole returned array, which is not directly possible.您正在使用 if 语句检查整个返回的数组,这是不可能的。 Instead, you can use all to check if ALL values in that array are true or not:相反,您可以使用all来检查该数组中的所有值是否为真:

if all(y.between(20,25)):
    plt.ylim(20, 25)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM