Python subplot. I'm asking you a question

Asked 2 years ago, Updated 2 years ago, 116 views

fig, ax = plt.subplots(5, 2)

ax[0, 0].plot(temp[:144], 'r')  
ax[0, 1].plot(temp[144:144*2], 'r')   
ax[1, 0].plot(temp[144*2:144*3], 'y')   
ax[1, 1].plot(temp[144*3:144*4], 'y')   
ax[2, 0].plot(temp[144*4:144*5], 'g')  
ax[2, 1].plot(temp[144*5:144*6], 'g')   
ax[3, 0].plot(temp[144*6:144*7], 'b')  
ax[3, 1].plot(temp[144*7:144*8], 'b')   
ax[4, 0].plot(temp[144*8:144*9], 'm')   
ax[4, 1].plot(temp[144*9:144*10], 'm')  

plt.show()

I wanted to make the graph come out like this, but the tempo was repeated, so I re-created the code.

fig, ax = plt.subplots(5, 2)

for i in range(5):  
    for j in range(2):  
        for k in range(10):  
            ax[i,j].plot(temp[144*k:144*(k+1)])
plt.show()

It's going to be like this.

What's wrong? I want you to tell me what's wrong Thank you ^

^

python subplot

2022-09-22 19:47

1 Answers

Looking at the pattern of the code that works, it would be more efficient to just go around for from 0 to 9. As a result, the three for sentences you uploaded will tour 5*2*10=100.

fig, ax = plt.subplots(5, 2)
s = 'rygbm'

for x in range(10) :
    # 0, 0, 1, 1, 2, 2, 3, ... Can be written like this to obtain a sequence of forms.
    # If you use int (round(x/3)), 0, 0, 0, 1, 1, 1, 2, ... A sequence of forms.
    a = int(round(x/2))

    # 0, 1, 0, 1, 0, ... You can do this to obtain a sequence of types.
    # If you use x%3, 0, 1, 2, 0, 1, 2, 0, ... A sequence of forms.
    b = x % 2

    c = x + 1
    d = s[a]
    ax[a, b].plot(temp[144*x:144*c], d)

plt.show()

I didn't test it. I hope it's useful.


2022-09-22 19:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.