def plot_forces_violinplots(experiment):
    ensemble = experiment.observations.kinematics
    ensembleF = ensemble.loc[
        (ensemble['position_x'] > 0.25) & (ensemble['position_x'] < 0.95),
        ['totalF_x', 'totalF_y', 'totalF_z',
         'randomF_x', 'randomF_y', 'randomF_z',
         'upwindF_x',
         'wallRepulsiveF_x', 'wallRepulsiveF_y', 'wallRepulsiveF_z',
         'stimF_x', 'stimF_y', 'stimF_z']] #== Nans
    # plot Forces
    #    f, axes = plt.subplots(2, 2, figsize=(9, 9), sharex=True, sharey=True)
    ##    forcefig = plt.figure(5, figsize=(9, 8))
    ##    gs2 = gridspec.GridSpec(2, 2)
    ##    Faxs = [fig.add_subplot(ss) for ss in gs2]
    forcefig = plt.figure()
    #    Faxs1 = forcefig.add_subplot(211)
    #    Faxs2 = forcefig.add_subplot(212)
    sns.violinplot(ensembleF, lw=3, alpha=0.7, palette="Set2")
    #    tF = sns.jointplot('totalF_x', 'totalF_y', ensemble, kind="hex", size=10)
    plt.suptitle("Force distributions")
    #    plt.xticks(range(4,((len(alignments.keys())+1)*4),4), [i[1] for i in medians_sgc], rotation=90, fontsize = 4)
    plt.tick_params(axis='x', pad=4)
    plt.xticks(rotation=40)
    #    remove_border()
    plt.tight_layout(pad=1.8)
    plt.ylabel("Force magnitude distribution (newtons)")

    fileappend, path, agent = get_agent_info(experiment.agent)

    plt.savefig(os.path.join(path, "Force Distributions" + fileappend + FIG_FORMAT))
    plt.show()
Exemple #2
0
 def _update_plot(self, axis, view):
     if self.plot_type == 'regplot':
         sns.regplot(x=view.x,
                     y=view.y,
                     data=view.data,
                     ax=axis,
                     **self.style)
     elif self.plot_type == 'boxplot':
         self.style.pop('return_type', None)
         self.style.pop('figsize', None)
         sns.boxplot(view.data[view.y],
                     view.data[view.x],
                     ax=axis,
                     **self.style)
     elif self.plot_type == 'violinplot':
         sns.violinplot(view.data[view.y],
                        view.data[view.x],
                        ax=axis,
                        **self.style)
     elif self.plot_type == 'interact':
         sns.interactplot(view.x,
                          view.x2,
                          view.y,
                          data=view.data,
                          ax=axis,
                          **self.style)
     elif self.plot_type == 'corrplot':
         sns.corrplot(view.data, ax=axis, **self.style)
     elif self.plot_type == 'lmplot':
         sns.lmplot(x=view.x,
                    y=view.y,
                    data=view.data,
                    ax=axis,
                    **self.style)
     elif self.plot_type in ['pairplot', 'pairgrid', 'facetgrid']:
         map_opts = [(k, self.style.pop(k)) for k in self.style.keys()
                     if 'map' in k]
         if self.plot_type == 'pairplot':
             g = sns.pairplot(view.data, **self.style)
         elif self.plot_type == 'pairgrid':
             g = sns.PairGrid(view.data, **self.style)
         elif self.plot_type == 'facetgrid':
             g = sns.FacetGrid(view.data, **self.style)
         for opt, args in map_opts:
             plot_fn = getattr(sns, args[0]) if hasattr(
                 sns, args[0]) else getattr(plt, args[0])
             getattr(g, opt)(plot_fn, *args[1:])
         plt.close(self.handles['fig'])
         self.handles['fig'] = plt.gcf()
     else:
         super(SNSFramePlot, self)._update_plot(axis, view)
