def bar_by_indep_2d(dep_key, indep_key, data, ax = None, bins = None, color = 'b', show_all = False): x = np.asarray(data[indep_key]) y = np.asarray(data[dep_key]) if bins is None: x_bin = x else: x_bin = misc.bin(x, bins) bins = np.unique(x_bin) nbins = bins.size y_mean = np.empty(nbins) y_sem = np.empty(nbins) for i in range(nbins): y_ = y[x_bin == bins[i]] y_mean[i] = st.nanmean(y_) y_sem[i] = st.nanstd(y_) / np.sqrt(y_.size) if ax is None: fig = plt.figure(); ax = fig.add_subplot(111); if show_all: ax.scatter(x, y, color = color, alpha = 0.25) lw = 2 else: lw = 1 ax.errorbar(bins, y_mean, yerr = y_sem, color = color, lw = lw) ax.set_xlim([bins[0]-1, bins[-1]+1]) plt.show()
def bar_by_indep_by_group_1d(dep, indep, data, group_keys, bins = None, visible = True, ax = None, color = 'b', show_all = False, use_bar = False, ordered_x = False): ''' For 1D data (a value such as bandwidth), plots ''' ''' make ugroup_keys, a list of the group names make groups_bin, the group values for each data point (binned for easier indexing) ''' ngroups = len(group_keys) if bins is None: bins = [None] * ngroups # get all levels for all groups groups_bin = [] ugroup_keys = [] for gr, bin in zip(group_keys, bins): if bin is None: ugroup_keys.append(list(np.unique(data[gr]))) groups_bin.append(data[gr]) else: ugroup_keys.append(bin) groups_bin.append(misc.bin(data[gr], bin)) # loop through all combinations of groups ndata = data.size for ugroup_key in itertools.product(*ugroup_keys): ix = np.empty(ndata) * True for group_key, ugroup_key_ in zip(group_keys, ugroup_key): ix_ = data[group_key] == ugroup_key_ ix = np.vstack((ix, ix_)).all(0) dat data[group[0]==gr[0]] if ax is None: fig = plt.figure(); ax = fig.add_subplot(111); clrs = 'bgrym' groups = data[group] ugroups = np.unique(groups) ngroups = ugroups.size for i in range(ngroups): data_ = data[data[group] == ugroups[i]] bar_by_indep_1d(dep, indep, data_, visible = visible, ax = ax, color = clrs[i], show_all = show_all, use_bar = use_bar, ordered_x = ordered_x) ax.legend(ugroups)
def bar_by_indep_by_group_2d(dep_key, indep_key, group_key, data, bins = None, group_bins = None, visible = True, ax = None, color = 'bgmcry', show_all = False): groups = data[group_key] if group_bins is None: group_bins = groups else: group_bins = misc.bin(groups, group_bins) bins = np.unique(group_bins) nbins = bins.size if ax is None: fig = plt.figure(); ax = fig.add_subplot(111); for i, bin in enumerate(bins): data_ = data[group_bins==bin] if data_.size > 0: bar_by_indep_2d(dep_key, indep_key, data_, visible = visible, ax = ax, color = color[i], show_all = show_all)
def bar_by_indep_by_group_3d(dep_key, indep_key, data, visible = True, ax = None, group_keys = 'gen', bins = None, iscategorical = True, show_all = False, use_bar = False, fig = None, **kwargs): ''' plots a comparison over many groups attemps to make multiple line plots containing 2 lines per plot for ease-of-viewing gen exp sess ''' ngroups = len(group_keys) # number of groups # bin data for each group if bins is None: bins = [None] * ngroups groups = [] # actual entries for each group groups_bins = [] ugroups = [] nbins_per_group = [] for bin, group_key in zip(bins, group_keys): groups.append(data[group_key]) if bin is None: groups_bins.append(groups[-1]) # binned labels for each data point ugroups.append(np.unique(groups[-1])) # all unique binned labels else: groups_bins.append(misc.bin(groups[-1], bin)) ugroups.append(bin[:-1]) nbins_per_group.append(ugroups[-1].size) # how many unique labels in each group ''' ix is for all possible group combinations, a boolean vector indicating membership of each data point in the data set l is passed to itertools.product to iterate through all combinations of group values ''' ix, l = build_index(groups_bins, ugroups, nbins_per_group) if fig is None: fig = plt.figure(figsize = (11, 14)); naxs1 = nbins_per_group[0] # exp / row naxs2 = nbins_per_group[1] # cf / column cm = plt.cm.gist_ncar clrs = [cm(i) for i in np.linspace(0, 0.9, nbins_per_group[-1])] nlines = np.zeros((nbins_per_group[0], nbins_per_group[1])) for i in itertools.product(*l): print i axisnum = i[0]*naxs2 + i[1] + 1 ax = fig.add_subplot(naxs1, naxs2, axisnum) if ix[i].sum()>0: nlines[i[0], i[1]] += 1 bar_by_indep_2d(dep_key, indep_key, data[ix[i]], ax = ax, color = clrs[i[-1]]) if i[1] == 0: ax.set_ylabel('%s\n%s' % (str(ugroups[0][i[0]]), dep_key), multialignment = 'center') if i[0] == 0: ax.set_title('%s = %s' % (str(group_keys[1]), str(ugroups[1][i[1]]))) else: ax.set_title('') axs = fig.get_axes() misc.sameyaxis(axs) misc.samexaxis(axs) done = False i = 0 while not done: i += 1 if nlines[i % nbins_per_group[0], int(np.floor(i/nbins_per_group[0]))] == nbins_per_group[2]: axs[i].legend(ugroups[2]) done = True plt.show() return fig