我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用seaborn.FacetGrid()。
def create_plot(json_data, output): all_data = pd.DataFrame(json_data) df = all_data[all_data['ProductDescription'] == 'Linux/UNIX'] df = df.drop_duplicates(subset=['DateTime', 'AvailabilityZone', 'InstanceType']) x_min = df['DateTime'].min() x_max = df['DateTime'].max() border_pad = (x_max - x_min) * 5 / 100 g = sns.FacetGrid( df, col='InstanceType', hue='AvailabilityZone', xlim=(x_min - border_pad, x_max + border_pad), legend_out=True, size=10, palette="Set1" ) g.map(plt.scatter, 'DateTime', 'SpotPrice', s=4).add_legend() plt.subplots_adjust(top=.9) g.fig.suptitle('AWS Spot Prices between {start} and {end}'.format(start=x_min, end=x_max)) g.savefig(output, format='png')
def map_data(self, func): """ Map the dataframe using :func:`func`. This method wraps the function :func:`func` so that a facet is plotted for the grouping variables. In order for this to work, :func:`func` has to take two values: `data`, which is a sub- dataframe after grouping, and `color`, which is currently not used, but which must be handled by `func` anyway. Technically, it calls :func:`FacetGrid.map_dataframe` from `seaborn` with `func` as a parameter if more than one plot is required. Otherwise, it calls `func` directly, as `FacetGrid` can have problems if only one plot is drawn. Parameters ---------- func : function The plotting function. """ if self._col_factor: self.g.map_dataframe(func) else: func(self._table, None)
def adc_attack_speed(): m_data = [champion_full.get_item_with_key(n) for n in _Marksman] champion_objects = [Champion(c['name'], **c['stats']) for c in m_data] data_as = {'champion': [], 'attack speed': [], 'level': []} for c in champion_objects: for i in range(1, 19): c.update_lv(i) data_as['champion'].append(c.name) data_as['attack speed'].append(c.attackspeed) data_as['level'].append(i) dff = pd.DataFrame(data_as) g = sns.FacetGrid(dff, hue='champion', size=5, aspect=1.5) g = g.map(plt.plot, 'level', 'attack speed').add_legend() g.set_axis_labels('??', '??') g.set_titles("??????????") g.set(xticks=np.arange(1, 20, 1)) g.set(yticks=np.arange(0.5, 1.2, 0.05)) sns.plt.show()
def display_covariate_dist(self, covariate_list, save_file=None): ''' ''' n_covars = len(covariate_list) for covariate in covariate_list: g = sns.FacetGrid(self.data, col="arm_assignment") if len(self.data[covariate].unique())>2: g.map(sns.distplot, covariate, kde=False) else: g.map(sns.distplot, covariate, kde=False) if save_file: g.savefig(save_file, dpi=450) if save_file is None: sns.plt.show()
def plot_polar(theta, N): """Plot polar plot. theta -- the dataframe with the turning angles N -- number of bins to use """ hist, bins = np.histogram(theta.ta, bins=N) # the width of the bins interval width = [t - s for s, t in zip(bins, bins[1:])] bins_ = bins[0:N] # exclude the last value # the actual plotting logic g = sns.FacetGrid(theta, size=4) radii = hist / max(hist) for ax in g.axes.flat: ax2 = plt.subplot(111, projection='polar') bars = ax2.bar(bins_, radii, width, bottom=0.0) for r, bar in zip(radii, bars): bar.set_facecolor(plt.cm.Spectral(r)) bar.set_alpha(0.5) sns.plt.savefig("turning_angles.png")
def plot_llk(train_elbo, test_elbo): import matplotlib.pyplot as plt import numpy as np import scipy as sp import seaborn as sns import pandas as pd plt.figure(figsize=(30, 10)) sns.set_style("whitegrid") data = np.concatenate([np.arange(len(test_elbo))[:, sp.newaxis], -test_elbo[:, sp.newaxis]], axis=1) df = pd.DataFrame(data=data, columns=['Training Epoch', 'Test ELBO']) g = sns.FacetGrid(df, size=10, aspect=1.5) g.map(plt.scatter, "Training Epoch", "Test ELBO") g.map(plt.plot, "Training Epoch", "Test ELBO") plt.savefig('./vae_results/test_elbo_vae.png') plt.close('all')
def plot_actions(cue=0): mpl.rcParams['axes.labelsize'] = 'large' d_map = {3:1, 8:2, 14:3, 23:4} df = pd.read_pickle('data.pkl').reset_index() df = df.loc[df['cue'] == cue] g = sns.FacetGrid(df, col='subject', col_wrap=6, size=1.5, ylim=(0, 5), aspect=1.5) g.map(plt.plot, 'action') g.set(xticks=[], yticks=[0,1,2,3], yticklabels=['3', '8', '14', '23']) g.set(ylim=(-0.5, 4)) g.set_ylabels('choice') g.fig.tight_layout() g.fig.subplots_adjust(top=0.93) subjects = df['subject'].unique() for ax, subject in zip(g.axes, subjects): df_subject = df.loc[df['subject'] == subject] df_subject.reset_index(inplace=True) df_wins = df_subject.loc[df_subject['reward'] > 0] df_lose = df_subject.loc[df_subject['reward'] < 0] pos_win = df_wins.loc[df_wins['subject'] == subject].index pos_lose = df_lose.loc[df_lose['subject'] == subject].index ax.eventplot(pos_win, lineoffsets=3.5, linelength=0.75, linewidths=0.4) ax.eventplot(pos_lose, lineoffsets=3.5, linelength=0.75, color='r', linewidths=0.4) plt.tight_layout() plt.savefig('actions_0.pdf') plt.show() globals().update(locals())
def setup_figure(self): """ Prepare the matplotlib figure for plotting. This method sets the default font, and the overall apearance of the figure. """ if options.cfg.xkcd: fonts = QtGui.QFontDatabase().families() for x in ["Humor Sans", "DigitalStrip", "Comic Sans MS"]: if x in fonts: self.options["figure_font"] = QtGui.QFont(x, pointSize=self.options["figure_font"].pointSize()) break else: for x in ["comic", "cartoon"]: for y in fonts: if x.lower() in y.lower(): self.options["figure_font"] = QtGui.QFont(x, pointSize=self.options["figure_font"].pointSize()) break plt.xkcd() with sns.plotting_context("paper"): self.g = sns.FacetGrid(self._table, col=self._col_factor, col_wrap=self._col_wrap, row=self._row_factor, sharex=True, sharey=True)
def get_grid(self, **kwargs): kwargs["data"] = self.df with sns.axes_style(self.axes_style): with sns.plotting_context(self.plotting_context): grid = sns.FacetGrid(**kwargs) return grid
def plot_lc(lc, metrics=None, outputs=False): lc = pd.melt(lc, id_vars=['split', 'epoch'], var_name='output') if metrics: if not isinstance(metrics, list): metrics = [metrics] tmp = '(%s)' % ('|'.join(metrics)) lc = lc.loc[lc.output.str.contains(tmp)] metrics = lc.output[~lc.output.str.contains('_')].unique() lc['metric'] = '' for metric in metrics: lc.loc[lc.output.str.contains(metric), 'metric'] = metric lc.loc[lc.output == metric, 'output'] = 'mean' lc.output = lc.output.str.replace('_%s' % metric, '') lc.output = lc.output.str.replace('cpg_', '') if outputs: lc = lc.loc[lc.output != 'mean'] else: lc = lc.loc[lc.output == 'mean'] grid = sns.FacetGrid(lc, col='split', row='metric', hue='output', sharey=False, size=3, aspect=1.2, legend_out=True) grid.map(mpl.pyplot.plot, 'epoch', 'value', linewidth=2) grid.set(ylabel='') grid.add_legend() return grid
def plot_stats(stats): stats = stats.sort_values('frac_obs', ascending=False) stats = pd.melt(stats, id_vars=['output'], var_name='metric') # stats = stats.loc[stats.metric.isin(['frac_obs', 'frac_one'])] # stats.metric = stats.metric.str.replace('frac_obs', 'cov') # stats.metric = stats.metric.str.replace('frac_one', 'met') grid = sns.FacetGrid(data=stats, col='metric', sharex=False) grid.map(sns.barplot, 'value', 'output') for ax in grid.axes.ravel(): ax.set(xlabel='', ylabel='') return grid
def errorplot(x, y, minconf, maxconf, **kwargs): ''' e.g. g = sns.FacetGrid(attr, col='run', hue='subj_pos', col_wrap=5) g = g.map(errorplot, 'n_diff_intervening', 'errorprob', 'minconf', 'maxconf').add_legend() ''' plt.errorbar(x, y, yerr=[y - minconf, maxconf - y], fmt='o-', **kwargs)
def plotXY(df, id_, x_coord, y_coord): """Plot the raw trajectories. df -- the trajectories dataframe id_ -- an identifier (for linear connections of objects) x_coord -- the x coordinate y_coord -- the y coordinate """ grid = sns.FacetGrid(df, hue=id_, size=6, palette=sns.color_palette("Set1", 10)) grid.fig.suptitle('XY trajectories') grid.map(plt.plot, x_coord, y_coord, marker=".", ms=0.3) grid.map(plt.scatter, x_coord, y_coord, marker="o", s=20) grid.set_xticklabels(rotation=90) sns.plt.savefig("trajectories.png")
def plot_predictive_baseline(env, samples, stddev_mult=3., title_name=None): # single var regression only samples = samples.squeeze() train_x, train_y = env.get_train_x(), env.get_train_y() test_x, test_y = env.get_test_x(), env.get_test_y() pad_width = test_x.shape[0] - train_x.shape[0] train_x_padded = np.pad(train_x[:, 0], (0, pad_width), 'constant', constant_values=np.nan) train_y_padded = np.pad(train_y[:, 0], (0, pad_width), 'constant', constant_values=np.nan) data = samples df = pd.DataFrame.from_dict({ 'time': test_x[:, 0], 'true_y': test_y[:, 0], 'train_x': train_x_padded, 'train_y': train_y_padded, 'mean': data.mean(axis=0), 'std': stddev_mult * data.std(axis=0), # 'stdn': 2. * (data.std(axis=0) + .5 ** .5), }).reset_index() g = sns.FacetGrid(df, size=9, aspect=1.8) g.map(plt.errorbar, 'time', 'mean', 'std', color=(0.7, 0.1, 0.1, 0.09)) g.map(plt.plot, 'time', 'mean', color='b', lw=1) g.map(plt.plot, 'time', 'true_y', color='r', lw=1) g.map(plt.scatter, 'train_x', 'train_y', color='g', s=20) ax = g.ax ax.set_title('Posterior Predictive Distribution' + (': ' + title_name) if title_name is not None else '') ax.set(xlabel='X', ylabel='Y') ax.set_xlim(env.view_xrange[0], env.view_xrange[1]) ax.set_ylim(env.view_yrange[0], env.view_yrange[1]) legend = ['Prediction mean', 'True f(x)', 'Training data', 'StdDev'] plt.legend(legend) # ax.annotate("MSE: {:.03f}".format(0), xy=(0.1, 0.9), xytext=(0.1, 0.9), xycoords='figure fraction', # textcoords='figure fraction') name = utils.DATA_DIR.replace('/', '-') plt.tight_layout(pad=0.6) utils.save_fig('predictive-distribution-' + name)
def plot_predictive_comparison(env, baseline_samples, target_samples, stddev_mult=3., target_metrics=None, title_name=None): # single var regression only baseline_samples = baseline_samples.squeeze() target_samples = target_samples.squeeze() train_x, train_y = env.get_train_x(), env.get_train_y() test_x, test_y = env.get_test_x(), env.get_test_y() pad_width = test_x.shape[0] - train_x.shape[0] train_x_padded = np.pad(train_x[:, 0], (0, pad_width), 'constant', constant_values=np.nan) train_y_padded = np.pad(train_y[:, 0], (0, pad_width), 'constant', constant_values=np.nan) df = pd.DataFrame.from_dict({ 'time': test_x[:, 0], 'true_y': test_y[:, 0], 'train_x': train_x_padded, 'train_y': train_y_padded, 'mean': target_samples.mean(axis=0), 'std': stddev_mult * target_samples.std(axis=0), 'base_mean': baseline_samples.mean(axis=0), 'base_std': stddev_mult * baseline_samples.std(axis=0), }).reset_index() g = sns.FacetGrid(df, size=9, aspect=1.8) g.map(plt.errorbar, 'time', 'base_mean', 'base_std', color=(0.7, 0.1, 0.1, 0.09)) g.map(plt.errorbar, 'time', 'mean', 'std', color=(0.1, 0.1, 0.7, 0.09)) g.map(plt.plot, 'time', 'mean', color='b', lw=1) g.map(plt.plot, 'time', 'true_y', color='r', lw=1) g.map(plt.scatter, 'train_x', 'train_y', color='g', s=20) ax = g.ax ax.set_title('Posterior Predictive Distribution' + (': ' + title_name) if title_name is not None else '') ax.set(xlabel='X', ylabel='Y') ax.set_xlim(env.view_xrange[0], env.view_xrange[1]) ax.set_ylim(env.view_yrange[0], env.view_yrange[1]) legend = ['Prediction mean', 'True f(x)', 'Training data', 'True StdDev', 'Predicted StdDev'] plt.legend(legend) if target_metrics is not None: offset = 0 for tm, tv in target_metrics.items(): ax.annotate(f'{tm}: {tv:.02f}', xy=(0.08, 0.92 - offset), xytext=(0.08, 0.92 - offset), xycoords='figure fraction', textcoords='figure fraction') offset += 0.04 name = utils.DATA_DIR.replace('/', '-') plt.tight_layout(pad=0.6) utils.save_fig('predictive-distribution-' + name)
def plot_facet_grid(df, target, frow, fcol, tag='eda', directory=None): r"""Plot a Seaborn faceted histogram grid. Parameters ---------- df : pandas.DataFrame The dataframe containing the features. target : str The target variable for contrast. frow : list of str Feature names for the row elements of the grid. fcol : list of str Feature names for the column elements of the grid. tag : str Unique identifier for the plot. directory : str, optional The full specification of the plot location. Returns ------- None : None. References ---------- http://seaborn.pydata.org/generated/seaborn.FacetGrid.html """ logger.info("Generating Facet Grid") # Calculate the number of bins using the Freedman-Diaconis rule. tlen = len(df[target]) tmax = df[target].max() tmin = df[target].min() trange = tmax - tmin iqr = df[target].quantile(Q3) - df[target].quantile(Q1) h = 2 * iqr * (tlen ** (-1/3)) nbins = math.ceil(trange / h) # Generate the pair plot sns.set(style="darkgrid") fg = sns.FacetGrid(df, row=frow, col=fcol, margin_titles=True) bins = np.linspace(tmin, tmax, nbins) fg.map(plt.hist, target, color="steelblue", bins=bins, lw=0) # Save the plot write_plot('seaborn', fg, 'facet_grid', tag, directory) # # Function plot_distribution #
def plot_alphadf(alphasdf, col_order, labeldict, metric='alpha'): """ Plot faceted alpha diversity. Parameters ---------- alphasdf : pandas DataFrame columns ['study', 'alpha', 'DiseaseState'] col_order : list dataset IDs in the order they should be plotted labeldict : dict dictionary with {dataset: label} mteric : str alpha diversity metric, to use in labeling y axis Returns ------- fig : Figure """ sns.set_style('white') g = sns.FacetGrid(alphasdf, col='study', col_wrap=6, col_order=col_order, sharex=False, sharey=False) g = g.map(sns.boxplot, "DiseaseState", "alpha") g = g.map(sns.stripplot, "DiseaseState", "alpha", split=True, jitter=True, size=5, linewidth=0.6) fig = plt.gcf() fig.set_size_inches(14.2, 9) # Fix y-axis gridlines axs = g.axes for i in range(len(axs)): ax = axs[i] yticks = ax.get_yticks() # If bottom limit is between 0 and 1 (i.e. not simpson) if not (yticks[0] < 1 and yticks[0] > 0): ax.set_ylim(floor(yticks[0]), floor(yticks[-1])) if yticks[0] < 0: ax.set_ylim(0, floor(yticks[-1])) yticks = ax.get_yticks() if (yticks[0] < 1 and yticks[0] > 0): ax.set_yticks(yticks[1::2]) else: ax.set_yticks(yticks[::2]) # Need some space on the y-axis for p-values ax.set_ylim(ax.get_ylim()[0], 1.2*ax.get_ylim()[1]) # Update title oldtitle = ax.get_title() newtitle = labeldict[oldtitle.split('=')[1].strip()] ax.set_title(newtitle) # Update y label if i % 6 == 0: ax.set_ylabel(metric) plt.tight_layout() return fig