From 076714de423f686c1d4d1e110d1ef8251231db98 Mon Sep 17 00:00:00 2001 From: Zhangyixue1537 Date: Tue, 19 Dec 2023 20:55:01 +0000 Subject: [PATCH] #8 add 1st rick to rick_try --- rick.py | 441 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 306 insertions(+), 135 deletions(-) diff --git a/rick.py b/rick.py index c3d1843..a338534 100644 --- a/rick.py +++ b/rick.py @@ -1,9 +1,10 @@ # -*- coding: utf-8 -*- +#!/usr/bin/env python """ Version 0.8.0 - """ + from psycopg2 import connect import psycopg2.sql as pg import pandas.io.sql as pandasql @@ -11,12 +12,12 @@ import matplotlib.pyplot as plt import matplotlib.patches as patches import matplotlib.ticker as ticker +import matplotlib.font_manager as font_manager import geopandas as gpd import os import shapely import seaborn as sns from shapely.geometry import Point -import matplotlib.font_manager as font_manager import numpy as np class font: @@ -38,6 +39,8 @@ class colour: purple = '#660159' grey = '#7f7e7e' light_grey = '#777777' + blue = '#253494' + orange = '#0D9F73' cmap = 'YlOrRd' class geo: @@ -45,7 +48,7 @@ class geo: Class for additional gis layers needed for the cloropleth map. """ - + def ttc(con): """Function to return the TTC subway layer. @@ -60,23 +63,16 @@ def ttc(con): Geopandas Dataframe of the Subway Layer """ - query = ''' - - SELECT * FROM gis.subway_to + query = '''SELECT * FROM gis.subway_to''' - ''' ttc = gpd.GeoDataFrame.from_postgis(query, con, geom_col='geom') - # ttc = ttc.to_crs({'init' :'epsg:3857'}) - ttc = ttc.to_crs(epsg=3857) + ttc = ttc.to_crs('epsg:3857') - # Below can be replaced by an apply lambda - # in case one row is of a different type (e.g. MULTIPOLYGON vs POLYGON) - #for index, row in ttc.iterrows(): - # rotated = shapely.affinity.rotate(row['geom'], angle=-17, origin = Point(0, 0)) - # ttc.loc[index, 'geom'] = rotated - ttc['geom']=ttc['geom'].apply(lambda x: shapely.affinity.rotate(x, angle=-17, origin = Point(0, 0))) + for index, row in ttc.iterrows(): + rotated = shapely.affinity.rotate(row['geom'], angle=-17, origin = Point(0, 0)) + ttc.loc[index, 'geom'] = rotated - return ttc + return ttc def island(con): @@ -98,23 +94,20 @@ def island(con): SELECT geom - FROM tts.zones_tts06 + FROM gis.zones_tts06 WHERE gta06 = 81 ''' island = gpd.GeoDataFrame.from_postgis(query, con, geom_col='geom') - # island = island.to_crs({'init' :'epsg:3857'}) - island = island.to_crs(epsg=3857) + island = island.to_crs('epsg:3857') - # Below can be replaced by an apply lambda - # in case one row is of a different type (e.g. MULTIPOLYGON vs POLYGON) - #for index, row in island.iterrows(): - # rotated = shapely.affinity.rotate(row['geom'], angle=-17, origin = Point(0, 0)) - # island.loc[index, 'geom'] = rotated - island['geom']=island['geom'].apply(lambda x: shapely.affinity.rotate(x, angle=-17, origin = Point(0, 0))) + for index, row in island.iterrows(): + rotated = shapely.affinity.rotate(row['geom'], angle=-17, origin = Point(0, 0)) + island.loc[index, 'geom'] = rotated return island + class charts: """ @@ -132,83 +125,77 @@ def func(): sns.set(font_scale=1.5) mpl.rc('font',family='Libre Franklin') - def chloro_map(con, df, lower, upper, title, **kwargs): + def chloro_map(con, df, subway, island, lower, upper, title, **kwargs): """Creates a chloropleth map - + Parameters ----------- con : SQL connection object - Connection object needed to connect to the RDS + Connection object needed to connect to the RDS df : GeoPandas Dataframe - Data for the chloropleth map. The data must only contain 2 columns; the first column has to be the geom column and the second has to be the data that needs to be mapped. + Data for the chloropleth map. The data must only contain 2 columns; the first column has to be the geom column and the second has to be the data that needs to be mapped. lower : int - Lower bound for colourmap + Lower bound for colourmap upper : int - Upper bound for the colourmap + Upper bound for the colourmap title : str - Legend label - subway : boolean, optional, default: False - True to display subway on the map - island : boolean, optional, defailt: True - False to grey out the Toronto island + Legend label cmap : str, optional, default: YlOrRd - Matplotlib colourmap to use for the map + Matplotlib colourmap to use for the map unit : str, optional - Unit to append to the end of the legend tick + Unit to append to the end of the legend tick nbins : int, optional, defualt: 2 - Number of ticks in the colourmap - + Number of ticks in the colourmap + Returns -------- fig Matplotlib fig object ax Matplotlib ax object - - """ - - func() - subway = kwargs.get('subway', False) - island = kwargs.get('island', True) + + """ + cmap = kwargs.get('cmap', colour.cmap) unit = kwargs.get('unit', None) nbins = kwargs.get('nbins', 2) - + df.columns = ['geom', 'values'] light = '#d9d9d9' - fig, ax = plt.subplots() + fig, ax = plt.subplots(dpi=450.0) fig.set_size_inches(6.69,3.345) - + ax.set_yticklabels([]) ax.set_xticklabels([]) ax.set_axis_off() - + mpd = df.plot(column='values', ax=ax, vmin=lower, vmax=upper, cmap = cmap, edgecolor = 'w', linewidth = 0.5) - - if island == False: - island_grey = geo.island(con) - island_grey.plot(ax=ax, edgecolor = 'w', linewidth = 4, color = light) - island_grey.plot(ax=ax, edgecolor = 'w', linewidth = 0, color = light) - + + if subway == True: ttc_df = geo.ttc(con) line = ttc_df.plot( ax=ax, linewidth =4, color = 'w', alpha =0.6) # ttc subway layer line = ttc_df.plot( ax=ax, linewidth =2, color = 'k', alpha =0.4) # ttc subway layer - - + + if island == False: + island_grey = geo.island(con) + island_grey.plot(ax=ax, edgecolor = 'w', linewidth = 4, color = light) + island_grey.plot(ax=ax, edgecolor = 'w', linewidth = 0, color = light) + + props = dict(boxstyle='round', facecolor='w', alpha=0) plt.text(0.775, 0.37, title, transform=ax.transAxes, wrap = True, fontsize=7, fontname = font.semibold, verticalalignment='bottom', bbox=props, fontweight = 'bold') # Adding the Legend Title - - + + cax = fig.add_axes([0.718, 0.16, 0.01, 0.22]) # Size of colorbar - - rect = patches.Rectangle((0.76, 0.01),0.235,0.43,linewidth=0.5, transform=ax.transAxes, edgecolor=light,facecolor='none') - ax.add_patch(rect) - + + #rect = patches.Rectangle((0.76, 0.01),0.235,0.43,linewidth=0.5, transform=ax.transAxes, edgecolor=light,facecolor='none') + #ax.add_patch(rect) + ax.margins(0.1) - + sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=lower, vmax=upper)) sm._A = [] cbr = fig.colorbar(sm, cax=cax) @@ -219,8 +206,8 @@ def chloro_map(con, df, lower, upper, title, **kwargs): cbr.ax.yaxis.set_tick_params(width=0.5) cbr.ax.tick_params(labelsize=6) # Formatting for Colorbar Text for l in cbr.ax.yaxis.get_ticklabels(): - l.set_family(font.normal) - + l.set_family(font.normal) + if unit is not None: if 0 < upper < 10: ax.text(0.829, 0.32, unit, transform=ax.transAxes, wrap = True, fontsize=6, fontname = font.normal, verticalalignment='bottom', ha = 'left') @@ -231,11 +218,59 @@ def chloro_map(con, df, lower, upper, title, **kwargs): elif 1000 <= upper < 100000: ax.text(0.862, 0.32, unit, transform=ax.transAxes, wrap = True, fontsize=6, fontname = font.normal, verticalalignment='bottom', ha = 'left') else: - pass + pass + + return fig, ax + + def histogram_chart(data, ylab, xlab, nbin, **kwargs): + """Creates a histogram chart with specified nbin (nbin data) + + Parameters + ----------- + data : array like or scalar + Data for the line chart. + ylab : str + Label for the y axis. + xlab : str + Label for the x axis. + ymax : int, optional, default is the max y value + The max value of the y axis + ymin : int, optional, default is 0 + The minimum value of the y axis + + Returns + -------- + fig + Matplotlib fig object + ax + Matplotlib ax object + props + Dictionary of the text annotation properties + """ + + func() + xmax = kwargs.get('xmax', None) + xmin = kwargs.get('xmin', 0) + + if (xmax is None): + xmax = int(max(data)) + + if (nbin is None): + nbin = 10 + + plt.style.use('seaborn-whitegrid') + fig, ax = plt.subplots(1,1, dpi=450.0) + fig.set_size_inches(6.1, 4.2) + ax.hist(data, bins=nbin, alpha=1.0, color=colour.purple) + ax.set_xlabel(xlab) + ax.set_ylabel(ylab) + ax.set_xlim(xmin, xmax) + ax.legend(loc='upper right') return fig, ax + def line_chart(data, ylab, xlab, **kwargs): """Creates a line chart. x axis must be modified manually @@ -247,14 +282,26 @@ def line_chart(data, ylab, xlab, **kwargs): Label for the y axis. xlab : str Label for the x axis. + xticker_labels : array, optional, with ticker identifications + Tickers for the x axis. ymax : int, optional, default is the max y value The max value of the y axis ymin : int, optional, default is 0 The minimum value of the y axis baseline : array like or scalar, optional, default is None Data for another line representing the baseline. + addedline1 : array like or scalar, optional, default is None + Data for another line representing the addedline1. + addedline2 : array like or scalar, optional, default is None + Data for another line representing the addedline2. yinc : int, optional The increment of ticks on the y axis. + list_legends: list, optional (when baseline is ON) + List of legend names to show on plot + + min_text : array of min-value text information, default is None + max_text : array of max-value text information, default is None + Returns -------- @@ -271,6 +318,13 @@ def line_chart(data, ylab, xlab, **kwargs): ymax = kwargs.get('ymax', int(data.max())) ymin = kwargs.get('ymin', 0) baseline = kwargs.get('baseline', None) + addedline1 = kwargs.get('addedline1', None) + addedline2 = kwargs.get('addedline2', None) + list_legends = kwargs.get('list_legends', None) + min_text = kwargs.get('min_text', None) + max_text = kwargs.get('max_text', None) + xticker_labels = kwargs.get('xticker_labels', None) + xticker_slots= kwargs.get('xticker_slots', None) delta = (ymax - ymin)/4 i = 0 @@ -281,14 +335,21 @@ def line_chart(data, ylab, xlab, **kwargs): break yinc = kwargs.get('yinc', int(round(delta+1)*pow(10,i))) - fig, ax =plt.subplots() - ax.plot(data ,linewidth=3, color = colour.purple) + fig, ax = plt.subplots(dpi=450.0) + fig.set_size_inches(6.1, 4.2) + line, = ax.plot(data, linewidth=3, color = colour.purple) if baseline is not None: - ax.plot(baseline ,linewidth=3, color = colour.grey) + line_baseline, = ax.plot(baseline ,linewidth=3, color = colour.grey) + if addedline1 is not None: + line_addedline1, = ax.plot(addedline1 ,linewidth=3, color = colour.blue) + if addedline2 is not None: + line_addedline2, = ax.plot(addedline2 ,linewidth=3, color = colour.orange) + + plt.grid() ax.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}')) - + ax.set_facecolor('xkcd:white') plt.xlabel(xlab, fontsize=9, fontweight = 'bold', horizontalalignment='right', x=0, labelpad=10, @@ -298,21 +359,56 @@ def line_chart(data, ylab, xlab, **kwargs): horizontalalignment='right', y=1.0, labelpad=10, fontname = font.normal) fig.set_size_inches(6.1, 4.1) - plt.xticks(fontsize=9, fontname = font.normal) + plt.xticks(xticker_slots, fontsize=9, fontname = font.normal) plt.yticks(range(ymin, ymax + yinc, yinc), fontsize =9, fontname = font.normal) + + if (xticker_labels is not None): + list_major_labels = xticker_labels + list_major_ticks = xticker_slots + ax.xaxis.set_major_locator(ticker.FixedLocator(list_major_ticks)) + ax.xaxis.set_major_formatter(ticker.FixedFormatter(list_major_labels)) + #ax.tick_params(axis='x', which='major', colors = colour.light_grey, labelsize=7, rotation=0) + + + # Set text - min & max props = dict(boxstyle='round, pad=0.4',edgecolor=colour.purple, linewidth = 2, facecolor = 'w', alpha=1) - + if (min_text is not None): + plt.text(min_text[0], min_text[1], int(data.min()), size=min_text[2], rotation=min_text[3], + ha="center", va="center", color='#660159', bbox=props) + if (max_text is not None): + plt.text(max_text[0], max_text[1], int(data.max()), size=max_text[2], rotation=max_text[3], + ha="center", va="center", color='#660159', bbox=props) + + + + + # Set lengends (if multiple lines) + if (baseline is not None): + ax.legend([line, line_baseline], list_legends, loc = 'best', + fontsize=9, framealpha=1.0, facecolor = 'w', + edgecolor='black') + if (baseline is not None and addedline1 is not None): + ax.legend([line, line_baseline, line_addedline1], list_legends, loc = 'best', + fontsize=9, framealpha=1.0, facecolor = 'w', + edgecolor='black') + if (baseline is not None and addedline1 is not None and addedline2 is not None): + ax.legend([line, line_baseline, line_addedline1, line_addedline2], list_legends, loc = 'best', + fontsize=9, framealpha=1.0, facecolor = 'w', + edgecolor='black') + + ax.set_ylim([ymin, ymax]) fig.patch.set_facecolor('w') return fig, ax, props + def tow_chart(data, ylab, **kwargs): """Creates a 7 day time of week line chart. Each data point represents 1 hour out of 168 hours. - + Parameters ----------- data : array like or scalar @@ -325,7 +421,7 @@ def tow_chart(data, ylab, **kwargs): The minimum value of the y axis yinc : int, optional The increment of ticks on the y axis. - + Returns -------- fig @@ -334,18 +430,25 @@ def tow_chart(data, ylab, **kwargs): Matplotlib ax object props Dictionary of the text annotation properties - + """ + import matplotlib.pyplot as plt + import matplotlib as mpl + import importlib + import matplotlib.ticker as ticker + import matplotlib.font_manager as font_manager + import seaborn as sns + func() ymax = kwargs.get('ymax', None) ymin = kwargs.get('ymin', 0) - - + + ymax_flag = True if ymax == None: ymax = int(data.max()) ymax_flag = False - + delta = (ymax - ymin)/3 i = 0 while True: @@ -354,43 +457,83 @@ def tow_chart(data, ylab, **kwargs): if delta < 10: break yinc = kwargs.get('yinc', int(round(delta+1)*pow(10,i))) - + if ymax_flag == True: upper = ymax else: upper = int(3*yinc+ymin) - - fig, ax =plt.subplots() + + fig, ax = plt.subplots(dpi=450.0) ax.plot(data, linewidth = 2.5, color = colour.purple) - plt.grid() + # Commented to view weekday spans + #plt.grid() + ax.set_xlim(0, 168) ax.set_facecolor('xkcd:white') plt.xlabel('Time of week', fontname = font.normal, fontsize=9, horizontalalignment='left', x=0, labelpad=3, fontweight = 'bold') - ax.set_ylim([ymin,upper]) + ax.set_ylim([ymin, upper]) ax.grid(color='k', linestyle='-', linewidth=0.2) plt.ylabel(ylab, fontname = font.normal, fontsize=9, horizontalalignment='right', y=1, labelpad=7, fontweight = 'bold') fig.set_size_inches(6.1, 1.8) - ax.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}')) plt.yticks(range(ymin,upper+int(0.1*yinc), yinc), fontsize =9, fontname = font.normal) - ax.set_xticks(range(0,180,12)) - ax.set_xticklabels(['0','12','0','12', - '0','12','0','12', - '0','12','0','12','0','12'], fontname = font.normal, fontsize = 7, color = colour.light_grey) - - ax.xaxis.set_minor_locator(ticker.FixedLocator(list(range(12,180,24)))) - ax.xaxis.set_minor_formatter(ticker.FixedFormatter(['Monday','Tuesday', - 'Wednesday','Thursday', - 'Friday','Saturday','Sunday'])) - ax.tick_params(axis='x', which='minor', colors = 'k', labelsize=9, pad =14) +# # Basic - Y ticks +# list_major_ticks = np.arange(0, 180, 12) +# list_major_labels = ['0','12','0','12','0','12','0','12','0','12','0','12','0','12','0'] + +# # Experiment-1 - Y ticks (every 3hrs) +# list_major_ticks = np.arange(0, 180, 3) +# list_major_labels = ['0','3','6','9','12','15','18','21', +# '0','3','6','9','12','15','18','21', +# '0','3','6','9','12','15','18','21', +# '0','3','6','9','12','15','18','21', +# '0','3','6','9','12','15','18','21', +# '0','3','6','9','12','15','18','21', +# '0','3','6','9','12','15','18','21', +# '0'] + +# # Experiment-2 - Y ticks (every 6hrs) +# list_major_ticks = np.arange(0, 180, 6) +# list_major_labels = ['0','6','12','18', +# '0','6','12','18', +# '0','6','12','18', +# '0','6','12','18', +# '0','6','12','18', +# '0','6','12','18', +# '0','6','12','18', +# '0'] + + + + # Experiment-3 - Y ticks (only 9H & 18H) + list_major_ticks = [0, 9, 18, 24, 33, 42, 48, 57, 66, 72, 81, 90, 96, 105, 114, 120, 129, 138, 144, 153, 162, 168] + list_major_labels = ['0','9','18', + '0','9','18', + '0','9','18', + '0','9','18', + '0','9','18', + '0','9','18', + '0','9','18', + '0'] + + ax.xaxis.set_major_locator(ticker.FixedLocator(list_major_ticks)) + ax.xaxis.set_major_formatter(ticker.FixedFormatter(list_major_labels)) + ax.tick_params(axis='x', which='major', colors = colour.light_grey, labelsize=7) + + list_minor_ticks = list(range(12,180,24)) + list_minor_labels = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] + + # Minor ticks location isn't being set correctly with below + ax.xaxis.set_minor_locator(ticker.FixedLocator(list_minor_ticks)) + ax.xaxis.set_minor_formatter(ticker.FixedFormatter(list_minor_labels)) + ax.tick_params(axis='x',which='minor', colors = 'k', labelsize=9, pad=14) props = dict(boxstyle='round, pad=0.3',edgecolor=colour.purple, linewidth = 1.5, facecolor = 'w', alpha=1) - ax.set_xlim([0,167]) return fig, ax, props def stacked_chart(data_in, xlab, lab1, lab2, **kwargs): @@ -458,7 +601,7 @@ def stacked_chart(data_in, xlab, lab1, lab2, **kwargs): ind = np.arange(len(data)) - fig, ax = plt.subplots() + fig, ax = plt.subplots(dpi=450.0) fig.set_size_inches(6.1, len(data)) ax.grid(color='k', linestyle='-', linewidth=0.25) @@ -509,7 +652,7 @@ def stacked_chart(data_in, xlab, lab1, lab2, **kwargs): return fig, ax - def bar_chart(data_in, xlab,**kwargs): + def bar_chart(data_in, xlab, ylab, horizontal=False, **kwargs): """Creates a bar chart Parameters @@ -518,15 +661,19 @@ def bar_chart(data_in, xlab,**kwargs): Data for the bar chart. The dataframe must have 2 columns, the first representing the y ticks, and the second representing the data xlab : str Label for the x axis. - xmax : int, optional, default is the max s value + ylab : str + Label for the y axis. + horizontal: bool, Alignment of bar_chart + True if horizontal else vertical + xymax : int, optional, default is the max s value The max value of the y axis - xmin : int, optional, default is 0 + xymin : int, optional, default is 0 The minimum value of the x axis precision : int, optional, default is -1 Decimal places in the annotations - xinc : int, optional - The increment of ticks on the x axis. + xyinc : int, optional + The increment of ticks on the x axis/y axis depending on horizontal bool value. Returns -------- @@ -541,44 +688,63 @@ def bar_chart(data_in, xlab,**kwargs): data.columns = ['name', 'values1'] - xmin = kwargs.get('xmin', 0) - xmax = kwargs.get('xmax', None) + xymin = kwargs.get('xymin', 0) + xymax = kwargs.get('xymax', None) precision = kwargs.get('precision', 0) - xmax_flag = True - if xmax == None: - xmax = data['values1'].max() - xmax_flag = False + xymax_flag = True + if xymax == None: + xymax = data['values1'].max() + xymax_flag = False - delta = (xmax - xmin)/4 + delta = (xymax - xymin)/4 i = 0 while True: if delta < 10: break delta /= 10 i += 1 - xinc = kwargs.get('xinc', int(round(delta+1)*pow(10,i))) + xyinc = kwargs.get('xyinc', int(round(delta+1)*pow(10,i))) - if xmax_flag == True: - upper = xmax + if xymax_flag == True: + upper = xymax else: - upper = int(4*xinc+xmin) + upper = int(4*xyinc+xymin) ind = np.arange(len(data)) - fig, ax = plt.subplots() - fig.set_size_inches(6.1, len(data)*0.7) + fig, ax = plt.subplots(dpi=450.0) + fig.set_size_inches(6.1, 4.2) +# fig.set_size_inches(6.1, len(data)*0.7) ax.grid(color='k', linestyle='-', linewidth=0.25) - p2 = ax.barh(ind, data['values1'], 0.75, align='center', color = colour.purple) - ax.xaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}')) - - ax.xaxis.grid(True) - ax.yaxis.grid(False) - ax.set_yticks(ind) - ax.set_xlim(0,upper) - ax.set_yticklabels(data['name']) - ax.set_xlabel(xlab, horizontalalignment='left', x=0, labelpad=10, fontname = font.normal, fontsize=10, fontweight = 'bold') + if(horizontal): + p2 = ax.barh(ind, data['values1'], 0.75, align='center', color = colour.purple) + ax.xaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}')) + ax.xaxis.grid(True) + ax.yaxis.grid(False) + ax.set_yticks(ind) + ax.set_xlim(0,upper) + ax.set_yticklabels(data['name']) + ax.set_xlabel(xlab, horizontalalignment='left', x=0, labelpad=10, fontname = font.normal, fontsize=10, fontweight = 'bold') + if (ylab is not None): + ax.set_ylabel(ylab, labelpad=10, fontname = font.normal, fontsize=10, fontweight = 'bold') + plt.xticks(range(xymin,upper+int(0.1*xyinc), xyinc), fontname = font.normal, fontsize =10) + plt.yticks( fontname = font.normal, fontsize =10) + else: + p2 = ax.bar(ind, data['values1'], 1.0, align='center', color = colour.purple) + ax.yaxis.set_major_formatter(mpl.ticker.StrMethodFormatter('{x:,.0f}')) + ax.yaxis.grid(True) + ax.xaxis.grid(False) + ax.set_xticks(ind) + ax.set_ylim(0, upper) + ax.set_xticklabels(data['name'], rotation=0.0) + ax.set_ylabel(ylab, labelpad=10, fontname = font.normal, fontsize=10, fontweight = 'bold') + if (xlab is not None): + ax.set_xlabel(xlab, labelpad=10, fontname = font.normal, fontsize=10, fontweight = 'bold') + plt.yticks(range(xymin, upper+int(0.1*xyinc), xyinc), fontname = font.normal, fontsize =10) + plt.xticks( fontname = font.normal, fontsize =10) + ax.set_facecolor('xkcd:white') j=0 @@ -586,15 +752,20 @@ def bar_chart(data_in, xlab,**kwargs): data['values1'] = data['values1'].astype(int) j=0 - for i in data['values1']: - if i < 0.1*upper: - ax.annotate(str(format(round(i,precision), ',')), xy=(i+0.015*upper, j-0.05), ha = 'left', color = 'k', fontname = font.normal, fontsize=10) - else: - ax.annotate(str(format(round(i,precision), ',')), xy=(i-0.015*upper, j-0.05), ha = 'right', color = 'w', fontname = font.normal, fontsize=10) - j=j+1 + if (horizontal == True and (precision != -1)): + for i in data['values1']: + if i < 0.1*upper: + ax.annotate(str(format(round(i, precision), ',')), xy=(i+0.015*upper, j-0.05), ha = 'left', color = 'k', fontname = font.normal, fontsize=10) + else: + ax.annotate(str(format(round(i, precision), ',')), xy=(i-0.015*upper, j-0.05), ha = 'right', color = 'w', fontname = font.normal, fontsize=10) + j=j+1 + elif (horizontal == False and (precision != -1)): + for i in data['values1']: + if i < 0.1*upper: + ax.annotate(str(format(round(i, precision), ',')), xy=(j-0.15, i+0.015*upper), ha = 'left', color = 'k', fontname = font.normal, fontsize=10, rotation=90.) + else: + ax.annotate(str(format(round(i, precision), ',')), xy=(j+0.15, i-0.06*upper), ha = 'right', color = 'w', fontname = font.normal, fontsize=10, rotation=90.) + j=j+1 - - plt.xticks(range(xmin,upper+int(0.1*xinc), xinc), fontname = font.normal, fontsize =10) - plt.yticks( fontname = font.normal, fontsize =10) - + return fig, ax