z_divided-line-1.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.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
007| from matplotlib.dates import DayLocator, MonthLocator, DateFormatter
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\blog\lcc'
010| ImgsPath = path.join(BasePath,'img')
011| 
012| chart_data = [
013|     (2022, [ 0,  8, 19,  4,  6,  7,  7,  6,  6,  8, 14,  3]),
014|     (2023, [ 5, 14, 16,  8,  4,  5, 19, 14, 26, 30, 19, 11]),
015|     (2024, [ 9, 28, 18,  9, 16, 23, 20, 18, 58, 37, 14, 18]),
016|     (2025, [15, 22, 19, 14, 11,  6,  7,  7, 25, 21, 18, 10]),
017| ]
018| start_date = date(2022,1,1)
019| end_date   = date(2026,1,1)
020| 
021| ##================================================================================================##
022| def do_chart_dates ():
023|     filename = path.join(ImgsPath,'divided-line-1.png')
024| 
025|     # Make some data...
026|     xs = []
027|     ys = []
028|     total = 0
029|     for row in chart_data:
030|         year = row[0]
031|         months = row[1]
032|         for mx,dat in enumerate(months):
033|             xs.append(date(year,mx+1,1))
034|             ys.append(dat)
035|             total += dat
036| 
037|     # Create a chart...
038|     fig, ax = plt.subplots()
039|     fig.suptitle('Plato\'s Divided Line (%d page Views)' % total, fontsize=15, fontweight='bold')
040| 
041|     ax.set_xlabel('Date', fontsize=11)
042|     ax.xaxis_date()
043|     ax.set_xlim(start_date, end_date)
044|     ax.xaxis.set_major_locator(MonthLocator(interval=1))
045|     #ax.xaxis.set_minor_locator(DayLocator(interval=7))
046|     ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
047|     #ax.xaxis.set_minor_formatter(NullFormatter())
048| 
049|     for tick in ax.xaxis.get_major_ticks():
050|         tick.label1.set_rotation('vertical')
051|         tick.label1.set_size(9)
052| 
053|     ax.set_ylabel('Page Views', fontsize=11)
054|     ax.set_ylim(0, 60)
055|     ax.yaxis.set_major_locator(MultipleLocator(10))
056|     ax.yaxis.set_minor_locator(MultipleLocator(5))
057|     ax.yaxis.set_major_formatter(FormatStrFormatter('%2d'))
058|     ax.yaxis.set_minor_formatter(NullFormatter())
059| 
060|     ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
061|     ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
062|     ax.tick_params(which='major', axis='both', direction='out', color='#000000')
063|     ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
064| 
065|     ax.bar(xs,ys, width=25, color='#dd6655', edgecolor='#000000', linewidth=1, zorder=11, alpha=0.9)
066| 
067|     fig.set_figwidth(8.0)
068|     fig.set_figheight(3.3)
069|     fig.subplots_adjust(left=0.07, right=0.99, bottom=0.25, top=0.92)
070|     fig.savefig(filename)
071|     print('wrote: %s' % filename)
072| 
073| 
074| ####################################################################################################
075| if __name__ == '__main__':
076|     print()
077|     do_chart_dates()
078|     print()
079| ####################################################################################################
080| '''eof'''
081|