Exemple #3
0
 def _update_plot(self, axis, view):
     style = self._process_style(self.style[self.cyclic_index])
     if self.plot_type == 'factorplot':
         opts = dict(style, **({'hue': view.x2} if view.x2 else {}))
         sns.factorplot(x=view.x, y=view.y, data=view.data, **opts)
     elif self.plot_type == 'regplot':
         sns.regplot(x=view.x, y=view.y, data=view.data, ax=axis, **style)
     elif self.plot_type == 'boxplot':
         style.pop('return_type', None)
         style.pop('figsize', None)
         sns.boxplot(view.data[view.y], view.data[view.x], ax=axis, **style)
     elif self.plot_type == 'violinplot':
         if view.x:
             sns.violinplot(view.data[view.y],
                            view.data[view.x],
                            ax=axis,
                            **style)
         else:
             sns.violinplot(view.data, ax=axis, **style)
     elif self.plot_type == 'interact':
         sns.interactplot(view.x,
                          view.x2,
                          view.y,
                          data=view.data,
                          ax=axis,
                          **style)
     elif self.plot_type == 'corrplot':
         sns.corrplot(view.data, ax=axis, **style)
     elif self.plot_type == 'lmplot':
         sns.lmplot(x=view.x, y=view.y, data=view.data, ax=axis, **style)
     elif self.plot_type in ['pairplot', 'pairgrid', 'facetgrid']:
         style_keys = list(style.keys())
         map_opts = [(k, style.pop(k)) for k in style_keys if 'map' in k]
         if self.plot_type == 'pairplot':
             g = sns.pairplot(view.data, **style)
         elif self.plot_type == 'pairgrid':
             g = sns.PairGrid(view.data, **style)
         elif self.plot_type == 'facetgrid':
             g = sns.FacetGrid(view.data, **style)
         for opt, args in map_opts:
             plot_fn = getattr(sns, args[0]) if hasattr(
                 sns, args[0]) else getattr(plt, args[0])
             getattr(g, opt)(plot_fn, *args[1:])
         plt.close(self.handles['fig'])
         self.handles['fig'] = plt.gcf()
     else:
         super(SNSFramePlot, self)._update_plot(axis, view)
Exemple #4
0
 def _update_plot(self, axis, view):
     style = self._process_style(self.style[self.cyclic_index])
     if self.plot_type == 'factorplot':
         opts = dict(style, **({'hue': view.x2} if view.x2 else {}))
         sns.factorplot(x=view.x, y=view.y, data=view.data, **opts)
     elif self.plot_type == 'regplot':
         sns.regplot(x=view.x, y=view.y, data=view.data,
                     ax=axis, **style)
     elif self.plot_type == 'boxplot':
         style.pop('return_type', None)
         style.pop('figsize', None)
         sns.boxplot(view.data[view.y], view.data[view.x], ax=axis,
                     **style)
     elif self.plot_type == 'violinplot':
         if view.x:
             sns.violinplot(view.data[view.y], view.data[view.x], ax=axis,
                            **style)
         else:
             sns.violinplot(view.data, ax=axis, **style)
     elif self.plot_type == 'interact':
         sns.interactplot(view.x, view.x2, view.y,
                          data=view.data, ax=axis, **style)
     elif self.plot_type == 'corrplot':
         sns.corrplot(view.data, ax=axis, **style)
     elif self.plot_type == 'lmplot':
         sns.lmplot(x=view.x, y=view.y, data=view.data,
                    ax=axis, **style)
     elif self.plot_type in ['pairplot', 'pairgrid', 'facetgrid']:
         style_keys = list(style.keys())
         map_opts = [(k, style.pop(k)) for k in style_keys if 'map' in k]
         if self.plot_type == 'pairplot':
             g = sns.pairplot(view.data, **style)
         elif self.plot_type == 'pairgrid':
             g = sns.PairGrid(view.data, **style)
         elif self.plot_type == 'facetgrid':
             g = sns.FacetGrid(view.data, **style)
         for opt, args in map_opts:
             plot_fn = getattr(sns, args[0]) if hasattr(sns, args[0]) else getattr(plt, args[0])
             getattr(g, opt)(plot_fn, *args[1:])
         if self._close_figures:
             plt.close(self.handles['fig'])
         self.handles['fig'] = plt.gcf()
     else:
         super(SNSFramePlot, self)._update_plot(axis, view)
