1
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]] 

I try to loop and plot these in formation where is 2 plots side by side and amount of rows comes from another function.

I have tried

import matplotlib.pyplot as plt
import math

epochs = len(epoch) #how many sets of data from similar time period.
rows = math.ceil(epochs/2) #I will have two plots on each row so I divide amount epochs by 2 to get correct amount of rows 



fig1, axes1 = plt.subplots(rows, 2)

for i in range(rows):
    for j in range(2):       
        axes1[i, j].plot(time, measurement)
        for k in epoch:
            plt.xlim(min(k)+0.01, max(k)+0.01)

I am trying to achieve a code that would plot supblots in (1,2,x) formation and use different x.lim for each plot.

Let me know if I need to clarify something. This is difficult to explain for me. So I am sorry and very grateful for everyones time.

Edit:

I can use this to loop xlim and amount of plots correctly But I need to do same with array of plots so there are multiple plots per row.

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

And I can use this to get the array of plots right:

fig1, axes1 = plt.subplots(rows, 2)

for i in range(rows):
    for j in range(2):       
        axes1[i, j].plot(time, measurement)

But I am unable to put those codes together.

Thank you so much for your time.

3
  • 1
    this code sound familiar Commented Sep 5, 2022 at 21:12
  • I hope my answer is what you are looking for. Let me know if not Commented Sep 5, 2022 at 21:30
  • I will let you know! It seems to be working but takes little bit time from me to adjust little bit! Thank you so much already! Commented Sep 5, 2022 at 22:06

1 Answer 1

1

I think you can try the following, note you have only 3 epochs and 4 figures, so the last axes is not formated

# constrained_layout  makes a nicer figure
fig1, axes1 = plt.subplots(rows, 2, constrained_layout=True)

epoch= [[11.0, 11.3, 11.4], [21.0, 22.0, 22.1], [98.0, 98.1, 98.2]] 
lst = iter(epoch)

for i in range(rows):
    for j in range(2):       
        axes1[i, j].plot(time, measurement)
        try:
            epoch = next(lst)
            axes1[i, j].set_xlim(min(epoch), max(epoch))
        except:
            print("no more epochs")
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you again so much. This gives answer to what I was asking. I still have a lot to do. Adding axis, adjusting the data and so on, but it is my turn to try. Lets see how far I can get before I get stuck :) i.gyazo.com/2ae70d0b5cdf7202703e96e193a00635.png
No problem, that is a nice figure :)
@lucas-m-uriarte, I am making decent progress with my plot. However I tried axes2 =axes1.twinx() to add another ax with different range. But it tells me 'numpy.ndarray' object has no attribute 'twinx'. Is there easy fix for this?
Never mind I figured it out. I used in loop axes1[i, j].twinx().plot...
great that is perfect! Hope everything is going good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.