wagner-cipriano

making date plots with python matplotlib

Nov 18th, 2017
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. #making date plots with python matplotlib
  2. #is usefull when do yu have a date in the xaxis and a valur (int, float...) in the yaxis
  3. #Author: Wagner Cipriano
  4. #Ref1: https://stackoverflow.com/questions/33962255/valueerror-invalid-literal-for-float-17-08-2015
  5. #Ref2: http://matplotlib.org/examples/pylab_examples/date_demo1.html
  6. #Python version tested successfully: 2.7.12
  7.  
  8. import numpy as np
  9. import pandas as pd
  10. import matplotlib.pyplot as plt
  11. from matplotlib.dates import DateFormatter
  12. from random import randrange, random
  13. from datetime import datetime
  14.  
  15. #generate date list
  16. start_date = np.datetime64('2010-01-01').astype(datetime)
  17.  
  18. numdays = 10
  19. dates = pd.date_range(start_date, periods=numdays)
  20.  
  21. #Generate data example
  22. data1 = [(random()+idx)**1.2 for idx in range(len(dates))]
  23. data2 = [(random()+idx)**1.5 for idx in range(len(dates))]
  24.  
  25. #plot
  26. fig, ax = plt.subplots()
  27. ax.plot_date(dates, data1, '-')
  28. ax.plot_date(dates, data2, '-')
  29.  
  30. #set the label for x and y and title
  31. plt.title('Matplot lib dates wc example')
  32. plt.xlabel('Dates')
  33. plt.ylabel('Random values example')
  34.  
  35. #date format
  36. ax.fmt_xdata = DateFormatter('%Y%m%d')
  37. #ax.fmt_ydata = data1
  38. ax.grid(True)
  39. fig.autofmt_xdate()
  40.  
  41. plt.show()
Add Comment
Please, Sign In to add comment