def plot_diff(ax, for_date, data, previous_date, previous_data, diff_ylims=None, diff_log_scale=None, earliest=None, colormap=DEFAULT_COLORMAP, date_format=DEFAULT_DATE_FORMAT): diff = data.sub(previous_data, fill_value=0) total_diff = diff.sum().sum() stacked_bar_plot(ax, diff, colormap) ax.set_title(f'Change between reports on {previous_date} and {for_date}: {total_diff:,.0f}') fix_x_axis(ax, diff, earliest, date_format) ax.yaxis.set_label_position("right") ax.yaxis.tick_right() ax.yaxis.grid(True) if diff_ylims: ax.set_ylim(diff_ylims) if diff_log_scale: ax.set_yscale('symlog') else: ax.yaxis.set_major_locator(MaxNLocator(nbins='auto', integer=True)) ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) ax.axhline(y=0, color='k')
def generate_graph(self): """ Generates a graph of daily price data """ prices = [] average = [] for data in self.graph_data['daily']: prices.append(self.graph_data['daily'][data]) for data in self.graph_data['average']: average.append(self.graph_data['average'][data]) plotter.rcParams['ytick.color'] = 'lightslategrey' plotter.rcParams['figure.figsize'] = 8, 3 plotter.box(on=None) plotter.xticks([]) plotter.title('Past 180 days', loc='right', color='lightslategrey') plotter.plot(average, color="red") plotter.plot(prices, color="lightslategrey") plotter.gca().yaxis.set_major_formatter( StrMethodFormatter('{x:,.0f} gp')) plotter.savefig('assets/graph.png', transparent=True) plotter.close()
def __add_new_axis(self, variable: str, nb: int, n_rows: int, n_cols: int): """ Add a new axis to the axes pool Parameters ---------- variable: str The name of the graph nb: int The total number of axes to create n_rows: int The number of rows for the subplots n_cols: int The number of columns for the subplots """ if self.automatically_organize: self.all_figures.append( plt.figure(variable, figsize=(self.width_step / 100, self.height_step / 131))) else: self.all_figures.append(plt.figure(variable)) axes = self.all_figures[-1].subplots(n_rows, n_cols) if isinstance(axes, np.ndarray): axes = axes.flatten() else: axes = [axes] for i in range(nb, len(axes)): axes[i].remove() axes = axes[:nb] idx_center = n_rows * n_cols - int(n_cols / 2) - 1 if idx_center >= len(axes): idx_center = len(axes) - 1 axes[idx_center].set_xlabel("time (s)") self.all_figures[-1].tight_layout() for ax in axes: ax.yaxis.set_major_formatter( StrMethodFormatter("{x:,.1f}")) # 1 decimal places return axes
def barras_apildas_sin_bought_components(total_cost_manufactured_false, total_cost_manufactured_true): manufactured_false = [] manufactured_false = list(total_cost_manufactured_false['M'].values()) manufactured_true = [] manufactured_true = list(total_cost_manufactured_true['M'].values()) fig, ax = plt_.subplots() fig.set_size_inches(7.38, 6.13) dif = [] percentage = [] for i in range(len(manufactured_true)): dif.append(manufactured_false[i] - manufactured_true[i]) percentage.append((manufactured_false[i] - manufactured_true[i]) / manufactured_false[i]) ind = np.arange(len(manufactured_true)) width = 0.35 # dif = [] # for i in range(len(b_means)): # dif.append(b_means[i]-a_means[i]) p1 = plt_.bar(ind, manufactured_false, width, color='#3498db') p2 = plt_.bar(ind, dif, width, bottom=manufactured_false, color='#f8c471') plt_.ylabel('Unitary cost [€]') plt_.xlabel('Number of Produced Units') plt_.xticks(ind, ["Prototype", "100uds", "1,000uds", "10,000uds", "50,000uds"]) plt_.grid(lw=0.1) ax.yaxis.set_major_formatter(FormatStrFormatter('%d')) ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,}')) plt_.legend(['No MOD', 'MOD']) fig.set_size_inches(7.38, 6.13) fig.savefig('barras_apiladas_sin_bought.png', bbox_inches="tight") plt_.show() print("Percentage Sin bought components", percentage)
def plot_contour_angle_hist(list_info, level=0.5, tol=0.1): start = 0 stop = 180 bin_count = 100 bin_edges = np.linspace(start, stop, bin_count + 1) bin_width = (stop - start) / bin_count for i, info in enumerate(list_info): degrees = get_angles(info["mask_filepath"], level, tol) hist, bin_edges = np.histogram(degrees, bins=bin_edges) freq = hist / np.sum(hist) plt.bar(bin_edges[1:] - bin_width/2, freq, width=bin_width, alpha=0.5, label=info["name"]) plt.title("Histogram of relative contour angles") plt.xlabel("Relative angle") plt.ylabel("Freq") plt.gca().xaxis.set_major_formatter(StrMethodFormatter(u"{x:.0f}°")) plt.legend(loc="upper left") plt.xlim(0, 180) plt.savefig("histogram_of_relative_contour_angles.pdf", transparent=True) plt.show()
def confusion(ax, y_pred, y_test, len_b, algorithm): angles = [n / float(len_b) * 2 * pi for n in range(1, len_b)] angles += angles[:1] ax.set_theta_offset(pi / 2) ax.set_theta_direction(-1) ax.set_xticks(angles[:-1]) bbot, values = [str(i) for i in range(1, len_b)], list() ax.set_xticklabels(bbot) ax.set_rlabel_position(0) values = list( user_defined_matthews_corrcoef(y_test, y_pred, averaging='no').values()) values += values[:1] ax.plot(angles, values, label=algorithm) ax.fill(angles, values, alpha=0.1) ax.tick_params(axis="x", labelsize=6) ax.tick_params(axis="y", labelsize=6) ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.0f}')) ax.yaxis.set_minor_formatter(NullFormatter()) ax.set_title(algorithm)
def Graph_the_book(ltrs_counts, opt): percent = pop_perct(ltrs_counts) x_values = np.arange(len(percent)) plt.bar(x_values, list(percent.values()), align='center', width=0.7) plt.xticks(x_values, percent.keys()) y_max = max(percent.values()) + 1 plt.ylim(0, y_max) plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:g} %')) plt.title('Percentage of Each Letter\'s Appearance in \n{}\'s {}'.format( bk_author, bk_title), fontsize=12) plt.xlabel('Letters of the Alphabet') if opt == 1: plt.show() else: plt.savefig('Graph of {}.png'.format(bk_title), dpi=150) plt.cla() plt.clf() plt.close()
def do_scatter(x, y, trend=True): '''scatter plot with optional trendline''' name = x.name if not trend: # categorical categories = y_train.groupby(X_train[name]).mean().sort_values().index x = OrdinalEncoder([categories]).fit_transform(x.to_frame()) sns.regplot(x, y, ci=None, color='C1', fit_reg=trend, scatter_kws={'alpha': .3}) plt.xlabel(name) plt.ylabel('Price (£)') if not trend: # categorical plt.xticks(ticks=range(len(categories)), labels=categories, rotation=90) plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}'))
def add_convergence_line(ax, ns, errors, yscale="log2", xlabel="$N$", name=""): # Remove small tick lines on the axes, that doesnt have any number with them. matplotlib.rcParams['xtick.minor.size'] = 0 matplotlib.rcParams['xtick.minor.width'] = 0 matplotlib.rcParams['ytick.minor.size'] = 0 matplotlib.rcParams['ytick.minor.width'] = 0 rc('font', **{'family': 'sans-serif', 'sans-serif': ['Helvetica']}) # for Palatino and other serif fonts use: # rc('font',**{'family':'serif','serif':['Palatino']}) rc('text', usetex=True) res = np.polyfit(np.log(ns), np.log(errors), deg=1) print("Polyfit:", name, res) print("Order of convergence", -res[0]) ax.plot(ns, errors, "-o", label=f'${name}: {abs(round(res[0], 3))}$') if yscale == "log2": ax.set_yscale("log", base=2) else: ax.set_yscale("log") ax.set_xscale("log") # Remove scientific notation along x-axis ax.xaxis.set_major_formatter(StrMethodFormatter('{x:.0f}')) ax.xaxis.set_minor_formatter(NullFormatter()) ax.set_xticks(ns) ns_names = [] for n in ns: ns_names.append(f'${round(n, 3)}$') ax.set_xticklabels(ns_names) ax.set_xlabel(xlabel) ax.set_ylabel(r"\textrm{Error}") ax.legend() return ax
def make_plot(columnsName, xlabel, ylabel, title): df_kaggle = pd.read_csv('Hotel_Reviews.csv') df_kaggle = df_kaggle[columnsName].value_counts()[:10].sort_values().tail( 15) ax = df_kaggle.plot(kind='barh', figsize=(4, 6), color='#86bf91', zorder=2, width=0.85) # Despine ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['left'].set_visible(False) ax.spines['bottom'].set_visible(False) # Switch off ticks ax.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on") # Draw vertical axis lines vals = ax.get_xticks() for tick in vals: ax.axvline(x=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1) ax.set_xlabel(xlabel, labelpad=20, weight='bold', size=12) ax.set_ylabel(ylabel, labelpad=20, weight='bold', size=12) ax.set_title(title) ax.xaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))
def eval_training_validation(self, stor_dir='.'): ''' Plot training and validation loss within one figure :return: ''' stat = np.zeros((self._n_folds, self._epochs)) val = np.zeros((self._n_folds, self._epochs)) plt.figure() plt.set_cmap('tab10') plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.2f}')) for i in range(self._n_folds): tmp = pd.read_csv(stor_dir + '/' + self._experiment_name + '/statistic/stat_fold_' + str(i + 1) + '.csv', header=None).values[:, 1:] stat[i, :] = np.mean(tmp, axis=-1) plt.plot(np.arange(self._epochs) + 1, stat[i, :], '-', label='Fold ' + str(i + 1) + ' Training', color='C' + str(i)) for i in range(self._n_folds): val[i, :] = pd.read_csv(stor_dir + '/' + self._experiment_name + '/validation/val_fold_' + str(i + 1) + '.csv', header=None).values[:, 1] plt.plot(np.arange(self._epochs) + 1, val[i, :], ':', label='Fold ' + str(i + 1) + ' Validation', color='C' + str(i)) plt.legend() plt.title('Statistic Training') plt.ylabel('Loss Per Token') plt.xlabel('Epoch') plt.savefig(self._experiment_name + '/statistic/all_statistic.png') plt.close()
def convert_plots_to_hexstring(figures, eps_vector, titles, filetype='png'): plt.figure(figsize=(8, 8)) plt.title(titles[0]) plt.subplots_adjust(hspace=0.6) for i, (title, plots) in enumerate(figures.items()): i += 1 plt.subplot(3, 1, i) plt.title(plots['layout']['title']) logymin = 10**100 logymax = -200 for plot in plots['traces']: if 'y' in plot: plt.semilogy(plot['x'], plot['y'], linestyle=plot['linestyle'], color=plot['color'], alpha=0.5, label=plot['name']) sanitizedy = plot['y'][np.nonzero(plot['y'])] logymin = min(logymin, np.log10(np.min(sanitizedy))) logymax = max(logymax, np.log10(np.max(sanitizedy))) yticks = np.logspace(logymin, logymax, num=5) plt.yticks(yticks) plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:.2E}')) plt.gca().yaxis.set_minor_formatter(NullFormatter()) plt.xlabel(plots['layout']['xaxis']['title']) plt.ylabel(plots['layout']['yaxis']['title']) plt.legend() with io.BytesIO() as f: plt.savefig(f, format=filetype, bbox_inches='tight', dpi=200, facecolor=BACKGROUNDCOLOR) f.seek(0) b64string = base64.b64encode(f.read()).decode('utf-8') return 'data:image/' + filetype + ';base64, ' + b64string
def create_graph_bars( datas, title ): # title in arguments because translations doesn't work in a separated thread fig, ax = plt.subplots() langs = datas.keys() values = [len(v) for v in datas.values()] ax.bar(langs, values, color=misc.Color(10, 100, 255, 0.5).mpl, edgecolor=misc.Color(10, 100, 255).mpl, linewidth=5) ax.yaxis.set_major_formatter( StrMethodFormatter('{x:,.0f}')) # No decimal places ax.set_yticks(range(1, max(values) + 1)) ax.set_title(title) buff = io.BytesIO() fig.savefig(buff) buff.seek(0) del fig return buff
def quick_vis(self, ax=None): import matplotlib.pyplot as plt if ax is None: fig, ax = plt.subplots(1, 1) ax.loglog(self.mod_freq, 10**self.mod_amp, color='grey', label=self.sig.id) ax.loglog(self.mod_freq, 10**self.result.best_fit, 'k--', label='model') ax.xaxis.set_major_formatter(StrMethodFormatter('{x:.2f}')) ax.xaxis.set_minor_formatter(NullFormatter()) ax.set_title(self.__param_string()) ax.set_xlabel('freq [Hz]') ax.set_ylabel('spectral amp') ax.legend() if ax is not None: return ax
def setup_ax(ax: np.ndarray, x_label: str, y_label: str, title: str): ax = ax[0] for x in ax: # Despine x.spines['right'].set_visible(False) x.spines['top'].set_visible(False) x.spines['left'].set_visible(False) # Switch off ticks x.tick_params(axis="both", which="both", bottom="off", top="off", labelbottom="on", left="off", right="off", labelleft="on") # Draw horizontal axis lines vals = x.get_yticks() for tick in vals: x.axhline(y=tick, linestyle='dashed', alpha=0.4, color='#eeeeee', zorder=1) # Remove title x.set_title(title) # Set x-axis label x.set_xlabel(x_label, labelpad=20, weight='bold', size=12) # Set y-axis label x.set_ylabel(y_label, labelpad=20, weight='bold', size=12) # Format y-axis label x.yaxis.set_major_formatter(StrMethodFormatter('{x:,g}'))
def makeVis(dbname): #data for plotting output = calcPercChange(dbname) x = output[0] y = output[1] #create line graph fig, ax = plt.subplots() fig.set_facecolor("lavender") ax.plot(x, y, "m.-") ax.set_xlabel(xlabel = "Dates", fontsize = 11) ax.set_ylabel(ylabel = "Percent Change in Positive Covid-19 Cases", fontsize=11) ax.set_title(label="Percent Change in Positive Covid-19 Cases in the United States", fontsize=14) ax.set_facecolor('honeydew') plt.xticks(fontsize=7) plt.yticks(fontsize=9) loc = plticker.MultipleLocator(base=50) # this locator puts ticks at regular intervals ax.xaxis.set_major_locator(loc) plt.gca().invert_xaxis() plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) ax.grid() fig.savefig("covid-cases-chart.png")
def format_ramachandran_axes_degrees(ax): """Format matplotlib axes for a ramachandran plot in degrees. Sets axis limits, labels, and formats tick labels. :param ax: Axes to format. :type ax: matplotlib.axes.Axes """ # Phi and psi labels ax.set_xlabel('$\\phi$') ax.set_ylabel('$\\psi$') # Axis limits ax.set_xlim([-180, 180]) ax.set_ylim([-180, 180]) # Degree sign in tick labels from matplotlib.ticker import StrMethodFormatter formatter = StrMethodFormatter('{x}\u00b0') ax.xaxis.set_major_formatter(formatter) ax.yaxis.set_major_formatter
def labels_vs_cost(result, cantidades, query_conditions): df = pd.DataFrame(data=result).transpose().round(decimals=0) ax = df.plot(rot=20, kind='bar', color=['#AFCB1D', '#00000066', '#545454', '#86C05E', 'black'], width=0.8) print("++++++ ", result) for p in ax.patches: ax.annotate(str(int(p.get_height())), (p.get_x() * 1.005, p.get_height() * 1.005), rotation=45, size=10, fontweight='bold') plt.grid() # pd.DataFrame() # if query_conditions.get('origin') == 'B': # if query_conditions.get('manuforiented') == False: # plt.xlabel('Bought components', fontsize=12) # else: # plt.xlabel('Bought components', fontsize=12) # else: # if query_conditions.get('manuforiented') == True: # plt.xlabel('Manufactured components', fontsize=12) # else: # plt.xlabel('Manufactured components', fontsize=12) plt.ylabel("Unitary cost [€]", fontsize=12) plt.legend(cantidades, title='Mass production\n rate(units)', loc='upper left') plt.ylim(0, 2600) plt.grid(lw=0.1) ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,}')) plt.show()
def generator(): data1 = pickle.load(open("Save/hybrid_losses", "rb")) data2 = pickle.load(open("Save/baseline_losses", "rb")) hybrid_losses = [loss[0] for loss in data1] baseline_losses = data2 t = np.arange(len(data1)) fig, ax = plt.subplots() plt.plot(t, hybrid_losses, label="Hybrid", color=red) plt.plot(t, baseline_losses, label="Baseline", color=blue) ax.set(xlabel="Steps ('000)", ylabel="Validation Loss") ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.2f}')) ax.yaxis.set_minor_formatter(NullFormatter()) axes = plt.gca() axes.set_ylim([1, 6]) yticks(np.arange(1, 6, 2)) # Add horizontal line showing lowest point val = min(hybrid_losses) ax.axhline(y=val, color=red, linestyle="--") trans = transforms.blended_transform_factory( ax.get_yticklabels()[0].get_transform(), ax.transData) ax.text(0,val, "{:.2f}".format(val), color=red, transform=trans, ha="right", va="center") val = min(baseline_losses) ax.axhline(y=val, color=blue, linestyle="--") trans = transforms.blended_transform_factory( ax.get_yticklabels()[0].get_transform(), ax.transData) ax.text(0, val, "{:.2f}".format(val), color=blue, transform=trans, ha="right", va="center") #plt.legend() plt.title("Generation") plt.tight_layout() plt.savefig("Figures/generator_loss.png") plt.show()
def uecGraphClaims(series_id, datum, title): fig = pyplot.figure(figsize=(15, 5), facecolor="lightgrey") fig.suptitle(title, fontsize=12) for i in range(len(datum)): x = datum[i][0] y = datum[i][1] if i == 0: subplot = fig.add_subplot(121) subplot.plot(x, y, 'r') subplot.set_title(label="Total Claims", fontsize=10) subplot.set_xlabel(xlabel="Date", fontsize=10) subplot.set_ylabel(ylabel="Number of Claims", fontsize=10) pyplot.xticks(fontsize=6) pyplot.yticks(fontsize=8) loc = plticker.MultipleLocator( base=12.5) # this locator puts ticks at regular intervals subplot.xaxis.set_major_locator(loc) pyplot.gca().invert_xaxis() pyplot.gca().yaxis.set_major_formatter( StrMethodFormatter('{x:,.0f}')) pyplot.grid() elif i == 1: subplot = fig.add_subplot(122) subplot.plot(x, y, 'b') subplot.set_title(label="Percent Change", fontsize=10) subplot.set_xlabel(xlabel="Date", fontsize=10) subplot.set_ylabel(ylabel="Change by Percent", fontsize=10) pyplot.xticks(fontsize=6) pyplot.yticks(fontsize=8) loc = plticker.MultipleLocator( base=12.5) # this locator puts ticks at regular intervals subplot.xaxis.set_major_locator(loc) pyplot.gca().invert_xaxis() pyplot.grid() pyplot.xticks(fontsize=6) pyplot.draw() # pyplot.show() fig.savefig("unemployment-" + series_id + "-chart.png")
def user_postprocess(self, load): # beta_n = self.stability.eigen from matplotlib.ticker import StrMethodFormatter outdir = self.parameters['outdir'] alpha = self.solver.alpha adm_pert = np.where(np.array([e['en_diff'] for e in stability.eigendata]) < 0)[0] fig = plt.figure(figsize=(4, 1.5), dpi=180,) ax = plt.gca() X =alpha.function_space().tabulate_dof_coordinates() xs = np.linspace(min(X[:, 0]),max(X[:, 0]), 300) ax.plot(xs, [alpha(x, 0) for x in xs], label='$\\alpha$', lw=1, c='k') ax.axhline(0., lw=.5, c='k', ls='-') ax3 = ax.twinx() ax.legend(fontsize='small') # print(stability.eigendata) # import pdb; pdb.set_trace() for mode in adm_pert: beta_n = stability.eigendata[mode]['beta_n'] ax3.plot(xs, [beta_n(x, 0) for x in xs], label='$\\beta_{}$'.format(mode), ls=':') for axi in [ax, ax3]: axi.spines['top'].set_visible(False) axi.spines['bottom'].set_visible(False) ax.get_yaxis().get_major_formatter().set_useOffset(False) ax.set_yticks(np.linspace(0, 1, 3)) ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.1f}')) # 2 decimal places plt.xlabel('$x$') ax.set_ylabel('$\\alpha$') ax3.set_ylabel('$\\beta$') ax.set_ylim(0., 1.) # ax.set_xlim(-.5, .5) ax3.legend(bbox_to_anchor=(0,-.45,1,0.2), loc="lower left", mode="expand", borderaxespad=0, ncol=len(adm_pert), frameon=False) fig.savefig(os.path.join(outdir, "profiles-{:.3f}.pdf".format(load)), bbox_inches="tight")
def plot_timeseries(data): fig, ax = plt.subplots() ax.yaxis.set_major_formatter(StrMethodFormatter("{x:,.0f}")) cases, deaths = _moving_avg(data["cases"]), _moving_avg(data["deaths"]) dates = [data["last_date"] - timedelta(days=i) for i in range(len(cases))][::-1] plt.figure(figsize=(13, 8)) plt.plot(dates, cases, ".-c", label="Infections") plt.fill_between(dates, cases, color="c", alpha=0.5) plt.plot(dates, deaths, ".-r", label="Deaths") plt.fill_between(dates, deaths, color="r", alpha=0.5) plt.annotate(round(cases[-1]), (dates[-1], cases[-1]), ha="right", va="bottom", color="c") plt.annotate(round(deaths[-1]), (dates[-1], deaths[-1]), ha="right", va="bottom", color="r") plt.legend() plt.xticks(rotation=30, ha="right") plt.xlim((dates[0], dates[-1])) plt.ylabel("Cases (moving 7-day avg.)") plt.title("New Covid-19 Cases in {} - {} Days".format( data["name"], len(cases))) plt.text(0, 0, "by @coronaviruskenyabot; data by JHUCSSE", fontsize=6, va="bottom", transform=ax.transAxes) plt.tight_layout() buffer = io.BytesIO() plt.savefig(buffer, format="png") buffer.seek(0) plt.clf() return buffer
def _get_formatter(self, locator, formatter, like, base, unit): log_base, symlog_thresh = self._parse_for_log_params(self.trans) if base is None: if symlog_thresh: log_base = 10 base = log_base if formatter is not None: return formatter if like is not None: if isinstance(like, str): if "{x" in like or "{pos" in like: fmt = like else: fmt = f"{{x:{like}}}" formatter = StrMethodFormatter(fmt) else: formatter = FuncFormatter(like) elif base is not None: # We could add other log options if necessary formatter = LogFormatterSciNotation(base) elif unit is not None: if isinstance(unit, tuple): sep, unit = unit else: sep = " " formatter = EngFormatter(unit, sep=sep) else: formatter = ScalarFormatter() return formatter
def plot_cumulative_excess(start, end, countries, age='total', ax=None, **kwargs): start, end = str(start), str(end) names = [] vals = [] for country in countries: names.append(country.country) s = country.excess_deaths_daily.loc[start:end, age] vals.append(s) df = pd.concat(vals, axis=1, keys=names).cumsum() if ax is None: fig, ax = plt.subplots(**kwargs) df.plot(legend=False, ax=ax) add_line_name(names, ax) ax.axhline(color="black", ls="--") year_string = f"{end}" if start == end else f"{start}-{end}" ax.set_title(f"{year_string} Cumulative Excess Deaths", size=10) ax.text( 0.1, 0.02, "Created by Teddy Petrou (@TedPetrou)", color="black", size=8, ha="left", transform=ax.transAxes, style="italic", ) ax.yaxis.set_major_formatter(StrMethodFormatter("{x:,.0f}")) return df
def create_graph_bars(datas, translated_texts: dict): # title in arguments because translations doesn't work in a separated thread ax: plt.Axes fig, ax = plt.subplots() languages = list(datas.keys()) number_per_language = [len(v) for v in datas.values()] lengths_per_language = [sorted([i[2] for i in v]) for v in datas.values()] cmap = mcolors.LinearSegmentedColormap.from_list("", ["red", "yellow", "green"]) lengths: plt.Axes = ax.twinx() ax.bar(range(1, len(languages)+1), number_per_language, color=cmap([i/max(number_per_language) for i in number_per_language])) lengths.boxplot(lengths_per_language, medianprops=dict(color='black'), labels=languages) # lengths.bar(langs, max_length, bottom=min_length, width=0.1, color='black') ax.yaxis.set_major_formatter(StrMethodFormatter('{x:,.0f}')) # No decimal places ax.set_yticks(range(1, max(number_per_language) + 1)) for label in ax.get_xticklabels(): label.set_ha("right") label.set_rotation(45) ax.set_title(translated_texts['title']) ax.set_ylabel(translated_texts['nb_parts']) lengths.set_ylabel(translated_texts['chars_parts']) buff = io.BytesIO() fig.savefig(buff) buff.seek(0) plt.close(fig) return buff
def box_plot_maker(a, yvals, y_labels, col_idx, colors, y_axis_label, title, logplot, LABEL_FONTSIZE, TITLE_FONTSIZE, TICK_FONTSIZE): data = [Y[:, -1] for Y in yvals] medianprops = dict(linestyle='-', color='black') bplot = a.boxplot(data, patch_artist=True, medianprops=medianprops) if y_labels is not None: a.set_xticklabels(y_labels, rotation=90) for patch, c in zip(bplot['boxes'], col_idx): patch.set(facecolor=colors[c]) a.set_ylabel(y_axis_label, fontsize=LABEL_FONTSIZE) a.set_title(title, fontsize=TITLE_FONTSIZE) if logplot: a.semilogy() else: a.yaxis.set_major_formatter(StrMethodFormatter('{x: >4.1f}')) a.tick_params(axis='both', which='major', labelsize=TICK_FONTSIZE) a.tick_params(axis='both', which='minor', labelsize=TICK_FONTSIZE)
name = variables_names.get(ss) error = np.linalg.norm( estimated_variables[ss, :] - test_input_cube[ss, :], 2) relative_error = 100 * error / np.linalg.norm(test_input_cube, 2) log_string = "Variable series {}, L2 error evaluation: {}".format( name, relative_error) print(log_string) fp.writelines(log_string + '\n') #plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) plt.autoscale(enable='False') plt.plot(Time, test_input_cube[:, ss], label="Target") plt.plot(Time, estimated_variables[:, ss], label="Estimated") plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:,.5f}')) plt.legend() plt.grid(True) plt.title("Variable {}".format(name)) plt.ylabel(name) plt.xlabel("t(s)") plt.savefig(data_path + '_' + case + '_variable_series_{}.png'.format(name)) plt.show() plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0)) plt.plot(Time, test_input_cube[:, ss] - estimated_variables[::interval, ss]) plt.grid(True) plt.title("Pointwise error, variable {}".format(name))
def percent_references_uva_authors(): """Percent UVA references by provider by year, referencing Scopus data. Looks at columns under 'References to journal/provider by your institution's authors (as measured in Scopus) References are defined as: Number of References made by researchers of your institution to an article from a given journal ex: (Elsevier UVA references 2008 / total references 2008) """ data = pd.read_csv('JournalsPerProvider.csv', skiprows=8) providers = [ 'Wiley', 'U Chicago Press', 'Taylor & Francis', 'Springer', 'Sage', 'SPIE', 'Royal Society of Chemistry', 'Project MUSE', 'ProQuest', 'Oxford UP', 'Ovid', 'Modern Language Association', 'MIT Press', 'Karger', 'JSTOR', 'IOPscience', 'IEEE', 'Gale', 'Emerald', 'Elsevier', 'Ebsco', 'DeGruyter', 'Cambridge UP', 'Brill', 'BioOne', 'Association for Computing Machinery', 'Annual Reviews', 'American Society of Mechanical Engineers', 'American Society of Civil Engineers', 'American Physical Society', 'American Mathematical Society', 'American Institute of Aeronautics and Astronautics', 'American Chemical Society', 'AIP' ] big5 = ['Elsevier', 'Sage', 'Springer', 'Taylor & Francis', 'Wiley'] sum_2008 = 0 sum_2009 = 0 sum_2010 = 0 sum_2011 = 0 sum_2012 = 0 sum_2013 = 0 sum_2014 = 0 sum_2015 = 0 sum_2016 = 0 sum_2017 = 0 for provider_name in providers: subset_by_provider = data.loc[data['Provider'] == provider_name] ref_2008 = subset_by_provider.ref_2008.tolist() sum_2008 += ref_2008[0] ref_2009 = subset_by_provider.ref_2009.tolist() sum_2009 += ref_2009[0] ref_2010 = subset_by_provider.ref_2010.tolist() sum_2010 += ref_2010[0] ref_2011 = subset_by_provider.ref_2011.tolist() sum_2011 += ref_2011[0] ref_2012 = subset_by_provider.ref_2012.tolist() sum_2012 += ref_2012[0] ref_2013 = subset_by_provider.ref_2013.tolist() sum_2013 += ref_2013[0] ref_2014 = subset_by_provider.ref_2014.tolist() sum_2014 += ref_2014[0] ref_2015 = subset_by_provider.ref_2015.tolist() sum_2015 += ref_2015[0] ref_2016 = subset_by_provider.ref_2016.tolist() sum_2016 += ref_2016[0] ref_2017 = subset_by_provider.ref_2017.tolist() sum_2017 += ref_2017[0] totals_by_year = list( (sum_2008, sum_2009, sum_2010, sum_2011, sum_2012, sum_2013, sum_2014, sum_2015, sum_2016, sum_2017)) #list of total references by year ref_by_provider = [] for provider_name in big5: subset_by_provider = data.loc[data['Provider'] == provider_name] ref_by_year = [] ref_2008 = subset_by_provider.ref_2008.tolist() ref_by_year.append(ref_2008[0]) ref_2009 = subset_by_provider.ref_2009.tolist() ref_by_year.append(ref_2009[0]) ref_2010 = subset_by_provider.ref_2010.tolist() ref_by_year.append(ref_2010[0]) ref_2011 = subset_by_provider.ref_2011.tolist() ref_by_year.append(ref_2011[0]) ref_2012 = subset_by_provider.ref_2012.tolist() ref_by_year.append(ref_2012[0]) ref_2013 = subset_by_provider.ref_2013.tolist() ref_by_year.append(ref_2013[0]) ref_2014 = subset_by_provider.ref_2014.tolist() ref_by_year.append(ref_2014[0]) ref_2015 = subset_by_provider.ref_2015.tolist() ref_by_year.append(ref_2015[0]) ref_2016 = subset_by_provider.ref_2016.tolist() ref_by_year.append(ref_2016[0]) ref_2017 = subset_by_provider.ref_2017.tolist() ref_by_year.append(ref_2017[0]) ref_by_provider.append(ref_by_year) all_percent_by_provider_of_total_by_year = [ ] #for each of big 5 providers, stores list of % of UVA references each provider made up of ALL UVA references for that year for i in ref_by_provider: individual_percent_total_by_provider_per_year = [] zipped = zip(totals_by_year, i) for i in zipped: individual_percent_total_by_provider_per_year.append(i[1] / i[0]) all_percent_by_provider_of_total_by_year.append( individual_percent_total_by_provider_per_year) years = [ '2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017' ] plt.figure(num=None, figsize=(10, 10)) plt.suptitle( f'Percent of References by UVA Authors by Provider \n (as percent of all UVA references by year)' ) plt.xlabel('Year') plt.ylabel('Percent of References') plt.gca().yaxis.set_major_formatter( StrMethodFormatter('{x:,.0%}')) #formats y axis as % plt.plot(years, all_percent_by_provider_of_total_by_year[0], label='Elsevier') plt.plot(years, all_percent_by_provider_of_total_by_year[1], label='Sage') plt.plot(years, all_percent_by_provider_of_total_by_year[2], label='Springer') plt.plot(years, all_percent_by_provider_of_total_by_year[3], label='Taylor & Francis') plt.plot(years, all_percent_by_provider_of_total_by_year[4], label='Wiley') plt.legend() plt.savefig('test.jpg') #saves output to working directory
def manufactured_vs_bought(units, result_bought, result_manufactured,mod): # Calcular el total de precios, manufacturados y comprados # result_total alamacenará Manufactured + Bought result_total = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0} for attr, value in result_bought.get('B').items(): result_total[attr] += value for attr, value in result_manufactured.get('M').items(): result_total[attr] += value percentage_bought = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0} percentage_manufactured = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0} for attr, value in result_total.items(): percentage_bought[attr] = int(round(result_bought.get('B')[attr] / result_total[attr],2)*100) percentage_manufactured[attr] = int(round(result_manufactured.get('M')[attr] / result_total[attr],2)*100) print(result_bought.get('B')['a'] / result_total['a']) print(percentage_manufactured) print(percentage_bought) color = ['#AFCB1D', '#00000066'] nombres = ['Bought components', 'Manufactured components'] fig, ax = plt.subplots() fig.set_size_inches(7.38, 6.13) if units != 'ALL': plt.pie([percentage_bought[units], percentage_manufactured[units]], colors=color, labels=nombres, autopct="%d %%" , explode=[0, 0.05]) else: ax1 = plt.subplot2grid(shape=(2, 6), loc=(0, 0), colspan=2) ax2 = plt.subplot2grid((2, 6), (0, 2), colspan=2) ax3 = plt.subplot2grid((2, 6), (0, 4), colspan=2) ax4 = plt.subplot2grid((2, 6), (1, 1), colspan=2) ax5 = plt.subplot2grid((2, 6), (1, 3), colspan=2) ax1.pie([percentage_bought['a'], percentage_manufactured['a']], colors=color, autopct="%d %%", explode=[0, 0.05]) ax1.set_title("Prototype", fontsize=16, fontstyle='italic') ax2.pie([percentage_bought['b'], percentage_manufactured['b']], colors=color, autopct="%d %%", explode=[0, 0.05]) ax2.set_title("100 units", fontsize=16, fontstyle='italic') ax3.pie([percentage_bought['c'], percentage_manufactured['c']], colors=color, autopct="%d %%", explode=[0, 0.05]) ax3.set_title("1,000 units", fontsize=16, fontstyle='italic') ax4.pie([percentage_bought['d'], percentage_manufactured['d']], colors=color, autopct="%d %%", explode=[0, 0.05]) ax4.set_title("10,000 units", fontsize=16, fontstyle='italic') ax5.pie([percentage_bought['e'], percentage_manufactured['e']], colors=color, autopct="%d %%" , explode=[0, 0.05]) ax5.set_title("50,000 units", fontsize=16, fontstyle='italic') ax5.yaxis.set_major_formatter(StrMethodFormatter('{x:,}')) plt.legend(bbox_to_anchor=(1.1, 1.05), labels=nombres) plt.show() if mod == str(True): fig.savefig('Tarta_Si_MOD.png', bbox_inches="tight") if mod == str(False): fig.savefig('Tarta_No_MOD.png', bbox_inches="tight")
def _plot_helper( df: pd.DataFrame, *, stage: Union[DiseaseStage, Literal[Select.ALL]], count: Counting, x_axis: Columns.XAxis, style: Optional[str] = None, color_mapping: LocationColorMapping = None, plot_size: Tuple[float] = None, savefile_path: Path, location_heading: str = None, ) -> List[Tuple[plt.Figure, plt.Axes]]: """Do all the logic required to turn the arguments given into the correct plot :param df: The dataframe whose data will be plotted :type df: pd.DataFrame :param x_axis: The x axis column the data will be plotted against :type x_axis: Columns.XAxis :param stage: The disease stage (one half of the y-value) that will be plotted; :type stage: Union[DiseaseStage, Literal[Select.ALL]] :param count: The counting method (the other half of the y-value) that will be plotted :type count: Counting :param savefile_path: Where to save the resulting figure :type savefile_path: Path :param style: The matplotlib plotting style to use, defaults to None :type style: Optional[str], optional :param color_mapping: The seaborn color mapping to use, defaults to None :type color_mapping: LocationColorMapping, optional :param plot_size: The (width inches, height inches) plot size, defaults to None :type plot_size: Tuple[float], optional :param location_heading: What locations are to be called ("Country", "State", etc.); the default of None is equivalent to "Location" :type location_heading: str, optional :return: A list of figures and axes created :rtype: List[Tuple[plt.Figure, plt.Axes]] """ DiseaseStage.verify(stage, allow_select=True) Counting.verify(count) Columns.XAxis.verify(x_axis) SORTED_POSITION = "Sorted_Position_" figs_and_axes = [] if plot_size is None: plot_size = (10, 12) fig, ax = plt.subplots(figsize=(8, 8), dpi=200, facecolor="white") fig: plt.Figure ax: plt.Axes if stage is Select.ALL: current_case_counts = get_current_case_data(df, stage=Select.DEFAULT, count=count, x_axis=x_axis) else: current_case_counts = get_current_case_data(df, stage=stage, count=count, x_axis=x_axis) df = df[df[Columns.CASE_TYPE].isin( CaseInfo.get_info_items_for(InfoField.CASE_TYPE, stage=stage, count=count).values)] # Filter and sort color mapping correctly so that colors 1. are assigned to the # same locations across graphs (for continuity) and 2. are placed correctly in the # legend (for correctness) color_mapping = color_mapping.copy() color_mapping = color_mapping[color_mapping[Columns.LOCATION_NAME].isin( current_case_counts[Columns.LOCATION_NAME])] color_mapping[SORTED_POSITION] = color_mapping[Columns.LOCATION_NAME].map( current_case_counts[Columns.LOCATION_NAME].tolist().index) color_mapping = color_mapping.sort_values(SORTED_POSITION) config_df = CaseInfo.get_info_items_for(InfoField.CASE_TYPE, InfoField.DASH_STYLE, stage=stage, count=count) with plt.style.context(style or "default"): g = sns.lineplot( data=df, x=x_axis.column(), y=Columns.CASE_COUNT, hue=Columns.LOCATION_NAME, hue_order=color_mapping[Columns.LOCATION_NAME].tolist(), style=Columns.CASE_TYPE, style_order=config_df[InfoField.CASE_TYPE].tolist(), dashes=config_df[InfoField.DASH_STYLE].tolist(), palette=color_mapping[COLOR].tolist(), ) default_stage = stage if stage is Select.ALL: default_stage = DiseaseStage.CONFIRMED # Configure axes and ticks # X axis (and y axis bottom limit, which is kind of x-axis related) if x_axis is Columns.XAxis.DATE: ax.xaxis.set_major_formatter(DateFormatter(r"%b %-d")) ax.xaxis.set_minor_locator(DayLocator()) for tick in ax.get_xticklabels(): tick.set_rotation(80) elif x_axis is Columns.XAxis.DAYS_SINCE_OUTBREAK: ax.xaxis.set_major_locator(MultipleLocator(5)) ax.xaxis.set_minor_locator(MultipleLocator(1)) _threshold, _axis_name = CaseInfo.get_info_items_for( InfoField.THRESHOLD, InfoField.CASE_TYPE, stage=default_stage, count=count, squeeze_rows=True, ).values ax.set_xlabel(f"Days Since Reaching {_threshold:.3g} {_axis_name}") if stage is not Select.ALL: # i.e. if only one DiseaseStage plotted ax.set_ylim(bottom=_threshold / 4) else: x_axis.raise_for_unhandled_case() # Y axis ax.set_ylabel( CaseInfo.get_info_item_for(InfoField.CASE_TYPE, stage=default_stage, count=count)) if count is Counting.TOTAL_CASES: ax.set_yscale("log", basey=2, nonposy="mask") ax.yaxis.set_major_locator(LogLocator(base=2, numticks=1000)) # ax.yaxis.set_major_formatter(ScalarFormatter()) ax.yaxis.set_major_formatter(StrMethodFormatter("{x:,.0f}")) ax.yaxis.set_minor_locator( # 5 ticks is one full "cycle": n, 1.25n, 1.5n, 1.75n, 2n # Hence 5-2 minor ticks between each pair of majors (omit endpoints) LogLocator(base=2, subs=np.linspace(0.5, 1, 5)[1:-1], numticks=1000)) ax.yaxis.set_minor_formatter(NullFormatter()) elif count is Counting.PER_CAPITA: ax.set_yscale("log", basey=10, nonposy="mask") # No need to set minor ticks; 8 is the default number, which makes one cycle # n, 2n, 3n, ..., 8n, 9n, 10n else: count.raise_for_unhandled_case() # Configure plot design now_str = datetime.now( timezone.utc).strftime(r"%b %-d, %Y at %H:%M UTC") ax.set_title(f"Last updated {now_str}", loc="right", fontsize="small") for line in g.lines: line.set_linewidth(2) ax.grid(True, which="minor", axis="both", color="0.9") ax.grid(True, which="major", axis="both", color="0.75") _format_legend( ax=ax, x_axis=x_axis, count=count, location_heading=location_heading, current_case_counts=current_case_counts, ) # If using this for a date-like x axis, use this (leaving commented code because # I foresee myself needing it eventually) # x_max = pd.Timestamp(matplotlib.dates.num2epoch(ax.get_xlim()[1]), unit="s") # Add doubling time lines _add_doubling_time_lines(fig, ax, x_axis=x_axis, stage=default_stage, count=count) # Save savefile_path = Paths.FIGURES / savefile_path savefile_path.parent.mkdir(parents=True, exist_ok=True) fig.savefig(savefile_path, bbox_inches="tight", dpi=300) print(f"Saved '{savefile_path.relative_to(Paths.ROOT)}'") figs_and_axes.append((fig, ax)) return figs_and_axes