0

this is the code for asking user to input their datetime:

startTime = datetime.strptime(raw_input('please enter start time in format like 21/7/2014 0:00 :'), '%d/%m/%Y %H:%M')
endTime   = datetime.strptime(raw_input('please enter end time in format like 22/7/2014 23:57 :') , '%d/%m/%Y %H:%M')

how do i display the user input as my graph title along side with other info

this is my graph title code:

plt.title('Max Value: {:.2f} at {}, Mode: {:.2f}, Average: {:.2f})'.format(max(y), t[np.argmax(y)], mode(y)[0][0], np.average(y)))

i tried this but it didn't work:

 plt.title('Irradiance vs Time Graph at startTime to endTime \n Max Value: {:.2f} at {}, Mode: {:.2f}, Average: {:.2f})'startTime, endTime, .format(max(y), t[np.argmax(y)], mode(y)[0][0], np.average(y)))

Thanks in advance

  • This comes down to putting variables into strings. Take a look at this:http://stackoverflow.com/questions/3367288/insert-variable-values-into-a-string-in-python. If you convert your variables to strings in advance, then you can just use `+` to connect a bunch of strings together. – Dan Jul 29 '14 at 05:02
  • String formatting using `.format()` is much more idiomatic and more pythonic than using string concatenation. Not to mention easier to use and less error prone. – kylieCatt Jul 29 '14 at 05:38

1 Answers1

0

The proper way to do string formatting by using the .format() method is like this:

'My name is {}'.format('Ian')

which will output 'My name is Ian'. To use it in your case:

long_title = 'Irradiance vs Time Graph at {} to {} \n Max Value: {:.2f} at {}, Mode: {:.2f}, Average: {:.2f})'

plt.title(long_title.format(
                            startTime,
                            endTime,
                            max(y),
                            t[np.argmax(y)],
                            mode(y)[0][0],
                            np.average(y)
))
kylieCatt
  • 10,672
  • 5
  • 43
  • 51