0

I have some previous code which will print out

'The number for january is x' - etc, throughout one year.

I'm trying to plot the x vs the months using this:

import matplotlib.pyplot as plt
for m, n in result.items():
    print 'The number for', m, "is", n
    plt.plot([n])
    plt.ylabel('Number')
    plt.xlabel('Time (Months)')
    plt.title('Number per month')
    plt.show()

Where m is the month (also the file name it reads from and n is the x value (number).

However when I run this I just get a blank graph - I think I may be missing somethhing major out?

Result contains:

{'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13} x 6 times 

For practical purposes I made each file have a number of 13 as the real files are enormous

6
  • and can you also post what result contains? Commented Mar 5, 2015 at 10:56
  • plt.plot(m,n) gives: ValueError: could not convert string to float: practice apr Commented Mar 5, 2015 at 10:57
  • result prints {'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13} x 6 times - For practical purposes I made each file have a number of 13 as the real files are enormous Commented Mar 5, 2015 at 10:59
  • So you are looking to have the time(months) as the x-axis and number as your y-axis? But if you do so, you will just get a straight line wouldn't you, as all the values for number are 13 Commented Mar 5, 2015 at 11:05
  • Yeah I know it will be a straight line this is just a practice run as when I put the real files in it will take a few hours to run and I want it fully working before I do that - and yes, time on the x and number on the y Commented Mar 5, 2015 at 11:06

1 Answer 1

1
import matplotlib.pyplot as plt
import numpy as np 

result = {'apr': 13, 'jun': 13, 'jul': 13, 'aug': 13, 'sep': 13, 'oct': 13}
for m, n in result.items():
    print 'The number for', m, "is", n
plt.plot(result.values())
plt.ylabel('Number')
plt.xlabel('Time (Months)')
plt.title('Number per month')
plt.xticks(range(len(result)), result.keys())
plt.show()

So what I have done here is to just remove the plotting section outside the for loop. Now you will get your results printed as you have been doing previously, but the plotting will be done for all the values once.

You can take out the values from a dictionary using dict.values, in our case gives us all the many values which are 13.

enter image description here

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

6 Comments

So does this mean I have to manually enter each value in once it has calculated it?
I don't understand what you are manually entering here.. Don't you already have your dict that contains the values and keys?
The order of the keys in the dictionary is not preserved - you need to use either an ordered dict, or a 2D array.
Ah no I haven't made a dictionary containing the values. So far I have just written the code to count the number in each file (one file for each month) in a for loop. If I change the file to say 12 in one the code will still print out a straight line graph of 13's
Oh, you could have mentioned that before! The answer was for the question with the example
|

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.