def plot_pie(values, labels, colors=[], title="", legend=False, sort=False): '''Extended version of matplotlib.pyplot's pie chart. In addition to plt.pie's original functionality the function can create a randomized color scheme for the slices in the pie and customize a legend table. Also there's an option to sort the value/labels based on the values. Argument: values - counts for labels labels - labels or names of the pie slices colors - predetermined color scheme title - title of the chart legend - whether or not a legend should be added next to the chart sort - whether or not the value-labels should be sorted based on labels ''' if title: plt.title(title) if not colors: colors = random.sample(cnames.keys(), len(values)) if sort: matrix = sorted(transpose([labels, values])) labels = transpose(matrix)[0] values = transpose(matrix)[1] if legend: patches, texts = plt.pie(values, colors=colors, shadow=True, startangle=140) percent = [100.*val/sum(values) for val in values] labels = ['{0} - {1:1.1f} %'.format(i,j) for i,j in zip(labels, percent)] plt.legend(patches, labels, loc='best', fontsize=10) else: plt.pie(values, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) plt.show()
def _highlight_plot(view, x_array, y_array, operator, y_values, colors): # Find corresponding indices if colors is None or len(colors) != len(y_values): from matplotlib.colors import cnames lcolors = list(cnames.keys()) i = ot.RandomGenerator.IntegerGenerate(len(y_values), len(lcolors)) colors = [lcolors[_] for _ in i] dim = len(x_array[0]) for k, val in enumerate(y_values): I = np.array([operator(_[0], val) for _ in y_array]) indices = np.where(I)[0] if len(indices) > 0: for i in range(dim): x = x_array[:, i] z = y_array[:, 0] index_i_i = 1 + i * dim + i view._ax[index_i_i].plot(x[indices], z[indices], '.', color=colors[k]) for j in range(i + 1, dim): y = x_array[:, j] index_i_j = 1 + i * dim + j index_j_i = 1 + j * dim + i view._ax[index_i_j].plot(x[indices], y[indices], '.', color=colors[k]) view._ax[index_j_i].plot(y[indices], x[indices], '.', color=colors[k]) return view
def plot(self, filename, labels): #obtain coordinates x = [coord[0] for coord in self.coords] y = [coord[1] for coord in self.coords] #obtain classes classes = [key for key, value in self.idmaker.items.iteritems()] all_colours = random.sample(list(cnames.keys()), len(classes) + 1) #translate class to color colours = [all_colours[label] for label in labels] plt.scatter(x, y, c=colours) #add legend recs = [] for label in classes: recs.append( mpatches.Rectangle( (0, 0), 1, 1, color=all_colours[self.idmaker.items[label]])) plt.legend(recs, classes, loc='center left', bbox_to_anchor=(1, 0.5), prop={'size': 10}) plt.savefig(filename, bbox_inches='tight') plt.clf()
def plot_color_picker(i): from matplotlib.colors import cnames from random import shuffle colorcodes = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] colorcodes.extend(cnames.keys()) i = i % len(colorcodes) return colorcodes[i]
def plot_ROC(model, X, y, cv_fold, png_filename=None): # Set up mean true and false positive rates mean_tpr, mean_fpr = 0, np.linspace( 0, 1, 101) # np.linspace(start, stop, datapoints) # Set the matplotlib colorwheel as a cycle colors = itertools.cycle(list(cnames.keys())) # Set the cross-validation fold skf = StratifiedKFold( cv_fold) # list(skf.split(X,y)) returns list of len n_splits # Create new plot fig = plt.figure() # loop over each split in the data and plot the ROC for idx, val in enumerate(zip(skf.split(X, y), colors)): (train, test), color = val probas_ = model.fit(X[train], y[train]).predict_proba( X[test]) # pull the values of X,y using indices fpr, tpr, _ = roc_curve(y[test], probas_[:, 1]) roc_auc = auc(fpr, tpr) mean_tpr += np.interp(mean_fpr, fpr, tpr) mean_tpr[0] = 0.0 plt.plot(fpr, tpr, lw=2, color=color, label='ROC fold %d (area = %0.2f)' % (idx, roc_auc)) # Plot mean tpr for all curves mean_tpr /= skf.get_n_splits(X, y) mean_tpr[-1] = 1.0 mean_auc = auc(mean_fpr, mean_tpr) plt.plot(mean_fpr, mean_tpr, color='k', linestyle='--', label='Mean ROC (area = %0.2f)' % mean_auc, lw=4) # Plot chance (tpr = fpr) plt.plot([0, 1], [0, 1], linestyle='--', lw=1, color='k', label='Chance') # Axes and labels plt.xlim([-0.05, 1.05]) plt.ylim([-0.05, 1.05]) plt.xlabel('False Positive Rate') plt.ylabel('True Positive Rate') plt.title( 'Receiver operating characteristic curve ({}-fold CV)'.format(cv_fold)) plt.legend(loc="lower right") if not png_filename: plt.show() else: plt.savefig(png_filename + '.png') return
def colorcheck(colname, varname): """ Local helper function performing sanity checks on Matplotlib color strings """ if not isinstance(colname, (str, unicode)): raise TypeError(varname + ' has to be a string!') if colname.lower() not in cnames.keys() and colname not in cnames.values(): msg = "Unsupported color `" + varname + " = " + colname + "`. Check `matplotlib.colors.cnames` for possible choices. " raise ValueError(msg)
def draw(path): fields_idx = dict() fields = [] clusters = defaultdict(list) rows = [] with open(path) as fi: for line_no, line in enumerate(fi.readlines()): line = line.replace("\n", "") if line_no == 0: fields = line.split(",") for idx, name in enumerate(fields): fields_idx[name] = idx if name not in field_converters: raise ValueError(u"un supported name `%s`" % name) else: values = line.split(",") vals = [] for idx, val in enumerate(values): vals.append(field_converters[fields[idx]](val)) rows.append(vals) cluster_lid = vals[fields_idx["cluster"]] clusters[cluster_lid].append(vals) plt.figure(1) # plt.subplot(211) # plt.hist([row[fields_idx["density"]] for row in rows], bins=100) # plt.subplot(211) plt.scatter([row[fields_idx["density"]] for row in rows], [row[fields_idx["dis"]] for row in rows], marker="o", s=30, color='b', label='density-dis') plt.figure(2) # plt.subplot(212) colors = cnames.keys() markers = MarkerStyle.markers.keys() for cluster_lid, rows in clusters.items(): print "draw cluster:", cluster_lid, len(rows) c = colors[random.randint(0, len(colors) - 1)] if cluster_lid != 0 \ else 'r' m = markers[random.randint(0, len(markers) - 1)] if cluster_lid != 0 \ else '.' plt.scatter( [row[fields_idx["lng"]] for row in rows], [row[fields_idx["lat"]] for row in rows], label=str(cluster_lid), c=c, marker=m, ) plt.show()
def set_colors(self, palette): self.palette = palette all_colors = list(cnames.keys()) if palette == 'transparent': self.colors = list( colorConverter.to_rgba_array( np.random.choice(all_colors, self.alg.k), 0.7)) + ['none'] if palette == 'solid': self.colors = list(np.random.choice(all_colors, self.alg.k)) + ['none'] if palette == 'mean': self.colors = list( np.minimum(1.0, self.alg.means[c, :3] / 255.0) for c in range(self.alg.k)) + ['none']
def ensureUnique(colour,used): if colour in used: colours=cnames.keys() i=0 while colours[i]!=colour: i+=1 if i>=len(colours): raise KeyError("colour "+colour+" not found!") # TODO: replace the following with a check of whether the colour is similar to any used colour while colours[i] in used: i+=1 if i >= len(colours): i-=1 break colour=colours[i] used.add(colour) return colour
def ensureUnique(colour, used): if colour in used: colours = cnames.keys() i = 0 while colours[i] != colour: i += 1 if i >= len(colours): raise KeyError("colour " + colour + " not found!") # TODO: replace the following with a check of whether the colour is similar to any used colour while colours[i] in used: i += 1 if i >= len(colours): i -= 1 break colour = colours[i] used.add(colour) return colour
def plot(self, filename, labels): #obtain coordinates x = [coord[0] for coord in self.coords] y = [coord[1] for coord in self.coords] #obtain classes classes = [key for key, value in self.idmaker.items.iteritems()] all_colours = random.sample(list(cnames.keys()), len(classes)+1) #translate class to color colours = [all_colours[label] for label in labels] plt.scatter(x, y, c=colours) #add legend recs = [] for label in classes: recs.append(mpatches.Rectangle((0,0),1,1,color=all_colours[self.idmaker.items[label]])) plt.legend(recs,classes,loc='center left', bbox_to_anchor=(1, 0.5), prop={'size':10}) plt.savefig(filename, bbox_inches='tight') plt.clf()
df.rename(columns={'y':'deposit'}, inplace=True) df.dtypes # y column # Binary Encoding df['deposit'] = np.where(df.deposit == 'yes', 1, 0) """###### CLEANING OUTLIERS USING PYOD""" import random from matplotlib.colors import cnames corr = df.corr()['deposit'].abs().sort_values(ascending=False) h_corr_cols = corr[corr < 1].index.tolist() colors = list(cnames.keys()) sns.set_style('darkgrid') fig , ax = plt.subplots(4,3,figsize = (16,12)) ax = ax.ravel() for i,col in enumerate(h_corr_cols): sns.boxplot(df[col], ax = ax[i],color = random.choice(colors)) x = df[h_corr_cols].values model = KNN(contamination=.1) model.fit(x) predicted = model.predict(x) outliers = df.loc[(predicted == 1),:] inliers = df.loc[(predicted == 0),:] df = df.drop(index = df.loc[(predicted == 1),:].index )
def main(): np.random.seed(42) urls = [ 'http://www.ehu.eus/ccwintco/uploads/6/67/Indian_pines_corrected.mat', 'http://www.ehu.eus/ccwintco/uploads/c/c4/Indian_pines_gt.mat', ] for url in urls: download_dataset(url) gt = load_data(DATA / 'Indian_pines_gt.mat') plt.imsave(IMG / 'gt.png', gt) ipc = load_data(DATA / 'Indian_pines_corrected.mat') p111 = scale2int(ipc[..., 111]) plt.imsave(IMG / '111.png', p111) plt.imsave(IMG / '111_canny.png', canny(p111)) data = get_data(DATA / 'indian_pines.csv', gt, ipc) X = data.copy().astype(np.float64) y = X.pop('target').astype(int) unique_y = len(y.unique()) X2 = scaler().fit(X).transform(X) n_components = 4 pca = PCA(n_components=n_components).fit(X2, y) X_pca = pca.fit_transform(X2) fig, ax = plt.subplots(1, 1) ax.set_xlabel('Principal Components') ax.set_ylabel('Variance Ratio') ax.set_title('Variance ratio for PCA on Indian Pines dataset') ax.grid() ax.set_xticks(range(1, n_components + 1)) ax.bar(range(1, n_components + 1), pca.explained_variance_ratio_) fig.savefig(IMG / 'pca_components.png') colorlist = np.random.choice(list(cnames.keys()), unique_y, replace=False).tolist() colors = y.map(lambda x: colorlist[x]) df = pd.DataFrame(X_pca[:, :2]) df = pd.concat([df, y, colors], axis=1) df.columns = ['PC1', 'PC2', 'target', 'color'] df_0 = df[df['target'] != 0] fig, ax = plt.subplots(1, 1) ax.set_xlabel('PC-1') ax.set_ylabel('PC-2') ax.set_title('PCA on Indian Pines dataset') ax.grid() ax.scatter(df_0['PC1'], df_0['PC2'], color=df_0['color'], s=3) fig.savefig(IMG / 'pc1_pc2.png') img = (df['PC1'] + df['PC2']).values.reshape((145, 145)) plt.imsave(IMG / 'pc12.png', img) c = canny(img, sigma=2., low_threshold=.15, high_threshold=.6, use_quantiles=True) plt.imsave(IMG / 'pc12_canny.png', c) gt2 = cv2.imread((IMG / 'gt.png').as_posix(), 0) plt.imsave(IMG / 'gt_canny.png', canny(gt2))
def graph(matrix, names): """ A method to graph a matrix matrix: a tdm names: a list of strings """ clean_matrix = list() keywords = ["syria", "refuge", "assad"] sorted_keys = [] colors = list(cnames.keys()) bad_colors = [ "aliceblue", "antiquewhite", "floralwhite", "hotpink", "ivory", "khaki", "lavenderblush", "lightblue", "lightcoral", "lightcyan", "lightgoldenrodyellow", "lightgreen", "lightgray", "lightpink", "lightsalmon", "lightseagreen", "lightskyblue", "lightslategray", "lightyellow", "white", "whitesmoke", "peachpuff", "pink", "beige", "azure", "bisque", "silver", "lavender", "gainsboro", "honeydew", "linen", "powderblue", "snow", "blanchedalmond", "ghostwhite", "lemonchiffon", "papayawhip" ] colors = [color for color in colors if color not in bad_colors] rectangles = list() for key in sorted(matrix.keys(), key=lambda x: x): if key in keywords: clean_matrix.append(matrix[key]) sorted_keys.append(key) bars = list() for i in range(len(names)): values = [value[1][i] for value in clean_matrix] bars.append(values) N = len(bars[0]) ind = np.arange(N) width = 1.0 / (len(bars) + 10) fig, ax = plt.subplots() ax.set_facecolor("lightblue") count = 0 print(len(bars)) for country in bars: print(country) rectangles.append( ax.bar(ind + width * count, country, width, alpha=1, color=colors[count], label=names[count])) count += 1 for country in rectangles: for rect in country: height = rect.get_height() ax.text(rect.get_x() + rect.get_width() / 2., 1.005 * height, '%d' % int(height), ha='center', va='bottom') ax.set_ylabel("Number of Tweets") ax.set_title("Number of Tweets in each country for each keyword") ax.set_xticks(ind + (len(bars)) / 2 * width) ax.set_xticklabels(sorted_keys) ax.legend() plt.show()
# plot lower bound mean plt.plot(mF, 'b', linewidth=1) plt.xlabel('iterations') plt.ylabel('lower bound') plt.gca() plt.show() # plot lower bound plt.plot(F, 'b', linewidth=1) plt.xlabel('iterations') plt.ylabel('lower bound') plt.gca() plt.show() # sparsity of mean var dist colors = list(mcolors.keys()) for i in range(D_out): plt.plot(range(D), meandist[:, i], '.', color=colors[i], markersize=3) plt.xlabel('variable index') plt.ylabel('mean of var dist') rnge = np.max(meandist) - np.min(meandist) plt.axis([ 0, len(meandist) + 1, (np.min(meandist) - 0.02 * rnge), np.max(meandist + 0.02 * rnge) ]) plt.show() mu_in = mu_in.numpy() mu_out = mu_out.numpy() X = X.data.numpy()
def plot_ROC_CV(clf, X, y, cv_fold=3, pos_label_=None, verbose=False): """ clf, classifiers, df_ROCs = plot_ROC_CV(clf, X, y, cv_fold=3, pos_label_=None, verbose=False) Plot an interactive ROC curve for a binary classifier with n='cv-fold' cross-validations. It returns the original clf, a classifier for each cv, a list of dataframes for each cv, and precision_info, which is a tuple of (precision, recall, avg_precision) of types (array, array, float). clf: untrained classifier object (e.g. rf_clf = RandomForestClassifer()) X: training + testing data y: targets (numeric/integers) cv_fold: cross-validations to run [default: 3] pos_label_: if targets are not binary (0, 1) then indicate integer for "positive" [default: None] verbose: print warnings [default: False] """ """ Check cross-validations to run and get stratification """ # Check cross-validation > 1 and get stratified data if cv_fold > 1: skf = StratifiedKFold(cv_fold) else: print(f"cv_fold must be greater than 1. You have input {cv_fold}") return clf """ Get source data for each ROC curve """ # Loop over each split in the data and get source data, df, and clf source_ROCs, df_ROCs, classifiers = [], [], [] for idx, val in enumerate(skf.split(X, y)): (train, test) = val data = (X[train], X[test], y[train], y[test]) # not that skf returns indices, not values source_, df_, clf_ = get_ROC_data(data, clf, pos_label_, verbose) source_ROCs.append(source_) df_ROCs.append(df_) classifiers.append(clf) """ Set up initial PLOT """ # Create custom HoverTool -- we'll name each ROC curve 'ROC' so we only see info on hover there hover_ = HoverTool(names=['ROC'], tooltips=[("TPR", "@y_tpr"), ("FPR", "@x_fpr"), ("Threshold", "@thresh")]) # Create your toolbox p_tools = [hover_, 'crosshair', 'zoom_in', 'zoom_out', 'save', 'reset', 'tap', 'box_zoom'] # Create figure and labels clf_name = get_clf_name(clf) p = figure(title=f'{clf_name} ROC curve with {cv_fold}-fold cross-validation', tools=p_tools) p.xaxis.axis_label = 'False Positive Rate' p.yaxis.axis_label = 'True Positive Rate' """ Get ROC CURVE for each iteration """ # Set the matplotlib colorwheel as a cycle colors_ = cycle(list(cnames.keys())) # plot each ROC curve - loop over source_ROCs, colors_ for _, val in enumerate(zip(source_ROCs, colors_)): (ROC, color_) = val p.line('x_fpr', 'y_tpr', line_width=1, color=color_, source=ROC) p.circle('x_fpr', 'y_tpr', size=10, color=color_, legend='auc_legend', source=ROC, name='ROC') """ Mean ROC and AUC for all curves and plot """ # process inputs mean_fpr, mean_tpr = interpolate_mean_tpr(df_list=df_ROCs) mean_auc = auc(mean_fpr, mean_tpr) mean_legend = [f'Mean, AUC: {mean_auc:.3f}']*len(mean_tpr) # Create ColumnDataSource source_ROC_mean = ColumnDataSource(data=dict(x_fpr=mean_fpr, y_tpr=mean_tpr, auc_legend=mean_legend)) # Plot mean ROC p.line('x_fpr', 'y_tpr', legend='auc_legend', color='black', line_width=3.33, line_alpha=0.33, line_dash='dashdot', source=source_ROC_mean, name='ROC') # Plot chance (tpr = fpr) p.line([0, 1], [0, 1], line_dash='dashed', line_width=0.5, color='black', name='Chance') # Finishing touches p.legend.location = "bottom_right" show(p) return clf, classifiers, df_ROCs
def bokeh_html(data): bk.output_file("test.html", title="SRS test plots") TOOLS = "pan,wheel_zoom,box_zoom,reset,save,box_select" # Figure 1: Raw time series data p1 = bk.figure( plot_width=700, plot_height=700, outline_line_color="red", tools=TOOLS, title="Raw Time Series Data", x_axis_label='Time(sec)', y_axis_label='Amplitude(Volts)') for channel_idx in range(24): p1.line(data._time_data[0], data.raw_volts[channel_idx], color=cnames.keys()[channel_idx]) bk.hold() # Figure 2: SRS Frequency Response p2 = bk.figure( plot_width=1100, plot_height=800, # width and height of the entire plot in pixels, including border space outline_line_color="red", tools=TOOLS, y_axis_type="log", x_axis_type="log", title="Shock Response Spectrum (SRS)", y_axis_label='Acceleration (gs)', x_axis_label='Frequency (Hz)') for channel_idx in range(24): p2.line(data.srs_fn, data.srs_gs[channel_idx], legend=data._labels[channel_idx + 1], line_alpha=0.8, line_width=1.5, min_border=2, color=cnames.keys()[channel_idx]) bk.hold() p2.grid.grid_line_color = "grey" p2.line(data.srs_fn, data.spec_interp_plus9dB, color='black', line_dash=[4, 4]) p2.line(data.srs_fn, data.spec_interp_plus6dB, color='black', line_dash=[4, 4]) p2.line(data.srs_fn, data.spec_interp_minus3dB, color='black', line_dash=[4, 4]) p2.line(data.srs_fn, data.spec_interp_minus6dB, color='black', line_dash=[4, 4]) p2.x_range = Range1d(start=10 ** 2, end=10 ** 5) # Figure 3: SRS Freq content per accel colors = [ "#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d" ] freq = [] channel = [] color = [] rate = [] largest_gs = 0 smallest_gs = 10000 for ch_idx in range(24): for fn_idx in range(120): freq.append(data.srs_fn[fn_idx]) rate.append(data.srs_gs[ch_idx][fn_idx]) channel.append(ch_idx + 1) if largest_gs < data.srs_gs[ch_idx][fn_idx]: largest_gs = data.srs_gs[ch_idx][fn_idx] if smallest_gs > data.srs_gs[ch_idx][fn_idx]: smallest_gs = data.srs_gs[ch_idx][fn_idx] largest_gs_lg = math.log(largest_gs) smallest_gs_lg = math.log(smallest_gs) diff = largest_gs_lg - smallest_gs_lg for i in range(len(rate)): color.append(colors[int((math.log(rate[i]) - smallest_gs_lg) * 8 / diff)]) source = bk.ColumnDataSource( data=dict( freq=[str(round(x, 1)) for x in freq], # y channel=channel, # x color=color, rate=rate) ) TOOLS = "resize,hover,save" p3 = bk.figure(title="Frequency data per Channel", x_range=[str(x) for x in range(1, 25)], y_range=[str(round(x, 1)) for x in list(reversed(data.srs_fn))], outline_line_color="red", x_axis_location="above", plot_width=1100, plot_height=900, toolbar_location="left", tools=TOOLS) p3.rect("channel", "freq", 1, 1, source=source, x_axis_location="above", color="color", line_color=None, title="Freq vs Accel") p3.grid.grid_line_color = "black" p3.axis.axis_line_color = "black" p3.axis.major_tick_line_color = "black" p3.axis.major_label_text_font_size = "5pt" p3.axis.major_label_standoff = 0 hover = p3.select(dict(type=HoverTool)) hover.snap_to_data = False hover.tooltips = OrderedDict([ ('channel', '@channel'), ('freq', '@freq'), ('rate', '@rate') ]) # bk.show(bk.VBox(p1, p2, p3))
def plot_ROC_clfs(classifiers, X, y, test_size=0.33, pos_label_=None, verbose=False): """ clf, classifiers, df_ROCs, precision_info = plot_ROC_clfs(clf, X, y,pos_label_=None, verbose=False) Plot an interactive ROC curve for a binary classifier with n='cv-fold' cross-validations. It returns the original clf, a classifier for each cv, and a list of dataframes for each cv. precision_info is a tuple of (precision, recall, avg_precision) of types (array, array, float). classifiers: list of untrained classifiers X: training + testing data y: targets (numeric/integers) test_size: test size for train_test_split (0 < x < 1) pos_label_: if targets are not binary (0, 1) then indicate integer for "positive" [default: None] verbose: print warnings [default: False] """ """ Get source data for each ROC curve """ # Get training and test data (data_) = train_test_split(X, y, test_size=test_size) # Loop over each CLASSIFIER now -- note that we don't redefine our classifiers source_ROCs, df_ROCs = [], [] for _, clf_ in enumerate(classifiers): source_, df_, clf_ = get_ROC_data(data_, clf_, pos_label_, verbose) source_ROCs.append(source_) df_ROCs.append(df_) """ Set up initial PLOT """ # Create custom HoverTool -- we'll name each ROC curve 'ROC' so we only see info on hover there hover_ = HoverTool(names=['ROC'], tooltips=[("TPR", "@y_tpr"), ("FPR", "@x_fpr"), ("Threshold", "@thresh")]) # Create your toolbox p_tools = [hover_, 'crosshair', 'zoom_in', 'zoom_out', 'save', 'reset', 'tap', 'box_zoom'] # Create figure and labels p = figure(title=f'Benchmarking {len(classifiers)} classifiers', tools=p_tools) p.xaxis.axis_label = 'False Positive Rate' p.yaxis.axis_label = 'True Positive Rate' """ Get ROC CURVE for each iteration """ # Set the matplotlib colorwheel as a cycle colors_ = cycle(list(cnames.keys())) # loop over source, color and plot each ROC curve for _, val in enumerate(zip(source_ROCs, colors_)): (ROC, color_) = val p.line('x_fpr', 'y_tpr', line_width=1, color=color_, source=ROC) p.circle('x_fpr', 'y_tpr', size=5, color=color_, legend='clf_legend', source=ROC, name='ROC') """ Mean ROC and AUC for all curves and plot """ # process mean values, legend, ColumnDataSource mean_fpr, mean_tpr = interpolate_mean_tpr(df_list=df_ROCs) mean_auc = auc(mean_fpr, mean_tpr) mean_legend = [f'Mean, AUC: {mean_auc:.3f}']*len(mean_tpr) source_ROC_mean = ColumnDataSource(data=dict(x_fpr = mean_fpr, y_tpr = mean_tpr, roc_legend=mean_legend)) # PLOT mean ROC p.line('x_fpr', 'y_tpr', legend='roc_legend', color='black', line_width=5, line_alpha=0.3, line_dash='dashed', source=source_ROC_mean, name='ROC') # PLOT chance (tpr = fpr) p.line([0, 1], [0, 1], line_dash='dashed', line_width=0.2, color='black', name='Chance') # Finishing touches p.legend.location = "bottom_right" show(p) # Print scores print("Scores:") # Get scores for each classifier: for i, df_ in enumerate(df_ROCs): print(df_.clf, np.round(df_.score, decimals=3)) return classifiers, df_ROCs
from matplotlib.pylab import * from matplotlib.colors import cnames keys = list(cnames.keys()) def HSVtoRGB(h, s, v): """ http://www.cs.rit.edu/~ncs/color/t_convert.html """ if s == 0: return v, v, v h /= 60 # sector 0 to 5 i = floor(h) f = h - i # factorial part of h p = v * (1 - s) q = v * (1 - s * f) t = v * (1 - s * (1 - f)) if i == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v return v, p, q z = [] i = [] g = 0 filloff = 0.
def hist_channel(celldicts, labels, outputdir='.', bins=None, colors=None, units_dx=None, titles=None, mode='total_fl', qcut=0, xminxmax=None, backgrounds=None, ncol=None): """ Make an histogram of the signal obtained per cell. """ # initialization if mode == 'concentration_fl': print "concentration_fl mode" fl_dtype = np.float_ fmt = "$\\mu = {mu:,.2f}$\n$\\sigma = {sig:,.2f}$\n$N = {N:,d}$\n$\\mathrm{{med}} = {med:,.2f}$" elif mode == 'total_fl': print "total_fl mode" fl_dtype = np.uint16 fmt = "$\\mu = {mu:,d}$\n$\\sigma = {sig:,d}$\n$N = {N:,d}$\n$\\mathrm{{med}} = {med:,d}$" else: raise ValueError( 'Wrong mode selection: \'total_fl\' or \'concentration_fl\'') # input data ndata = len(celldicts) if ndata == 0: raise ValueError("Empty input data") if ndata != len(labels): raise ValueError("Labels should have the same dimension as cell dicts") print "ndata = {:d}".format(ndata) # channels nchannel = len(celldicts[0].values()[0]['fluorescence']['total']) print "nchannel = {:d}".format(nchannel) # colors if colors is None: colors = colornames.keys()[:ndata] # bins if bins is None: bins = {i: None for i in range(nchannel)} for c in range(nchannel): if bins[c] is None: bins[c] = ['auto'] * ndata # units_dx if units_dx is None: units_dx = {i: None for i in range(nchannel)} # titles if titles is None: titles = [None for i in range(nchannel)] # xminxmax if xminxmax is None: xminxmax = [[None, None] for i in range(nchannel)] # filling up the data data = [[] for c in range(nchannel)] data_bg = [[] for c in range(nchannel)] for c in range(nchannel): for i in range(ndata): cells = celldicts[i] ncells = len(cells) if ncells == 0: raise ValueError("Empty cell dictionary!") FL = [] BG = [] # make lists keys = cells.keys() for n in range(ncells): key = keys[n] cell = cells[key] npx = cell['area'] fl = cell['fluorescence']['total'] bg_px = cell['fluorescence']['background_px'] x = fl[c] bg = bg_px[c] * npx if mode == 'concentration_fl': try: volume = cell['volume'] except KeyError: raise ValueError('Missing volume attribute in cell!') x = float(x) / volume bg = float(bg) / float(volume) elif mode == 'total_fl': pass FL.append(x) BG.append(bg) # end loop on cells FL = np.array(FL, dtype=fl_dtype) BG = np.array(BG, dtype=fl_dtype) data[c].append(FL) data_bg[c].append(BG) # end loop on data sets # end loop on channels # Ns = [len(d) for d in data] # mus = [np.mean(d).astype(d.dtype) for d in data] # meds = [np.median(d).astype(d.dtype) for d in data] # sigs = [np.std(d).astype(d.dtype) for d in data] # errs = [s/np.sqrt(N) for s,N in zip(sigs,Ns)] if backgrounds is None: bgcolor = 'r' bgs = [ np.nanmedian(np.concatenate(data_bg[c])).astype( data_bg[c][0].dtype) for c in range(nchannel) ] else: bgcolor = 'g' bgs = [np.float_(backgrounds[c]) for c in range(nchannel)] # make figure fig = plt.figure(num=None, facecolor='w', figsize=(4 * nchannel, 3)) gs = mgs.GridSpec(1, nchannel) ax0 = fig.add_subplot(gs[0, 0]) axes = [ax0] for c in range(1, nchannel): ax = fig.add_subplot(gs[0, c]) axes.append(ax) for c in range(nchannel): print "channel = {:d} / {:d}".format(c, nchannel - 1) ax = axes[c] for i in range(ndata): print "{:<2s}data {:d} / {:d}".format("", i, ndata - 1) # compute histogram d = data[c][i] N = len(d) d = np.sort(d) n0 = int(0.5 * qcut * float(N)) print "{:<4s}qcut = {:.1f} %".format("", qcut * 100) n1 = N - n0 # print n0, n1 d = d[n0:n1] hist, edges = np.histogram(d, bins=bins[c][i], density=True) print "{:<4s}nbins = {:,d}".format("", len(edges) - 1) # plot histogram color = colors[i] #ax.bar(edges[:-1], hist, np.diff(edges), color='none', edgecolor=color, lw=0.5, label=labels[i]) ax.plot(0.5 * (edges[:-1] + edges[1:]), hist, '-', color=color, lw=0.5) # end loop on data # plot background ax.axvline(x=bgs[c], color=bgcolor, lw=0.5, ls='--') # add legends if not (titles[c] is None): ax.set_title(titles[c], fontsize='large') # adjust the axis if (c == 0): ax.legend(loc='best', fontsize="medium", frameon=False) #ax.set_ylabel("pdf",fontsize="medium",labelpad=10) ax.tick_params(length=4) ax.tick_params(axis='both', labelsize='medium', labelleft=False, left=False) if not (units_dx[c] is None): ax.xaxis.set_major_locator( matplotlib.ticker.MultipleLocator(base=units_dx[c])) # ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(base=0.1)) # ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(base=0.5)) # ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(base=0.1)) ax.set_xlim(xminxmax[c]) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) # end loop on channels # legend patches = [] for n in range(ndata): label = labels[n] color = colors[n] if label is None: label = "{:d}".format(n) patch = mpatches.Patch(color=color, label=label) patches.append(patch) #axes[2].add_artist(plt.legend(handles=patches, loc='upper right')) if (ncol is None): ncol = ndata #plt.figlegend(handles=patches, fontsize='small', mode='expand', ncol=ncol,borderaxespad=0, borderpad=0, loc='lower center', frameon=False) plt.figlegend(handles=patches, fontsize='small', ncol=ncol, borderaxespad=0, borderpad=0, loc='lower center', frameon=False) nrow = int(float(ndata) / ncol + 0.5) a = 0.05 rect = [0., nrow * a, 1., 1.] gs.tight_layout(fig, rect=rect) if mode == 'concentration_fl': filename = 'analysis_overlay_concentration_fl' elif mode == 'total_fl': filename = 'analysis_overlay_total_fl' exts = ['.pdf', '.svg', '.png'] for ext in exts: fileout = os.path.join(outputdir, filename + ext) fig.savefig(fileout, bbox_inches='tight', pad_inches=0) print "Fileout: {:<s}".format(fileout) return
def hist_dimensions(celldicts, labels, outputdir='.', bins=None, colors=None, units_dx=None, title=None, mode='um', qcut=0, ncol=None): """ Make an histogram of the signal obtained per cell. """ # initialization if mode == 'um': titles = [ u'length (\u03BCm)', u'width (\u03BCm)', u'area (\u03BCm\u00B2)', u'volume (\u03BCm\u00B3)' ] attrs = ['height_um', 'width_um', 'area_um2', 'volume_um3'] else: titles = [ u'length (px)', u'width (px)', u'area (px\u00B2)', u'volume (px\u00B3)' ] attrs = ['height', 'width', 'area', 'volume'] nattrs = len(attrs) if len(units_dx) != nattrs: raise ValueError("units_dx has the wrong dimensions!") ndata = len(celldicts) if ndata == 0: raise ValueError("Empty input data") print "ndata = {:d}".format(ndata) # colors if colors is None: colors = colornames.keys()[:ndata] # bins if bins is None: bins = {j: None for j in range(nattrs)} for j in range(nattrs): if bins[j] is None: bins[j] = ['auto'] * ndata # units_dx if units_dx is None: units_dx = {i: None for i in range(nattrs)} # titles if titles is None: titles = [None for i in range(nattrs)] # filling up the data data = [] for i in range(ndata): cells = celldicts[i] ncells = len(cells) if ncells == 0: raise ValueError("Empty cell dictionary!") # make lists dimensions = [[] for i in range(nattrs)] keys = cells.keys() for n in range(ncells): key = keys[n] cell = cells[key] for i in range(nattrs): attr = attrs[i] dimensions[i].append(cell[attr]) dimensions = np.array(dimensions) data.append(dimensions) # end loop on data sets Ns = [len(d) for d in data] mus = [np.mean(d, axis=1).astype(d.dtype) for d in data] meds = [np.median(d, axis=1).astype(d.dtype) for d in data] sigs = [np.std(d, axis=1).astype(d.dtype) for d in data] errs = [s / np.sqrt(N) for s, N in zip(sigs, Ns)] # make figure fig = plt.figure(num=None, facecolor='w', figsize=(4 * nattrs, 3)) gs = mgs.GridSpec(1, nattrs) ax0 = fig.add_subplot(gs[0, 0]) axes = [ax0] for j in range(1, nattrs): #ax = fig.add_subplot(gs[0,j],sharey=ax0) ax = fig.add_subplot(gs[0, j]) axes.append(ax) for j in range(nattrs): attr = attrs[j] print "attr {:d} / {:d}".format(j, nattrs - 1) ax = axes[j] for i in range(ndata): print "{:<2s}data {:d} / {:d}".format("", i, ndata - 1) # compute histogram d = data[i][j] N = len(d) d = np.sort(d) n0 = int(qcut * float(N)) n1 = min(int((1. - qcut) * float(N)), N - 1) d = d[n0:n1 + 1] print "{:<4s}qcut = {:.1f} %".format("", qcut * 100) hist, edges = np.histogram(d, bins=bins[j][i], density=True) print "{:<4s}nbins = {:,d}".format("", len(edges) - 1) # plot histogram color = colors[i] #ax.bar(edges[:-1], hist, np.diff(edges), color='none', edgecolor=color, lw=0.5, label=labels[i]) ax.plot(0.5 * (edges[:-1] + edges[1:]), hist, '-', color=color, lw=0.5) # end loop # end loop # add legends if not (titles[j] is None): ax.set_title(titles[j], fontsize='large') #ax.annotate(fmts[i].format(mu=mus[i],sig=sigs[i], N=len(data[i]), med=meds[i]), xy=(0.70,0.98), xycoords='axes fraction', ha='left', va='top') # adjust the axis # if (j == 0): # ax.legend(loc='best',fontsize="medium",frameon=False) #ax.set_ylabel("pdf",fontsize="medium",labelpad=10) ax.tick_params(length=4) ax.tick_params(axis='both', labelsize='medium', labelleft=False, left=False) if not (units_dx[j] is None): ax.xaxis.set_major_locator( matplotlib.ticker.MultipleLocator(base=units_dx[j])) # ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(base=0.1)) # ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(base=0.5)) # ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(base=0.1)) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) # legend patches = [] for n in range(ndata): label = labels[n] color = colors[n] if label is None: label = "{:d}".format(n) patch = mpatches.Patch(color=color, label=label) patches.append(patch) #axes[2].add_artist(plt.legend(handles=patches, loc='upper right')) if (ncol is None): ncol = ndata #plt.figlegend(handles=patches, fontsize='small', mode='expand', ncol=ncol,borderaxespad=0, borderpad=0, loc='lower center', frameon=False) plt.figlegend(handles=patches, fontsize='small', ncol=ncol, borderaxespad=0, borderpad=0, loc='lower center', frameon=False) nrow = int(float(ndata) / ncol + 0.5) a = 0.05 rect = [0., nrow * a, 1., 1.] gs.tight_layout(fig, rect=rect) if mode == 'um': filename = 'analysis_overlay_dimensions_um' else: filename = 'analysis_overlay_dimensions_px' exts = ['.pdf', '.svg', '.png'] for ext in exts: fileout = os.path.join(outputdir, filename + ext) fig.savefig(fileout, bbox_inches='tight', pad_inches=0) print "Fileout: {:<s}".format(fileout) return
def hist_queen(celldicts, labels, outputdir='.', channels=[0, 1], bins=None, colors=None, units_dx=None, titles=None, mode='total_fl', qcut=0, xminxmax=None, backgrounds=None, ncol=None): """ Make an histogram of the signal obtained per cell for the fluorescence channels used to measure the Queen ratio. Also plot the distribution of the Queen ratio. """ # initialization if mode == 'concentration_fl': print "concentration_fl mode" fl_dtype = np.float_ fmt = "$\\mu = {mu:,.2f}$\n$\\sigma = {sig:,.2f}$\n$N = {N:,d}$\n$\\mathrm{{med}} = {med:,.2f}$" elif mode == 'total_fl': print "total_fl mode" fl_dtype = np.uint16 fmt = "$\\mu = {mu:,d}$\n$\\sigma = {sig:,d}$\n$N = {N:,d}$\n$\\mathrm{{med}} = {med:,d}$" else: raise ValueError( 'Wrong mode selection: \'total_fl\' or \'concentration_fl\'') # input data ndata = len(celldicts) if ndata == 0: raise ValueError("Empty input data") if ndata != len(labels): raise ValueError("Labels should have the same dimension as cell dicts") print "ndata = {:d}".format(ndata) # channels nchannel = 2 c1 = channels[0] c2 = channels[1] print "nchannel = {:d}, c1 = {:d}, c2 = {:d}".format(nchannel, c1, c2) nplot = nchannel + 1 # colors if colors is None: colors = colornames.keys()[:ndata] # bins if bins is None: bins = {i: None for i in range(nplot)} for c in range(nplot): if bins[c] is None: bins[c] = ['auto'] * ndata # units_dx if units_dx is None: units_dx = {i: None for i in range(nplot)} # titles if titles is None: titles = [None for i in range(nplot)] # xminxmax if xminxmax is None: xminxmax = [[None, None] for i in range(nplot)] # filling up the data data_I1 = [] data_I2 = [] data_BG1 = [] data_BG2 = [] data_queen = [] for i in range(ndata): cells = celldicts[i] ncells = len(cells) if ncells == 0: raise ValueError("Empty cell dictionary!") FL1 = [] FL2 = [] BG1 = [] BG2 = [] QUEEN = [] # make lists keys = cells.keys() for n in range(ncells): key = keys[n] cell = cells[key] npx = cell['area'] fl = cell['fluorescence']['total'] bg_px = cell['fluorescence']['background_px'] x1 = fl[c1] x2 = fl[c2] bg_x1 = bg_px[c1] * npx bg_x2 = bg_px[c2] * npx if mode == 'concentration_fl': try: volume = cell['volume'] except KeyError: raise ValueError('Missing volume attribute in cell!') x1 = float(x1) / volume x2 = float(x2) / volume if backgrounds is None: bg_x1 = float(bg_x1) / volume bg_x2 = float(bg_x2) / volume else: bg_x1 = backgrounds[c1] bg_x2 = backgrounds[c2] elif mode == 'total_fl': sys.exit("Not programmed yet!") pass try: z = ((float(x1) - float(bg_x1)) / (float(x2) - float(bg_x2))) except ZeroDivisionError: print( "x = {:.6f} bg_x = {:.6f} y = {:.6f} bg_y = {:.6f}" .format(x1, bg_x1, x2, bg_x2)) print("Skipping cell {:s}".format(cell['id'])) continue FL1.append(x1) FL2.append(x2) BG1.append(bg_x1) BG2.append(bg_x2) QUEEN.append(z) # end loop on cells data_I1.append(np.array(FL1)) data_I2.append(np.array(FL2)) data_BG1.append(np.array(BG1)) data_BG2.append(np.array(BG2)) data_queen.append(np.array(QUEEN)) # end loop on data sets if backgrounds is None: bgcolor = 'r' bgs = [ np.nanmedian(np.concatenate(data_BG1)), np.nanmedian(np.concatenate(data_BG2)) ] else: bgcolor = 'g' bgs = [np.float_(backgrounds[c]) for c in range(2)] # make figure fig = plt.figure(num=None, facecolor='w', figsize=(4 * nplot, 3)) gs = mgs.GridSpec(1, nplot) ax0 = fig.add_subplot(gs[0, 0]) axes = [ax0] for c in range(1, nplot): ax = fig.add_subplot(gs[0, c]) axes.append(ax) # plot fluorescence data = [data_I1, data_I2] for c in [c1, c2]: print "channel = {:d} / {:d}".format(c, 1) ax = axes[c] for i in range(ndata): print "{:<2s}data {:d} / {:d}".format("", i, ndata - 1) # compute histogram d = data[c][i] N = len(d) d = np.sort(d) n0 = int(qcut * float(N)) n1 = min(int((1. - qcut) * float(N)), N - 1) d = d[n0:n1 + 1] hist, edges = np.histogram(d, bins=bins[c][i], density=True) nbins = len(edges) - 1 print "{:<4s}nbins = {:,d}".format("", nbins) # plot histogram color = colors[i] #ax.bar(edges[:-1], hist, np.diff(edges), color='none', edgecolor=color, lw=0.5, label=labels[i]) ax.plot(0.5 * (edges[:-1] + edges[1:]), hist, '-', color=color, lw=0.5) # plot background ax.axvline(x=bgs[c], color=bgcolor, lw=0.5, ls='--') print "background = {:.2f}".format(bgs[c]) # end loop on data # plot Queen ratio ax = axes[2] for i in range(ndata): print "{:<2s}data {:d} / {:d}".format("", i, ndata - 1) # compute histogram d = data_queen[i] N = len(d) d = np.sort(d) n0 = int(qcut * float(N)) n1 = min(int((1. - qcut) * float(N)), N - 1) d = d[n0:n1 + 1] hist, edges = np.histogram(d, bins=bins[2][i], density=True) nbins = len(edges) - 1 print "{:<4s}nbins = {:,d}".format("", nbins) # plot histogram color = colors[i] #ax.bar(edges[:-1], hist, np.diff(edges), color='none', edgecolor=color, lw=0.5, label=labels[i]) if (c == 0): label = labels[i] else: label = None ax.plot(0.5 * (edges[:-1] + edges[1:]), hist, '-', color=color, lw=0.5, label=label) # customize axes for c in range(nplot): ax = axes[c] # add legends if not (titles[c] is None): ax.set_title(titles[c], fontsize='large') # adjust the axis if (c == 0): ax.legend(loc='best', fontsize="medium", frameon=False) #ax.set_ylabel("pdf",fontsize="medium",labelpad=10) ax.tick_params(length=4) ax.tick_params(axis='both', labelsize='medium', labelleft=False, left=False) if not (units_dx[c] is None): ax.xaxis.set_major_locator( matplotlib.ticker.MultipleLocator(base=units_dx[c])) # ax.xaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(base=0.1)) # ax.yaxis.set_major_locator(matplotlib.ticker.MultipleLocator(base=0.5)) # ax.yaxis.set_minor_locator(matplotlib.ticker.MultipleLocator(base=0.1)) ax.set_xlim(xminxmax[c]) ax.spines['left'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) # end loop on channels # legend patches = [] for n in range(ndata): label = labels[n] color = colors[n] if label is None: label = "{:d}".format(n) patch = mpatches.Patch(color=color, label=label) patches.append(patch) #axes[2].add_artist(plt.legend(handles=patches, loc='upper right')) if (ncol is None): ncol = ndata #plt.figlegend(handles=patches, fontsize='small', mode='expand', ncol=ncol,borderaxespad=0, borderpad=0, loc='lower center', frameon=False) plt.figlegend(handles=patches, fontsize='small', ncol=ncol, borderaxespad=0, borderpad=0, loc='lower center', frameon=False) nrow = int(float(ndata) / ncol + 0.5) a = 0.05 rect = [0., nrow * a, 1., 1.] gs.tight_layout(fig, rect=rect) if mode == 'concentration_fl': filename = 'analysis_overlay_queen_concentration_fl' elif mode == 'total_fl': filename = 'analysis_overlay_queen_total_fl' exts = ['.pdf', '.svg', '.png'] for ext in exts: fileout = os.path.join(outputdir, filename + ext) fig.savefig(fileout, bbox_inches='tight', pad_inches=0) print "Fileout: {:<s}".format(fileout) return
def add_plot_parser_arguments(parser): from cis.data_io.products.AProduct import AProduct from cis.parse_datetime import parse_as_number_or_datetime_delta, parse_as_number_or_datetime import cis.plugin as plugin from matplotlib.colors import cnames product_classes = plugin.find_plugin_classes(AProduct, 'cis.data_io.products', verbose=False) parser.add_argument("datagroups", metavar="Input datagroups", nargs="+", help="The datagroups to be plotted, in the format 'variable:filenames[:options]', where " "options are entered in a comma separated list of the form \'keyword=value\'. Available " "options are color, edgecolor, itemstylem, label and product. Colour is any valid html " "colour and product is one of the options listed below. For example 'cis plot " "var1:file:product=NetCDF_CF_Gridded,colour=red'. Products: " + str([cls().__class__.__name__ for cls in product_classes])) parser.add_argument("-o", "--output", metavar="Output filename", nargs="?", default=None, help="The filename of the output file for the plot image") parser.add_argument("--type", metavar="Chart type", nargs="?", help="The chart type, one of: " + str(plot_types.keys()), choices=plot_types.keys()) parser.add_argument("--xlabel", metavar="X axis label", nargs="?", help="The label for the x axis") parser.add_argument("--ylabel", metavar="Y axis label", nargs="?", help="The label for the y axis") parser.add_argument("--cbarlabel", metavar="Colour bar label", nargs="?", help="The label for the colour bar") parser.add_argument("--title", metavar="Chart title", nargs="?", help="The title for the chart") parser.add_argument("--fontsize", metavar="Font size", nargs="?", help="The size of the font in points", type=float) parser.add_argument("--cmap", metavar="Colour map", nargs="?", help="The colour map used, e.g. RdBu") parser.add_argument("--height", metavar="Plot height", nargs="?", help="The height of the plot in inches", type=float) parser.add_argument("--width", metavar="Plot width", nargs="?", help="The width of the plot in inches", type=float) parser.add_argument("--xmin", metavar="Minimum x", nargs="?", help="The minimum x value to plot", type=parse_as_number_or_datetime) parser.add_argument("--xmax", metavar="Maximum x", nargs="?", help="The maximum x value to plot", type=parse_as_number_or_datetime) parser.add_argument("--xstep", metavar="X step", nargs="?", help="The step of the x axis", type=parse_as_number_or_datetime_delta) parser.add_argument("--ymin", metavar="Minimum y", nargs="?", help="The minimum y value to plot", type=parse_as_number_or_datetime) parser.add_argument("--ymax", metavar="Maximum y", nargs="?", help="The maximum y value to plot", type=parse_as_number_or_datetime) parser.add_argument("--ystep", metavar="Y step", nargs="?", help="The step of the y axis", type=parse_as_number_or_datetime_delta) parser.add_argument("--vmin", metavar="Minimum value", nargs="?", help="The minimum value to plot", type=parse_as_number_or_datetime) parser.add_argument("--vmax", metavar="Maximum value", nargs="?", help="The maximum value to plot", type=parse_as_number_or_datetime) parser.add_argument("--vstep", metavar="X value", nargs="?", help="The step of the colour bar", type=parse_as_number_or_datetime_delta) parser.add_argument("--xbins", metavar="Number of histogram x axis bins", nargs="?", help="The number of bins on the x axis of a histogram", type=int) parser.add_argument("--ybins", metavar="Number of histogram x axis bins", nargs="?", help="The number of bins on the y axis of a histogram", type=int) parser.add_argument("--cbarorient", metavar="Colour bar orientation", nargs="?", help="The orientation of the colour bar, either horizontal or vertical", choices=['vertical', 'horizontal']) parser.add_argument("--nocolourbar", dest='colourbar', help="Does not show the colour bar", action='store_false') parser.add_argument("--logx", help="Uses a log scale (base 10) on the x axis", action='store_true') parser.add_argument("--logy", help="Uses a log scale (base 10) on the y axis", action='store_true') parser.add_argument("--logv", help="Uses a log scale (base 10) on the colour bar", action='store_true') parser.add_argument("--grid", help="Shows grid lines on the plot", action='store_true') parser.add_argument("--xaxis", metavar="Variable on x axis", nargs="?", help="Name of variable to use on the x axis") parser.add_argument("--yaxis", metavar="Variable on y axis", nargs="?", help="Name of variable to use on the y axis") parser.add_argument("--coastlinescolour", metavar="Coastlines Colour", nargs="?", help="The colour of the coastlines on a map. Any valid html colour (e.g. red)", choices=(list(cnames.keys()) + ['grey'])) parser.add_argument("--nasabluemarble", help="Add the NASA 'Blue Marble' image as the background to a map, instead of coastlines", action='store_true') parser.add_argument("--cbarscale", metavar="A scaling for the color bar", nargs="?", help="Scale the color bar, use when color bar does not match plot size", type=float) parser.add_argument("--projection", choices=projections.keys()) # Taylor diagram specific options parser.add_argument('--solid', action='store_true', help='Use solid markers') parser.add_argument('--extend', type=float, help='Extend plot for negative correlation') parser.add_argument('--fold', action='store_true', help='Fold plot for negative correlation or large variance') parser.add_argument('--gammamax', type=float, help='Fix maximum extent of radial axis') parser.add_argument('--stdbiasmax', type=float, help='Fix maximum standardised bias') parser.add_argument('--bias', metavar='METHOD', choices=['color', 'colour', 'size', 'flag'], help='Indicate bias using the specified method (colo[u]r, size, flag)') return parser
from matplotlib.pylab import * from matplotlib.colors import cnames keys = cnames.keys() def HSVtoRGB(h, s, v): """ http://www.cs.rit.edu/~ncs/color/t_convert.html """ if s == 0: return v, v, v h /= 60 # sector 0 to 5 i = floor(h) f = h - i # factorial part of h p = v * (1 - s) q = v * (1 - s * f) t = v * (1 - s * (1 - f)) if i == 0: return v, t, p if i == 1: return q, v, p if i == 2: return p, v, t if i == 3: return p, q, v if i == 4: return t, p, v return v, p, q z = [] i = [] g = 0 filloff = 0. for line in open(sys.argv[1], "r").readlines():