Changing ylim while looping

Question:

I have from dataframe

time= 11.0, 11.3, 11.4, 21.0, 22.0 22.1, 98.0, 98.1, 98.2 

measurement= 13, 13.5, 13, 15, 16, 15, 14, 12, 14

epoch= [[11.0, 11.3, 11.4], [21.0, 22.0, 22.1], [98.0, 98.1, 98.2]] 
   

and I have:

epoch list has same data than time but it has been put to sets [] with values that are near each others.

I try to plot time as x and detections as y as many times as there are sets in epoch list. And for each plot I would like to change xlim so it would match min. and max. of each set in epoch list.

plt.xlim(min(epoch[0]), max(epoch[0])) and the next plot plt.xlim(min(epoch[1]), max(epoch[1]))

etc until the loop ends and three plots are drawn and whole timeline has been split to three plots.

import matplotlib.pyplot as plt

for i in epoch:
    #plt.xlim(min(epoch[0]), max(epoch[0])) 
    plt.plot(time, measurement, '.')
    plt.xlabel('time vs detection')
    plt.ylabel('magnitude')
    plt.show()

I can make this to draw the plot as many times as there are sets in epoch.

But I would like to limit min and max limits for each plot so they match min and max of each min and max of correct epoch.

Desired end result would be three plots with first covering time period from 11 to 11.4, second having time period from 21 to 22.1 and third having time period from 98 to 98.2

I hope this makes sense!

Thank you for your time.

Asked By: Joonatan

||

Answers:

I think you need to use your variable i in the loop

import matplotlib.pyplot as plt

for i in epoch:
    plt.plot(time, measurement, '.')
    plt.xlabel('time vs detection')
    plt.ylabel('magnitude')
    plt.xlim(min(i), max(i)) 
    plt.show()

enter image description here
enter image description here

Answered By: Lucas M. Uriarte
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.