z_dates.html


001| ####################################################################################################
002| from os import path
003| from datetime import date
004| from random import random
005| import matplotlib.pyplot as plt
006| from matplotlib.dates import DayLocator, DateFormatter
007| ####################################################################################################
008| BasePath = r'C:\CJS\prj\Python\charts'
009| ImgsPath = path.join(BasePath,'img')
010| 
011| 
012| ##================================================================================================##
013| def do_chart_dates ():
014|     filename = path.join(ImgsPath,'z_dates.png')
015| 
016|     # Make some data...
017|     xn = [n+1 for n in range(31) if random() < 0.66]
018|     xd = [date(2017,7,x) for x in xn]
019|     xv = [(25.0+(random()*50.0)) for x in xn]
020| 
021|     # Create a chart...
022|     fig, ax = plt.subplots()
023|     fig.suptitle('Test Chart', fontsize=13)
024|     ax.set_xlabel('Date', fontsize=11)
025|     ax.set_ylabel('Value', fontsize=11)
026| 
027|     ax.xaxis_date()
028|     ax.set_xlim(date(2017,6,1), date(2017,7,31))
029|     ax.set_ylim(20.0, 80.0)
030|     ax.xaxis.set_major_locator(DayLocator(interval=7))
031|     ax.xaxis.set_major_formatter(DateFormatter('%b %d'))
032|     for tick in ax.xaxis.get_major_ticks():
033|         tick.label1.set_rotation('vertical')
034|         tick.label1.set_size(9)
035|     ax.grid()
036| 
037|     ax.plot(xd,xv, marker='o',ms=4)
038| 
039|     fig.set_figwidth(6.4)
040|     fig.set_figheight(4.8)
041|     fig.subplots_adjust(left=0.12, bottom=0.16, right=0.97, top=0.92)
042|     fig.savefig(filename)
043| 
044| 
045| ####################################################################################################
046| if __name__ == '__main__':
047|     do_chart_dates()
048| ####################################################################################################
049| '''eof'''
050|