1

I have the following code:

import matplotlib.pyplot as plt
horas = [1,2,3,4]
diccionario = {(1,1,2,1):[2,3,4,5],
               (1,2,2,2):[2,5,1,5],
               (1,3,2,3):[2,5,5,5],
               (1,4,2,4):[2,6,8,5],
               (1,5,2,5):[2,7,5,5],
               (1,6,2,6):[2,8,2,5],
               (1,7,2,7):[2,9,6,5],
               (1,8,2,8):[2,4,9,5]}
plt.figure()     
i = 1 
maximo = 0
keys = diccionario.keys()
for n in range(0,len(keys)-1,2):
    gn, = plt.plot(horas,diccionario[keys[n]],'ro-')
    gn1, = plt.plot(horas,diccionario[keys[n+1]],'g*-')
    plt.subplot(len(keys)//2, 1,i)
    plt.legend([gn,gn1], [keys[n],keys[n+1]])
    i+=1
plt.show()

I expect to have 4 subplots with two lines each. I have them, but the last one is empty.

Could anyone explain why? I have tried many different ways without succeeding.

1
  • Finally I got it. Simply put plt.subplot(len(keys)//2, 1,i) before gn, = plt.plot(horas,diccionario[keys[n]],'ro-') Commented May 31, 2016 at 22:42

1 Answer 1

1

Put your subplot() before you plot gn and gn1. That will solve your problem.

for n in range(0, len(keys) - 1,2):
    plt.subplot(len(keys)//2, 1, i)
    gn, = plt.plot(horas, diccionario[keys[n]], 'ro-')
    gn1, = plt.plot(horas, diccionario[keys[n+1]], 'g*-')
    plt.legend([gn, gn1], [keys[n], keys[n+1]])
    i+=1

By the way, I recommend to use tuple instead of dict. You may notice that the sequence of results is quite different from what you want.

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

Comments

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.