def bar_image(data, params): ## from input data, create a bar chart image, store in outfile filename = params['filename'] outfile = get_anlpath() + filename img_title = params['title'] img_ylabel = params['ylabel'] img_xlabel = params['xlabel'] keys_values = list(data.items()) keys_values.sort() x_labels = [a for (a, b) in keys_values] x_pos = numpy.arange(len(x_labels)) y_data = [b for (a, b) in keys_values] plt.figure(figsize=(10, 8)) plt.bar(x_pos, y_data) plt.title(img_title) plt.ylabel(img_ylabel) plt.xlabel(img_xlabel) plt.xticks(x_pos, tuple(x_labels)) plt.savefig(outfile)
def stacked_bar_image(data, params): ## from input data, create a stacked bar chart image, store in outfile filename = params['filename'] outfile = get_anlpath() + filename img_title = params['title'] img_ylabel = params['ylabel'] img_xlabel = params['xlabel'] colors = params['colors'] dates = list(data.keys()) dates.sort() groups = [g for g in data[dates[0]].keys() if g != 'total'] groups.sort() groups.reverse() N = len(dates) ind = numpy.arange(N) ## list of series of length equal to the number of groups ## which each series of length equal to the number of dates seriesList = [] for g in groups: this_group_series = [] for d in dates: this_group_series.append(data[d][g]) this_group_tuple = tuple(this_group_series) seriesList.append(this_group_tuple) ## build the bottom of each bar based on previous series ## initialize bottoms with zeros for each column bottoms = [] this_list = [] ld = range(len(dates)) for d in ld: this_list.append(0) bottoms.append(this_list) ## fill in with bottom + prev value for s in range(1, len(seriesList)): this_list = [bottoms[s - 1][d] + seriesList[s - 1][d] for d in ld] bottoms.append(this_list) plt.figure(figsize=(10, 8)) bars = [] for i in range(len(seriesList)): bars.append( plt.bar(ind, seriesList[i], bottom=bottoms[i], color=colors[i])) plt.title(img_title) plt.ylabel(img_ylabel) plt.xlabel(img_xlabel) plt.xticks(ind, tuple(dates)) plt.legend(tuple([p[0] for p in bars]), tuple(groups)) plt.savefig(outfile)
def line_image(data, params): ## from input data, create a line graph image, store in outfile filename = params['filename'] outfile = get_anlpath() + filename img_title = params['title'] img_ylabel = params['ylabel'] img_xlabel = params['xlabel'] keys_values = list(data.items()) keys_values.sort() x_labels = [a for (a, b) in keys_values] x_pos = numpy.arange(len(x_labels)) y_data = [b for (a, b) in keys_values] plt.figure(figsize=(17, 11)) plt.plot(x_pos, y_data) # zip joins x and y coordinates in pairs for x, y in zip(x_pos, y_data): label = "{:.0f}".format(y) plt.annotate( label, # this is the text (x, y), # this is the point to label textcoords="offset points", # how to position the text fontweight=1000, # boldness of text xytext=(0, 10), # distance from text to points (x,y) rotation=300, # rotation in degrees of the label text rotation_mode= 'anchor', # text will be first aligned to horiz and vert axis before rotation ha='center') # horizontal alignment can be left, right or center plt.title(img_title) plt.ylabel(img_ylabel) plt.xlabel(img_xlabel) plt.xticks(x_pos, tuple(x_labels), rotation=90) plt.savefig(outfile)
def bar_image(data, params): ## from input data, create a bar chart image, store in outfile filename = params['filename'] outfile = get_anlpath() + filename img_title = params['title'] img_ylabel = params['ylabel'] img_xlabel = params['xlabel'] keys_values = list(data.items()) keys_values.sort() x_labels = [a for (a, b) in keys_values] x_pos = numpy.arange(len(x_labels)) y_data = [b for (a, b) in keys_values] plt.figure(figsize=(10, 8)) plt.bar(x_pos, y_data) # zip joins x and y coordinates in pairs for x, y in zip(x_pos, y_data): label = "{:.0f}".format(y) plt.annotate( label, # this is the text (x, y), # this is the point to label textcoords="offset points", # how to position the text xytext=(0, 10), # distance from text to points (x,y) ha='center') # horizontal alignment can be left, right or center plt.title(img_title) plt.ylabel(img_ylabel) plt.xlabel(img_xlabel) plt.xticks(x_pos, tuple(x_labels)) plt.savefig(outfile)
def stacked_bar_image(data, params): ## from input data, create a stacked bar chart image, store in outfile filename = params['filename'] outfile = get_anlpath() + filename img_title = params['title'] img_ylabel = params['ylabel'] img_xlabel = params['xlabel'] colors = params['colors'] dates = list( data.keys() ) dates.sort() groups = [ g for g in data[dates[0]].keys() if g != 'total' ] groups.sort() groups.reverse() N = len(dates) ind = numpy.arange(N) ## list of series of length equal to the number of groups ## which each series of length equal to the number of dates seriesList = [] for g in groups: this_group_series = [] for d in dates: this_group_series.append( data[d][g] ) this_group_tuple = tuple(this_group_series) seriesList.append( this_group_tuple ) ## build the bottom of each bar based on previous series ## initialize bottoms with zeros for each column bottoms = [] this_list = [] ld = range( len(dates) ) for d in ld: this_list.append(0) bottoms.append( this_list ) ## fill in with bottom + prev value for s in range(1, len(seriesList) ): this_list = [ bottoms[s-1][d] + seriesList[s-1][d] for d in ld ] bottoms.append( this_list ) fig = plt.figure(figsize=(10,8)) ax = fig.add_subplot(111) bars = [] for i in range(len(seriesList)): bars.append( plt.bar(ind, seriesList[i], bottom=bottoms[i], color=colors[i] ) ) slna = numpy.array(seriesList) # search all of the bar segments and annotate for j in range(len(bars)): for i, patch in enumerate(bars[j].get_children()): bl = patch.get_xy() y = 0.5*patch.get_width() + bl[0] x = 0.5*patch.get_height() + bl[1] mpl.rcParams['text.color'] = 'black' mpl.rcParams['font.size'] = 11 mpl.rcParams['font.weight'] = 700 ax.text(y,x, (slna[j,i]), ha='center') plt.title(img_title) plt.ylabel(img_ylabel) plt.xlabel(img_xlabel) plt.xticks(ind, tuple(dates)) plt.legend(tuple( [ p[0] for p in bars ] ), tuple(groups)) plt.savefig(outfile)
def multi_line_image(data, params): ## from input data, create a stacked bar chart image, store in outfile filename = params['filename'] outfile = get_anlpath() + filename img_title = params['title'] img_ylabel = params['ylabel'] img_xlabel = params['xlabel'] colors = params['colors'] linestyle = params['linestyle'] dates = list(data.keys()) dates.sort() groups = [g for g in data[dates[0]].keys() if g != 'total'] groups.sort() groups.reverse() N = len(dates) ind = numpy.arange(N) ## list of series of length equal to the number of groups ## which each series of length equal to the number of dates seriesList = [] for g in groups: this_group_series = [] for d in dates: this_group_series.append(data[d][g]) this_group_tuple = tuple(this_group_series) seriesList.append(this_group_tuple) ## build the bottom of each bar based on previous series ## initialize bottoms with zeros for each column bottoms = [] this_list = [] ld = range(len(dates)) for d in ld: this_list.append(0) bottoms.append(this_list) ## fill in with bottom + prev value for s in range(1, len(seriesList)): this_list = [seriesList[s - 1][d] for d in ld] bottoms.append(this_list) fig = plt.figure(figsize=(17, 11)) ax = fig.add_subplot(111) # fig, (ax1, ax2) = plt.subplot(1, 2) lines = [] for i in range(len(seriesList)): lines.append( plt.plot(ind, seriesList[i], color=colors[i], linestyle=linestyle[i], linewidth=5)) # slna = numpy.array(seriesList) # search all of the bar segments and annotate # for j in range(len(lines)): # print(len(lines)) # for i, patch in enumerate(lines[j]): # print(lines[j])# returned [<matplotlib.lines.Line2D object at 0x7fd389667048>] 5 times LEFT OFF HERE 05/04/2020 # bl = patch.get_xy() # y = 0.5*patch.get_width() + bl[0] # x = 0.5*patch.get_height() + bl[1] # mpl.rcParams['text.color'] = 'black' # mpl.rcParams['font.size'] = 11 # mpl.rcParams['font.weight'] = 700 # ax.text(ind,N, (slna[j,i]), ha='center') plt.title(img_title) plt.ylabel(img_ylabel) plt.xlabel(img_xlabel) plt.xticks(ind, tuple(dates), rotation=90) plt.legend(tuple([p[0] for p in lines]), tuple(groups), loc='upper right', bbox_to_anchor=(1.1, 1.14)) # plt.show() plt.savefig(outfile)