def plotBar(pos, cases, dt, color, title): """ This plots based base on the time series data :param pos: :param cases: :param dt: :param color: :param title: :return: """ labels = [val[1] for val in cases] labels2 = [ "Test" ] + labels # this is to overcome a bug in the bar function x = np.arange(len(labels)) # the label locations axs[pos].bar(x, [val[0] for val in cases], color=color, edgecolor='k') axs[pos].set_title("{} on {}".format(title, str(dt)), fontsize=15) axs[pos].xaxis.set_major_locator(plt.FixedLocator(x)) axs[pos].xaxis.set_major_formatter(plt.FixedFormatter(labels)) #axs[pos].set_xticklabels(labels2) axs[pos].grid(axis="y") plt.setp(axs[pos].xaxis.get_majorticklabels(), rotation=35, fontsize=12)
def graph_flow_generator(total): ticket_dict = {} ticket_dict["Information"] = total[0] ticket_dict["Mineure"] = total[1] ticket_dict["Majeure"] = total[2] ticket_dict["Bloquante"] = total[3] ticket_dict["Autre"] = total[4] fig = plt.figure() ax = fig.add_subplot(111) frequencies = ticket_dict.values() names = ticket_dict.keys() x_coordinates = np.arange(len(ticket_dict)) ax.bar(x_coordinates, frequencies, align='center') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names)) plt.savefig("flow" + contract_id + ".svg") # plt.show() img_insert("Repartition des demandes", "flow" + contract_id + ".svg", "Repartition des demandes")
def bar_plot(d, bars, outdir='.'): N = len(bars) ind = numpy.arange(N) width = 0.35 plt.title("LCA of reads for contigs with alignment purity < 0.95", size='x-small') # add bars sum_values = sum(d.values()) plt.bar(ind, [d[k] * 100 / sum_values for k in bars], width, color='red') # Final bar is not really visible, increase xlim xmin, xmax = plt.xlim() plt.xlim(xmin=xmin-1) plt.xlim(xmax=xmax+1) # axis setup labels = [] for k in bars: labels.append('%s\n%d' % (k, d[k])) plt.xticks(ind + width / 2., bars, size='xx-small', rotation=17) tick_range = numpy.arange(0, 110, 10) plt.yticks(tick_range, size='xx-small') formatter = plt.FixedFormatter([str(x) for x in tick_range]) plt.gca().yaxis.set_major_formatter(formatter) plt.gca().yaxis.grid(which='major') plt.savefig(outdir + "/plots/barplot.png") plt.close()
def generate_histrogram_strings(buckets, title, x_label, y_label, output): fig = plt.figure() ax = fig.add_subplot(111) sorted_keys = sorted(buckets.keys(), key=lambda item: int(''.join(x for x in item if x.isdigit()))) indexes = np.arange(0, len(sorted_keys)) # the x locations for the groups width = 0.1 # the width of the bars ax.set_yscale('log') ax.grid(zorder=0, axis="y") bars1 = ax.bar(indexes, [buckets.get(sorted_keys[i], 0) for i in indexes], align='center', color='blue') # axes and labels ax.set_ylim(0.5, max(buckets.values()) + 1) ax.set_xlabel(x_label) ax.set_ylabel(y_label) ax.set_title(title) ax.xaxis.set_major_locator(plt.FixedLocator(indexes)) ax.xaxis.set_major_formatter(plt.FixedFormatter(sorted_keys)) labels = ax.get_xticklabels() for tick in labels: tick.set_rotation(90) plt.tight_layout() fig.savefig(output)
def plot_bar_graph_within(names, observered_data, result_data, title, fig, sub_plot_index): """ :param names: labels of x-axis :param observered_data: as variable name :param result_data: as variable name :param title: the title of the subplot :param fig: the fig object :param sub_plot_index: the index number is in order, such 221 means the generate a graph has 2 by 2 figure, which could have 4 subplots in it the 1 means the first subplot in the figure. :return: the fig object """ # define the subplot ax = fig.add_subplot(sub_plot_index) x = np.arange(names.__len__()) * 2 # set the value ax.bar(x, observered_data, align='center', color='red') ax.bar(x + 1, result_data, align='center', color='blue') # create the patch red_patch = mpatches.Patch(color='red', label='Observed Value') blue_patch = mpatches.Patch(color='blue', label='Simulated Value') # Set the label x-axis ax.xaxis.set_major_locator(plt.FixedLocator(x + 0.5)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names)) ax.set_title(title) plt.legend(handles=[red_patch, blue_patch]) return fig
def save_ability_power_statistics(statistics): fig = plt.figure() ax = fig.add_subplot(111) x = np.arange(len(statistics)) plt.bar(x, [s[1] for s in statistics], width=0.8, align='center') ax.set_xlim(-0.5, len(statistics) + 0.5) ax.set_ylim(0, statistics[0][1]) locator = plt.IndexLocator(1, 0.4) formatter = plt.FixedFormatter([s[0] for s in statistics]) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) plt.setp(plt.getp(ax, 'xticklabels'), rotation=45, fontsize=8, horizontalalignment='right') ax.set_title('Wins per ability') plt.tight_layout() plt.savefig('/tmp/wins.png')
def plot_bar_from_counter(counter, title, xlabel, ylabel, ax=None): """" This function creates a bar plot from a counter. :param counter: This is a counter object, a dictionary with the item as the key and the frequency as the value :param ax: an axis of matplotlib :return: the axis wit the object in it """ if ax is None: fig = plt.figure() ax = fig.add_subplot(111) frequencies = counter.values() names = list(counter.keys()) x_coordinates = np.arange(len(counter)) ax.bar(x_coordinates, frequencies, align='center') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names)) ax.xaxis.set_label_text(xlabel) ax.yaxis.set_label_text(ylabel) ax.set_title(title) plt.autoscale(enable=True, axis='x') DefaultSize = fig.get_size_inches() fig.set_size_inches((DefaultSize[0] * 5, DefaultSize[1] * 2.5)) mkdir_p(IMG_URL) plt.savefig(IMG_URL + 'GIST Data Histogram.png') plt.close() im = crop_image(IMG_URL + 'GIST Data Histogram.png') return ax
def save_ability_wins_distribution(statistics, ability_wins): fig = plt.figure() ax = fig.add_subplot(111) keys, wins = list(zip(*statistics)) # pylint: disable=W0612 data = [ability_wins[key] for key in keys] ax.boxplot(data) #, positions=[i for i in xrange(len(keys))]) ax.set_xlim(0.5, len(statistics) + 0.5) ax.set_ylim(0, TEST_BATTLES_NUMBER * len(HERO_LEVELS)) locator = plt.IndexLocator(1, 0.5) formatter = plt.FixedFormatter([s[0] for s in statistics]) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) plt.setp(plt.getp(ax, 'xticklabels'), rotation=45, fontsize=8, horizontalalignment='right') ax.set_title('wins destribution') plt.tight_layout() plt.savefig('/tmp/wins_destribution.png')
def plot_bar_from_counter(counter, ax=None): """" This function creates a bar plot from a counter. :param counter: This is a counter object, a dictionary with the item as the key and the frequency as the value :param ax: an axis of matplotlib :return: the axis wit the object in it """ if ax is None: fig = plt.figure(figsize=(7, 5)) ax = fig.add_subplot(111) frequencies = [f[1] for f in counter] names = [f[0] for f in counter] x_coordinates = np.arange(len(counter)) ax.barh(x_coordinates, frequencies, align='center') ax.yaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.yaxis.set_major_formatter(plt.FixedFormatter(names)) ax.yaxis.set_tick_params(labelsize=10) ax.set_ylim(-1, len(counter) + 1) ax.set_xlim(0, max(frequencies) + 300) for i, v in enumerate(frequencies): ax.text(v + 20, i - 0.25, str(v), color='black', fontweight='normal', fontsize=10) fig.subplots_adjust(left=0.4) return ax
def ohlc_plot(ax, df, open_='open', high_='high', low_='low', close_='close', t_='trading_day', width_=0.7, n_=10): assert isinstance(df, pd.DataFrame) assert all([x in df.columns for x in (open_, high_, low_, close_)]) n = len(df) width = width_ offset = width / 2.0 for i in range(n): op = df[open_].values[i] hi = df[high_].values[i] lo = df[low_].values[i] cl = df[close_].values[i] x = i y = min(op, cl) height = abs(cl - op) is_raise = op < cl clr = 'red' if is_raise else 'green' fill = True # True if is_raise else False rect = Rectangle((x - offset, y), width, height, facecolor=clr, edgecolor=clr, linewidth=1, fill=fill) vline = Line2D(xdata=(x, x), ydata=(lo, hi), linewidth=1, color=clr) ax.add_patch(rect) ax.add_line(vline) ax.autoscale_view() x_label_idx = n_ * np.array(range(len(df) // n_ + 1)) x_labels = df[t_].iloc[x_label_idx].values ax.xaxis.set_major_locator(plt.FixedLocator(x_label_idx)) ax.xaxis.set_major_formatter(plt.FixedFormatter(x_labels))
def plot_bar_from_counter(counter, ax=None): """" This function creates a bar plot from a counter. :param counter: This is a counter object, a dictionary with the item as the key and the frequency as the value :param ax: an axis of matplotlib :return: the axis wit the object in it """ if ax is None: fig = plt.figure() ax = fig.add_subplot(111) frequencies = counter.values() names = counter.keys() x_coordinates = np.arange(len(counter)) ax.bar(x_coordinates, frequencies, align='center') for idx in xrange(len(frequencies)): x = x_coordinates[idx] y = frequencies[idx] ax.text(x + 3, y + 3, str(v), color='red', fontweight='bold') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names)) ax.set_ylim(0, np.max(frequencies) * 1.1) return ax
def climate_plot(): df1 = climate_data() df1.name = 'Total GHG' df1.index = pd.to_datetime(df1.index, format='%Y') # 將index改為年datetime object df1.index = df1.index.to_period('A').to_timestamp('A') # 將datetime start of the year change to end of the year df_temp = df_temperature df_temp.index = pd.to_datetime(df_temp.index, format='%Y-%m-%d') # 將index改為年datetime object df2 = df_temp df2 = df2.resample('A').mean() # 重採樣為以年為頻率,方法為平均 df_temp = df_temp.resample('Q').mean() df_ghg_temp = normalize(pd.concat([df2, df1], axis=1, join='inner')).loc['1990':'2010'] # 1990 年 - 2010 年 fig, axes = plt.subplots(nrows=2, ncols=2) # 子图 1(线形图 kind = 'line'):绘制 1990 年 - 2010 年间,「全球历年温室气体排放总量」与「历年陆地平均温度」及「历年陆地-海洋平均温度」三者之间的线形图。 #plt.subplot(221) ax1 = df_ghg_temp.plot(kind='line', ax=axes[0, 0], figsize=(16, 9)) ax1.set_xlabel('Years') ax1.set_ylabel('Values') #ax1.legend(loc='best') ax1.autoscale(axis='x', tight=True) ax1.grid(True) # 子图 2(柱状图 kind = 'bar'):绘制 1990 年 - 2010 年间,「全球历年温室气体排放总量」与「历年陆地平均温度」及「历年陆地-海洋平均温度」三者之间的柱状图。 #plt.subplot(222) ax2 = df_ghg_temp.plot(kind='bar', ax=axes[0, 1], figsize=(16, 9)) ax2.xaxis.set_major_formatter(plt.FixedFormatter(df_ghg_temp.index.to_series().dt.strftime("%Y"))) ax2.set_xlabel('Years') ax2.set_ylabel('Values') #ax2.legend(loc='best') ax2.autoscale(axis='x', tight=True) ax2.grid(True) # 子图 3(面积图 kind = 'area'):绘制有气象数据的年份,「各季度地面平均温度」与「各季度地面-海洋平均温度」面积图。 #plt.subplot(223) ax3 = df_temp.plot(kind='area', ax=axes[1, 0], figsize=(16, 9)) ax3.set_xlabel('Quarters') ax3.set_ylabel('Temperature') #ax3.legend(loc='best') ax3.autoscale(axis='x', tight=True) ax3.grid(True) # 子图 4(核密度估计图 kind = 'kde'):绘制有气象数据年份,「各季度地面平均温度」与「各季度地面-海洋平均温度」对应的核密度估计图。 #plt.subplot(224) ax4 = df_temp.plot(kind='kde', ax=axes[1, 1], figsize=(16, 9)) # set figsize 避免擠在一起 ax4.set_xlabel('Values') ax4.set_ylabel('Values') ax4.autoscale(axis='x', tight=True) ax4.grid(True) # Adjust the subplot layout, because the logit one may take more space # than usual, due to y-tick labels like "1 - 10^{-3}" plt.subplots_adjust(top=0.92, bottom=0.08, left=0.10, right=0.95, hspace=0.25, wspace=0.35) plt.show() return fig
def palplot(k, cmap="viridis"): pal = sns.color_palette(palette=cmap, n_colors=k) fig, ax = plt.subplots(1, 1) pal = np.array(pal) pal = pal.reshape((k, 1, 3)) ax.imshow(pal) ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_formatter(plt.FixedFormatter(np.arange(k, dtype=int))) ax.yaxis.set_major_locator(plt.FixedLocator(np.arange(k))) return ax
def plot_bar_from_counter(counter): ax = fig.add_subplot(1, 1, 1) x_coordinates = np.arange(len(counter)) ax.bar(x_coordinates, counter.values(), align='center') ax.xaxis.set_tick_params(rotation=90) ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(list(counter.keys()))) return ax
def test_twin_axis_locaters_formatters(): vals = np.linspace(0, 1, num=5, endpoint=True) locs = np.sin(np.pi * vals / 2.0) majl = plt.FixedLocator(locs) minl = plt.FixedLocator([0.1, 0.2, 0.3]) fig = plt.figure() ax1 = fig.add_subplot(1, 1, 1) ax1.plot([0.1, 100], [0, 1]) ax1.yaxis.set_major_locator(majl) ax1.yaxis.set_minor_locator(minl) ax1.yaxis.set_major_formatter(plt.FormatStrFormatter('%08.2lf')) ax1.yaxis.set_minor_formatter(plt.FixedFormatter(['tricks', 'mind', 'jedi'])) ax1.xaxis.set_major_locator(plt.LinearLocator()) ax1.xaxis.set_minor_locator(plt.FixedLocator([15, 35, 55, 75])) ax1.xaxis.set_major_formatter(plt.FormatStrFormatter('%05.2lf')) ax1.xaxis.set_minor_formatter(plt.FixedFormatter(['c', '3', 'p', 'o'])) ax2 = ax1.twiny() ax3 = ax1.twinx()
def create_histogram(self): figure = plt.figure() ax = figure.add_subplot(111) frequencies = self.ticket_dict.values() names = self.ticket_dict.keys() x_coordinates = np.arange(len(self.ticket_dict)) ax.bar(x_coordinates , frequencies , align='center') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names))
def test_learning_rates(train_set_x, train_set_y, test_set_x, test_set_y): """ Plot the learning curve and the accuracy varying the learning rate Arguments: X_train -- training set represented by a numpy array of shape (number of features, m_train) Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train) X_test -- test set represented by a numpy array of shape (number of features, m_test) Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test) """ learning_rates = [0.01, 0.001, 0.0001, 0.05, 0.005] train_accuracy = [] test_accuracy = [] models = {} for i in learning_rates: print("learning rate is: " + str(i)) models[str(i)] = logistic_regression(train_set_x, train_set_y, test_set_x, test_set_y, epochs=1500, learning_rate=i) train_accuracy.append(models[str(i)]["train_accuracy"]) test_accuracy.append(models[str(i)]["test_accuracy"]) print('\n' + "-------------------------------------------------------" + '\n') for i in learning_rates: plt.plot(np.squeeze(models[str(i)]["costs"]), label=str(models[str(i)]["learning_rate"])) #plot learning curves plt.title('Learning curve per Learning rate') plt.ylabel('cost') plt.xlabel('iterations (hundreds)') legend = plt.legend(loc='upper center', shadow=True) frame = legend.get_frame() frame.set_facecolor('0.90') plt.show() #plot accuracy bar graph df = pd.DataFrame({ "Train accuracy": train_accuracy, "Test accuracy": test_accuracy }) ax = df.plot.bar(color=["SkyBlue", "LightGreen"], rot=0, title="Accuracy per learning rate") ax.set_xlabel("Learning rate") ax.set_ylabel("Accuracy") ax.xaxis.set_major_formatter(plt.FixedFormatter(learning_rates)) plt.show()
def plot_daily_data(df, state, type_): fig = sns.barplot(df.index, df[state]) fig.set_title(f'{type_} Cases ({state})') fig.set_ylabel('Number of Cases') fig.set_xticklabels(fig.get_xticklabels(), rotation=45, horizontalalignment='right', fontweight='light') plt.gca().xaxis.set_major_formatter( plt.FixedFormatter(df.index.to_series().dt.strftime('%d %b %Y')))
def plot_color_labels(meta, ax): sizes = meta.groupby(["merge_class"], sort=False).size() uni_class = sizes.index.unique() counts = sizes.values count_map = dict(zip(uni_class, counts)) names = [] colors = [] for key, val in count_map.items(): names.append(f"{key} ({count_map[key]})") colors.append(CLASS_COLOR_DICT[key]) colors = colors[::-1] # reverse because of signal flow sorting names = names[::-1] palplot(len(colors), colors, ax=ax) ax.yaxis.set_major_formatter(plt.FixedFormatter(names))
def obspred_plot(paramsplot, fityrs, startage, endage, trans = 'none', trans_time_coords = False, trans_y_coords = False): obsframe = paramsplot['test'].rx2('obs') ptype = paramsplot['ptype'] obsage = obsframe.rx2('age') predage = base.seq(startage, endage, by = 5) xlinlab = r'$\mathrm{log}(t)$' ratelinlab = r'$\mathrm{log}(r(t))$' survlinlab = r'$\mathrm{log}[-\mathrm{log}[S(t)]]$' plotlabs = {'none': {'x': '$t$', 'rate': '$r(t)$', 'surv': '$S(t)$'}, 'gomp_lin': {'x': '$t$', 'rate': ratelinlab, 'surv': survlinlab}, 'weib_lin': {'x': xlinlab,'rate': ratelinlab, 'surv': survlinlab}} plt.close() fig = plt.figure() ax = fig.add_subplot(111) def trans_y_ticks(x, p): ticks_round = np.round(predcols['none'][ptype], 6) return str(ticks_round[p]).replace('.', ',') for yr in fityrs: obs = obsframe.rx2(str(yr)) pred = stats.predict(paramsplot['test'].rx2('sourcefit').rx2(str(yr)), newdata = ro.r['list'](age = predage)) obscols = transdict(np.array(obsage), np.array(obs)) predcols = transdict(np.array(predage), np.array(pred)) obsplot = ax.plot(obscols[trans]['x'], obscols[trans][ptype], 'o', label = str(yr)) curcolor = obsplot[0].get_color() predplot = ax.plot(predcols[trans]['x'], predcols[trans][ptype], color = curcolor) plt.legend(loc = 2, framealpha = 0.5) ax.set_title('Observerad vs förutsedd ' + paramsplot['plottitle']) if (trans_time_coords): ax.xaxis.set_major_locator(plt.FixedLocator(predcols[trans]['x'])) ax.xaxis.set_major_formatter(plt.FixedFormatter(predcols['none']['x'])) ax.set_xlabel(plotlabs['none']['x']) plt.xticks(rotation = 90) else: ax.set_xlabel(plotlabs[trans]['x']) if (trans_y_coords): ax.yaxis.set_major_locator(plt.FixedLocator(predcols[trans][ptype])) ax.yaxis.set_major_formatter(plt.FuncFormatter(trans_y_ticks)) ax.set_ylabel(plotlabs['none'][ptype]) else: ax.set_ylabel(plotlabs[trans][ptype])
def plot_bar_from_counter(counter, ax=None): if ax is None: fig = plt.figure() ax = fig.add_subplot(111) frequencies = counter.values() names = counter.keys() x_coordinates = np.arange(len(counter)) ax.bar(x_coordinates, frequencies, align='center') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names)) return ax
def configure(self): # Set various axis parameters self.axis.set_title("Performance of the rat", fontsize=22, pad=10) self.axis.set_xlabel("Trials", fontsize=16, labelpad=20) self.axis.set_ylabel("Path length (m)", fontsize=16, labelpad=10) self.axis.set_ylim(0, TRIAL_TIMEOUT * SWIMING_SPEED * 1.5) self.axis.tick_params(length=0, labelsize=12) # Add an horizontal grid to the plot self.axis.yaxis.grid() # Group the labels by day using X labels self.axis.xaxis.set_major_locator( plt.FixedLocator([2.5 + (i * 4) for i in range(0, 10)])) self.axis.xaxis.set_major_formatter( plt.FixedFormatter(["Day " + str(i) for i in range(1, 10)]))
def plot_bar_graph(dictionary, ax=None): if ax is None: fig = plt.figure() ax = fig.add_subplot(111) counts = dictionary.values() directories = dictionary.keys() plt.xlabel('Directories') plt.ylabel('Counts') x_coordinates = np.arange(len(dictionary)) ax.bar(x_coordinates, counts, align='center') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(directories)) return ax
def plot_class_colormap(): from src.visualization import palplot from src.data import load_metagraph mg = load_metagraph("G") uni_class, counts = np.unique(mg["merge_class"], return_counts=True) count_map = dict(zip(uni_class, counts)) names = [] colors = [] for key, val in CLASS_COLOR_DICT.items(): if key in uni_class: names.append(f"{key} ({count_map[key]})") colors.append(val) fig, ax = plt.subplots(1, 1, figsize=(3, 15)) palplot(len(colors), colors, ax=ax) ax.yaxis.set_major_formatter(plt.FixedFormatter(names)) plt.savefig("./maggot_models/notebooks/outs/current_cmap.png", dpi=300, bbox_inches="tight")
def plot_hist_days(df, filename=None, col='latitude', figsize=(15,10), timestamp_col='timestamp'): ''' plot distribution of the data collected on each day ''' hist_df = df.set_index(pd.DatetimeIndex(df[timestamp_col]), drop=False, inplace=False) hist_df = hist_df[[col]].groupby(pd.Grouper(freq='D')).count() sns.set() fig, ax = plt.subplots(figsize=figsize) plot = sns.barplot(x=hist_df.index, y=hist_df[col], ax=ax); ax.set_xlabel('Timestamp'); ax.set_ylabel('Count'); ax.xaxis_date(); ax.xaxis.set_major_formatter(plt.FixedFormatter(hist_df.index.to_series().dt.strftime("%d %b"))); for tick in ax.get_xticklabels(): tick.set_rotation(65); if filename is not None: fig = plot.get_figure() fig.savefig(filename)
def save_ability_mathces_statistics(statistics, matches): # pylint: disable=R0914 fig = plt.figure() ax = fig.add_subplot(111) keys, wins = list(zip(*statistics)) # pylint: disable=W0612 index = dict((key, i) for i, key in enumerate(keys)) data = [] for (x, y), (w_1, w_2) in list(matches.items()): data.append((index[x], index[y], 1000 * w_1 / float(w_1 + w_2))) data.append((index[y], index[x], 1000 * w_2 / float(w_1 + w_2))) x, y, powers = list(zip(*data)) ax.scatter(x, y, s=powers, marker='o', c=powers) ax.set_xlim(-0.5, len(statistics) + 0.5) ax.set_ylim(-0.5, len(statistics) + 0.5) locator = plt.IndexLocator(1, 0) formatter = plt.FixedFormatter([s[0] for s in statistics]) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) plt.setp(plt.getp(ax, 'xticklabels'), rotation=45, fontsize=8, horizontalalignment='right') ax.yaxis.set_major_locator(locator) ax.yaxis.set_major_formatter(formatter) plt.setp(plt.getp(ax, 'yticklabels'), fontsize=8) ax.set_title('matches results') plt.tight_layout() plt.savefig('/tmp/matches.png')
def test_epochs(train_set_x, train_set_y, test_set_x, test_set_y): """ Plot the accuracy varying the number of epochs Arguments: X_train -- training set represented by a numpy array of shape (number of features, m_train) Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train) X_test -- test set represented by a numpy array of shape (number of features, m_test) Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test) """ epochs = [100, 500, 1000, 2000, 5000, 10000] models = {} train_accuracy = [] test_accuracy = [] for i in epochs: print("No of epochs: " + str(i)) models[str(i)] = logistic_regression(train_set_x, train_set_y, test_set_x, test_set_y, epochs=i) train_accuracy.append(models[str(i)]["train_accuracy"]) test_accuracy.append(models[str(i)]["test_accuracy"]) print('\n' + "-------------------------------------------------------" + '\n') #plot accuracy bar graph df = pd.DataFrame({ "Train accuracy": train_accuracy, "Test accuracy": test_accuracy }) ax = df.plot.bar(color=["SkyBlue", "LightGreen"], rot=0, title="Accuracy per number of epochs") ax.set_xlabel("Number of epochs") ax.set_ylabel("Accuracy") ax.xaxis.set_major_formatter(plt.FixedFormatter(epochs)) plt.show()
def plot_distrib(train_distrib, val_distrib, test_distrib, info): fig, ax = plt.subplots() width = 0.25 x = np.arange(len(info)) ax.bar(x - width, train_distrib, width, label="Trainig") ax.bar(x, val_distrib, width, label="Validation") ax.bar(x + width, test_distrib, width, label="Test") #setting custom labels: put symbols (user input) instead of numbers on the x avis ax.xaxis.set_major_locator(plt.FixedLocator(x)) ax.xaxis.set_major_formatter(plt.FixedFormatter(info)) #plot configuration plt.title("Distribution of Instances") plt.xlabel("Symbol") plt.ylabel("Frequency") ax.legend() ax.grid(True, axis='y', alpha=0.35) plt.show()
def plot_case_histograms(df, top_n_provinces=6, suptitle=''): resampled = df.T.iloc[:, :top_n_provinces].resample('d').sum() print(resampled.tail()) # df.columns = [dt.strftime('%m/%d') for dt in df.columns] axes = resampled.plot.bar(rot=90, subplots=True, sharey=True, figsize=(6, 8)) for ax in axes: ax.legend(loc=2) ax.set(title='') ax.xaxis.set_major_formatter( plt.FixedFormatter(resampled.index.to_series().dt.strftime("%d %b"))) fig = plt.gcf() fig.suptitle(suptitle, y=1) # fig.autofmt_xdate() fig.set_tight_layout(True)
def plot_bar_from_counter(counter, ax=None): """" This function creates a bar plot from a counter. :param counter: This is a counter object, a dictionary with the item as the key and the frequency as the value :param ax: an axis of matplotlib :return: the axis wit the object in it """ if ax is None: fig = plt.figure() ax = fig.add_subplot(111) frequencies = counter.values() names = counter.keys() x_coordinates = np.arange(len(counter)) ax.bar(x_coordinates, frequencies, align='center') ax.xaxis.set_major_locator(plt.FixedLocator(x_coordinates)) ax.xaxis.set_major_formatter(plt.FixedFormatter(names)) return ax