def test_violinplot_beanplot(): # Test violinplot and beanplot with the same dataset. data = anes96.load_pandas() party_ID = np.arange(7) labels = ["Strong Democrat", "Weak Democrat", "Independent-Democrat", "Independent-Independent", "Independent-Republican", "Weak Republican", "Strong Republican"] age = [data.exog['age'][data.endog == id] for id in party_ID] fig = plt.figure() ax = fig.add_subplot(111) violinplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30}) plt.close(fig) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30}) plt.close(fig)
def beanplot_grid(vals, names, fig=None, title=None): from matplotlib import pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid from statsmodels.graphics.boxplots import beanplot if fig is None: fig = plt.figure() if title: fig.suptitle(title) n = vals.shape[1] grid = ImageGrid(fig, 111, nrows_ncols=(n, n), aspect=False, share_all=True, label_mode='L') for i in range(n * n): grid[i].set_visible(False) for j, i in itertools.combinations_with_replacement(range(n), 2): idx = i * n + j grid[idx].set_visible(True) data = vals[:, i, j] if np.ptp(data) == 0: grid[idx].hlines(data[0], .85, 1.15, lw=0.5, color='k') else: beanplot([data], labels=[''], ax=grid[idx], plot_opts={'cutoff': True}) # TODO: actually show the names... return grid
def plot_aucs(aucs, ylabel=None, names=None, rotation=90, ha='center'): import matplotlib.pyplot as plt if names is None: names = KEY_NAMES names, aucs = list(zip(*sorted((names[k], v) for k, v in aucs.items()))) if all(a.size == 1 for a in aucs): plt.plot(aucs, linestyle='None', marker='o') indices = np.arange(len(names)) else: try: from statsmodels.graphics.boxplots import beanplot except ImportError: plt.boxplot(aucs) else: # jiggle anything that's all exactly one value, to avoid singularity aucs = [ grp if len(set(grp)) > 1 else list(grp) + [grp[0] + .01] for grp in aucs ] beanplot(aucs, ax=plt.gca(), plot_opts={'cutoff': True}) indices = np.arange(len(names)) + 1 plt.xticks(indices, names, rotation=rotation, ha=ha) plt.xlim(indices[0] - .5, indices[-1] + .5) bot, top = plt.ylim() if bot < 0 < top: plt.hlines(0, *plt.xlim(), color='k') if ylabel: plt.ylabel(ylabel)
def plot_lnprob(tup): """ Bean plot of prior vs. posterior log-probabilities Parameters ---------- tup: tuple of arrays Tuple (lnprob_prior, lnprob_post) of log-probabilities Returns ------- fig: matplotlib figure The figure with the plot """ try: from statsmodels.graphics.boxplots import beanplot fig = pl.figure() ax = fig.gca() beanplot(tup, ax=ax, jitter=False, labels=("prior","posterior"), plot_opts={"bean_mean_color":"c", "bean_median_color":"m", "violin_fc":"w", "violin_alpha":1}) ax.set_xlabel("prior vs. posterior") ax.set_ylabel("log-probability") return fig except ImportError: print "Consider installing StatsModels package for bean plots."
def plot_lnprob(tup): """ Bean plot of prior vs. posterior log-probabilities Parameters ---------- tup: tuple of arrays Tuple (lnprob_prior, lnprob_post) of log-probabilities Returns ------- fig: matplotlib figure The figure with the plot """ try: from statsmodels.graphics.boxplots import beanplot fig = pl.figure() ax = fig.gca() beanplot(tup, ax=ax, jitter=False, labels=("prior", "posterior"), plot_opts={ "bean_mean_color": "c", "bean_median_color": "m", "violin_fc": "w", "violin_alpha": 1 }) ax.set_xlabel("prior vs. posterior") ax.set_ylabel("log-probability") return fig except ImportError: print "Consider installing StatsModels package for bean plots."
def test_violinplot_beanplot(close_figures): # Test violinplot and beanplot with the same dataset. data = anes96.load_pandas() party_ID = np.arange(7) labels = ["Strong Democrat", "Weak Democrat", "Independent-Democrat", "Independent-Independent", "Independent-Republican", "Weak Republican", "Strong Republican"] age = [data.exog['age'][data.endog == id] for id in party_ID] fig = plt.figure() ax = fig.add_subplot(111) violinplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30}) fig = plt.figure() ax = fig.add_subplot(111) violinplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30, 'bw_factor':.2}) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, plot_opts={'cutoff_val':5, 'cutoff_type':'abs', 'label_fontsize':'small', 'label_rotation':30}) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, jitter=True, plot_opts={'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30}) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, jitter=True, side='right', plot_opts={'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30}) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, jitter=True, side='left', plot_opts={'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30}) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, plot_opts={'bean_legend_text': 'text'})
def test_beanplot(age_and_labels, close_figures): age, labels = age_and_labels fig, ax = plt.subplots(1, 1) beanplot(age, ax=ax, labels=labels, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 })
def make_35_binary(indata, cyto, ax, confounders): valid_mask = (indata['Age'] >= 50) | (indata['Age'] <= 35) eqn, tmp_cyto = extract_cols(indata[valid_mask], cyto, ['OlderThan50']+confounders) y, X = dmatrices(eqn, tmp_cyto, return_type='dataframe') model = sm.OLS(y, X).fit() boxes = [tmp_cyto['y'][tmp_cyto['OlderThan50']], tmp_cyto['y'][~tmp_cyto['OlderThan50']]] if ax is not None: try: beanplot(boxes, labels=['>50', '<35'], ax=ax) except: ax.boxplot(boxes) return model, model.f_pvalue, model.pvalues['OlderThan50[T.True]'], model.params['OlderThan50[T.True]']
def test_violinplot_beanplot(): """Test violinplot and beanplot with the same dataset.""" data = anes96.load_pandas() party_ID = np.arange(7) labels = [ "Strong Democrat", "Weak Democrat", "Independent-Democrat", "Independent-Independent", "Independent-Republican", "Weak Republican", "Strong Republican" ] age = [data.exog['age'][data.endog == id] for id in party_ID] fig = plt.figure() ax = fig.add_subplot(111) violinplot(age, ax=ax, labels=labels, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) plt.close(fig) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) plt.close(fig)
def make_50_binary(indata, cyto, ax, confounders): eqn, tmp_cyto = extract_cols(indata, cyto, ['OlderThan50']+confounders) y, X = dmatrices(eqn, tmp_cyto, return_type='dataframe') try: model = sm.OLS(y, X).fit() except: print indata print confounders print eqn print tmp_cyto print X print y raise TypeError if ax is not None: boxes = [tmp_cyto['y'][tmp_cyto['OlderThan50']], tmp_cyto['y'][~tmp_cyto['OlderThan50']]] try: beanplot(boxes, labels=['>50', '<50'], ax=ax) except: ax.boxplot(boxes) return model, model.f_pvalue, model.pvalues['OlderThan50[T.True]'], model.params['OlderThan50[T.True]']
def test_beanplot_legend_text(age_and_labels, close_figures): age, labels = age_and_labels fig, ax = plt.subplots(1, 1) beanplot(age, ax=ax, labels=labels, plot_opts={'bean_legend_text': 'text'})
def test_violinplot_beanplot(close_figures): # Test violinplot and beanplot with the same dataset. data = anes96.load_pandas() party_ID = np.arange(7) labels = [ "Strong Democrat", "Weak Democrat", "Independent-Democrat", "Independent-Independent", "Independent-Republican", "Weak Republican", "Strong Republican" ] age = [data.exog['age'][data.endog == id] for id in party_ID] fig = plt.figure() ax = fig.add_subplot(111) violinplot(age, ax=ax, labels=labels, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) fig = plt.figure() ax = fig.add_subplot(111) violinplot(age, ax=ax, labels=labels, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30, 'bw_factor': .2 }) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, jitter=True, plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, jitter=True, side='right', plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, jitter=True, side='left', plot_opts={ 'cutoff_val': 5, 'cutoff_type': 'abs', 'label_fontsize': 'small', 'label_rotation': 30 }) fig = plt.figure() ax = fig.add_subplot(111) beanplot(age, ax=ax, labels=labels, plot_opts={'bean_legend_text': 'text'})
from statsmodels.graphics.boxplots import beanplot labels = ['Old', 'Young', 'Pooled'] for cyto in cytos: bins = [] labs = [] for gname, gmask in comp: td = sf_data[cyto][gmask].dropna() if len(td) > 2: bins.append(td.copy()) labs.append(gname) if len(bins) > 1: fig = plt.figure() ax = plt.subplot(111) beanplot(bins, labels=labs, ax=ax) ax.set_ylim([0, ax.get_ylim()[1]]) ax.set_ylabel(cyto) ax.set_title(cyto) plt.savefig('/home/will/KrebbsData/draft_figures/violin_plot_%s.png' % cyto) plt.close() # <codecell> print res[(res['Pval'] < 0.1)][['Cyto','Group1', 'G1mean','Group2','G2mean','Pval']] # <codecell> wanted_cl = set(['Ect1', 'End1', 'VK2']) cl_data = data[data['CellLine'].map(lambda x: x in wanted_cl)]