Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #making date plots with python matplotlib
- #is usefull when do yu have a date in the xaxis and a valur (int, float...) in the yaxis
- #Author: Wagner Cipriano
- #Ref1: https://stackoverflow.com/questions/33962255/valueerror-invalid-literal-for-float-17-08-2015
- #Ref2: http://matplotlib.org/examples/pylab_examples/date_demo1.html
- #Python version tested successfully: 2.7.12
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- from matplotlib.dates import DateFormatter
- from random import randrange, random
- from datetime import datetime
- #generate date list
- start_date = np.datetime64('2010-01-01').astype(datetime)
- numdays = 10
- dates = pd.date_range(start_date, periods=numdays)
- #Generate data example
- data1 = [(random()+idx)**1.2 for idx in range(len(dates))]
- data2 = [(random()+idx)**1.5 for idx in range(len(dates))]
- #plot
- fig, ax = plt.subplots()
- ax.plot_date(dates, data1, '-')
- ax.plot_date(dates, data2, '-')
- #set the label for x and y and title
- plt.title('Matplot lib dates wc example')
- plt.xlabel('Dates')
- plt.ylabel('Random values example')
- #date format
- ax.fmt_xdata = DateFormatter('%Y%m%d')
- #ax.fmt_ydata = data1
- ax.grid(True)
- fig.autofmt_xdate()
- plt.show()
Add Comment
Please, Sign In to add comment