Exemple #5
0
    def violinplot(self, x=None, y=None, hue=None, data=None, *args, **kwargs):
        """
        Draw a violin plot to show distributions with respect to categories

        Parameters
        ----------
        x : the name of a variable in data that provides labels for categories

        y : a list of names of variables in data that needs to visualize \
            distribution

        hue : the name of a variable in data that provides labels for \
            sub-categories in each big category

        data : pandas dataframe

        **kwargs : other arguments in seaborn.violinplot

            order, hue_order : lists of strings, optional

            bw : {'scott', 'silverman', float}, optional

            cut : float, optional

            scale : {'area', 'count', 'width'}, optional

            scale_hue : bool, optional

            gridsize : int, optional

            width : float, optional

            inner : {'box', 'quartile', 'point', 'stick', None}, optional

            split : bool, optional

            dodge : bool, optional

            orient : 'v' | 'h', optional

            linewidth : float, optional

            color : matplotlib color, optional

            palette : palette name, list, or dict, optional

            saturation : float, optional

        Returns
        -------
        figure : matplotlib figure with multiple axes

        References
        ----------
        Seaborn violinplot further documentation
        https://seaborn.pydata.org/generated/seaborn.violinplot.html
        """
        # check data
        if not isinstance(data, (pd.DataFrame)):
            raise ValueError('data must be pandas dataframe')

        # check x and hue
        if x is not None:
            if x not in data.columns.values:
                raise ValueError('{} is NOT in data'.format(x))
        if hue is not None:
            if hue not in data.columns.values:
                raise ValueError('{} is NOT in data'.format(hue))

        # handle single string
        if not isinstance(y, (list, tuple, np.ndarray, pd.Index)):
            y = [y]

        # create fig and axes
        nrows = len(y)
        plt.close()
        fig, axes = plt.subplots(nrows=nrows,
                                 ncols=1,
                                 sharex=self.sharex,
                                 figsize=(self.size[0], nrows * self.size[1]))
        # HACK: handle Axes indexing when only one ax in fig
        if nrows == 1:
            axes = [axes]
        # iterate thru x
        for i, col in enumerate(y):
            # check if col in data
            if col not in data.columns.values:
                raise ValueError('{} is NOT in data'.format(col))
            a = data[col]
            not_nan = np.ones(a.shape[0], dtype=np.bool)
            if np.logical_not(np.isfinite(a)).any():
                logger.warning('RUNTIME WARNING: {} column has inf or nan '
                               ''.format(col))
                a = a.replace([-np.inf, np.inf], np.nan)
                # filter
                not_nan = np.logical_not(a.isnull())
            # plot
            sns.violinplot(x=x,
                           y=col,
                           hue=hue,
                           data=data[not_nan],
                           ax=axes[i],
                           *args,
                           **kwargs)
            if x is not None:
                axes[i].set_title(
                    label='Violin Distribution of {} With Respect To {} '
                    ''.format(col, x),
                    fontsize=self.title_fontsize)
                axes[i].set_xlabel(xlabel=x, fontsize=self.label_fontsize)
                axes[i].set_ylabel(ylabel=col, fontsize=self.label_fontsize)
            else:  # x is None
                axes[i].set_title(
                    label='Violin Distribution of {}'.format(col),
                    fontsize=self.title_fontsize)
                axes[i].set_xlabel(xlabel=col, fontsize=self.label_fontsize)
                axes[i].set_ylabel(ylabel='value',
                                   fontsize=self.label_fontsize)
            axes[i].tick_params(axis='both',
                                which='maj',
                                labelsize=self.tick_fontsize)
            axes[i].legend(loc='lower right')
            fig.subplots_adjust(wspace=0.5,
                                hspace=0.3,
                                left=0.125,
                                right=0.9,
                                top=0.9,
                                bottom=0.1)
            fig.tight_layout()
        plt.show()
        return axes
