'Changing y axis limit in a loop

I am working on dataframe. I have data for a week ,since its huge I am dividing the dataframe for each day. I am plotting a parameter temperature with time. In most of all the days temperature will be within 20 to 30 ,some days it will exceed above 30. I need to write a code in such a way that, in a day, when the temperature is within 20 and 30 ,my plot Y axis limit should be (20,30), if it is out of those range I need to have a limit (0,50). My current code looks like this

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

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

Thanks for your help in advance. I know someone may think why so much requirement, the reason is, I am analysing through lots of data, I should have a standard scale, so I can just keep on looking instead of looking for Y axis and see the value



Solution 1:[1]

You could use:

plt.ylim(y.min() - 5, y.max() + 5)

This will scale between the min and max temperature values every plot (+- 5 to have some empty space above and below.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dzang