Exemple #6
0
# Box plot survived x age
fig.add_subplot(3, 3, 7)
#sns.swarmplot(x="Survived", y="Age", hue="Sex", alpha=.7, data=df)
sns.distplot(df[df.Survived == 0]['Age'].dropna(),
             bins=20,
             kde_kws={"lw": 2},
             hist_kws={"alpha": .4},
             label='0')
sns.distplot(df[df.Survived == 1]['Age'].dropna(),
             bins=20,
             kde_kws={"lw": 2},
             hist_kws={"alpha": .4},
             label='1')
plt.legend()
fig.add_subplot(3, 3, 8)
sns.violinplot(x="Survived", y="Age", data=df, alpha=.7)

# fare and Survived
fig.add_subplot(3, 3, 9)
sns.violinplot(x="Survived", y="Fare", alpha=.7, data=df, saturation=.7)

plt.show()

# In[425]:

cm = df.drop(['PassengerId'], axis=1).corr()
mask = np.zeros_like(cm, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
plt.figure(figsize=(16, 8))
hm = sns.heatmap(cm,
                 mask=mask,
    matching_df['y_pred'],
    bins=np.arange(0, .9, .2)
)

bins = matching_df['y_pred_bin'].unique()
# (matching_df
#     .loc[lambda df: df['GoodCustomer'] == 1]
#     .loc[lambda df: df['y_pred_bin'] == bins[0]]
# )

max_cost = matching_df['total_cost'].max()
plt.rc("font", size=20)
for y_true in [-1, +1]:
    plt.figure(figsize=(4, 4))
    ax = sns.violinplot(x='y_pred_bin',  y='total_cost', hue='Gender',
                        data=matching_df.loc[lambda df: df['GoodCustomer'] == y_true].sort_values('Gender'),
                        linewidth = 0.5, cut=0, background='white',
                        scale = 'width', color="gold",  inner = 'quartile')
    # , inner = 'quartile', color = "gold", scale = 'width'
    ax.set_xticklabels(["10%", "30%", "50%", "70%"])#, rotation='vertical')
    
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    
    ax.set_ylim((0, max_cost))
    # plt.title('Total Cost By Gender')
    plt.ylabel('Cost of Recourse')
    plt.xlabel('Predicted Risk' )
    
    # plt.legend(bbox_to_anchor=(0.1, .03, .65, .14), ncol=2, mode="expand", borderaxespad=0.)
    # plt.legend(fontsize=14, ncol=2, mode='expand',bbox_to_anchor=(.18, .51, .875, .51), )
    if y_true == -1:
    'Age-downsampled': "Sample Pop.",
    'Full Dataset': "Target Pop.",
}

plt.rc("font", size=20)
for y_true in [0, 1]:

    for training_run in ['Downsampled', 'Full Dataset']:

        plt.figure(figsize=(4, 4))
        ax = sns.violinplot(
            x='age_cut',
            y='total_cost',
            data=(combined_data_df.
                  loc[lambda df: df['Training Run'] == training_run].
                  loc[lambda df: df['y_true'] == y_true]),
            linewidth=0.5,
            cut=0,
            scale='width',
            color="gold",
            inner='quartile')

        plt.ylim((0, max_total_cost))
        plt.ylabel("Cost of Recourse")
        plt.xlabel("Age")
        ax.set_ylim((0, 1))
        ax.set_xticks(np.arange(0, 14, 2) - 1, )
        ax.set_xticklabels(np.arange(20, 90, 10))
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        for l in ax.lines: