def test_standard_colors(self): for c in ['r', 'red', 'green', '#FF0000']: result = plotting._get_standard_colors(1, color=c) self.assertEqual(result, [c]) result = plotting._get_standard_colors(1, color=[c]) self.assertEqual(result, [c]) result = plotting._get_standard_colors(3, color=c) self.assertEqual(result, [c] * 3) result = plotting._get_standard_colors(3, color=[c]) self.assertEqual(result, [c] * 3)
def test_standard_colors(self): for c in ["r", "red", "green", "#FF0000"]: result = plotting._get_standard_colors(1, color=c) self.assertEqual(result, [c]) result = plotting._get_standard_colors(1, color=[c]) self.assertEqual(result, [c]) result = plotting._get_standard_colors(3, color=c) self.assertEqual(result, [c] * 3) result = plotting._get_standard_colors(3, color=[c]) self.assertEqual(result, [c] * 3)
def test_standard_colors_all(self): import matplotlib.colors as colors # multiple colors like mediumaquamarine for c in colors.cnames: result = plotting._get_standard_colors(num_colors=1, color=c) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=1, color=[c]) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=3, color=c) self.assertEqual(result, [c] * 3) result = plotting._get_standard_colors(num_colors=3, color=[c]) self.assertEqual(result, [c] * 3) # single letter colors like k for c in colors.ColorConverter.colors: result = plotting._get_standard_colors(num_colors=1, color=c) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=1, color=[c]) self.assertEqual(result, [c]) result = plotting._get_standard_colors(num_colors=3, color=c) self.assertEqual(result, [c] * 3) result = plotting._get_standard_colors(num_colors=3, color=[c]) self.assertEqual(result, [c] * 3)
def g_colors(self, count): color_values = [] for rgb in _get_standard_colors(count, None, 'random'): color_values.append(colors.rgb2hex(rgb)) return color_values
def g_colors(count, color): color_values = _get_standard_colors(count, None, 'random', color) return color_values
def andrews_curves(data, class_column, ax=None, samples=200, colormap=None, **kwds): """ Parameters: ----------- data : DataFrame Data to be plotted, preferably normalized to (0.0, 1.0) class_column : Name of the column containing class names ax : matplotlib axes object, default None samples : Number of points to plot in each curve colormap : str or matplotlib colormap object, default None Colormap to select colors from. If string, load colormap with that name from matplotlib. kwds : Optional plotting arguments to be passed to matplotlib Returns: -------- ax: Matplotlib axis object """ from math import sqrt, pi, sin, cos import matplotlib.pyplot as plt def function(amplitudes): def f(x): x1 = amplitudes[0] result = x1 / sqrt(2.0) harmonic = 1.0 for x_even, x_odd in zip(amplitudes[1::2], amplitudes[2::2]): result += (x_even * sin(harmonic * x) + x_odd * cos(harmonic * x)) harmonic += 1.0 if len(amplitudes) % 2 != 0: result += amplitudes[-1] * sin(harmonic * x) return result return f n = len(data) class_col = data[class_column] uniq_class = class_col.drop_duplicates() columns = [data[col] for col in data.columns if (col != class_column)] x = [-pi + 2.0 * pi * (t / float(samples)) for t in range(samples)] used_legends = set([]) colors = _get_standard_colors(num_colors=len(uniq_class), colormap=colormap, color_type='random', color=kwds.get('color')) col_dict = dict([(klass, col) for klass, col in zip(uniq_class, colors)]) if ax is None: ax = plt.gca(xlim=(-pi, pi)) for i in range(n): row = [columns[c].iloc[i] for c in range(len(columns))] f = function(row) y = [f(t) for t in x] label = None if com.pprint_thing(class_col.iloc[i]) not in used_legends: label = com.pprint_thing(class_col.iloc[i]) used_legends.add(label) ax.plot(x, y, color=col_dict[class_col.iloc[i]], label=label, **kwds) else: ax.plot(x, y, color=col_dict[class_col.iloc[i]], **kwds) ax.legend(loc='upper right') ax.grid() return ax
def parallel_coordinates(model, data, c=None, cols=None, ax=None, colors=None, use_columns=False, xticks=None, colormap=None, target="top", brush=None, zorder=None, **kwds): if "axes.facecolor" in mpl.rcParams: orig_facecolor = mpl.rcParams["axes.facecolor"] mpl.rcParams["axes.facecolor"] = "white" df = data.as_dataframe(_combine_keys(model.responses.keys(), cols, c)) #, exclude_dtypes=["object"]) if brush is not None: brush_set = BrushSet(brush) assignment = apply_brush(brush_set, data) color_map = brush_color_map(brush_set, assignment) class_col = pd.DataFrame({"class" : assignment})["class"] is_class = True else: if c is None: c = df.columns.values[-1] class_col = df[c] is_class = df.dtypes[c].name == "object" color_map = None if is_class: df = df.drop(c, axis=1) if c in cols: cols.remove(c) else: class_min = class_col.min() class_max = class_col.max() if cols is not None: df = df[cols] df_min = df.min() df_max = df.max() df = (df - df_min) / (df_max - df_min) n = len(df) used_legends = set([]) ncols = len(df.columns) for i in range(ncols): if target == "top": if model.responses[df.columns.values[i]].dir == Response.MINIMIZE: df.ix[:,i] = 1-df.ix[:,i] elif target == "bottom": if model.responses[df.columns.values[i]].dir == Response.MAXIMIZE: df.ix[:,i] = 1-df.ix[:,i] # determine values to use for xticks if use_columns is True: if not np.all(np.isreal(list(df.columns))): raise ValueError('Columns must be numeric to be used as xticks') x = df.columns elif xticks is not None: if not np.all(np.isreal(xticks)): raise ValueError('xticks specified must be numeric') elif len(xticks) != ncols: raise ValueError('Length of xticks must match number of columns') x = xticks else: x = range(ncols) if ax is None: fig = plt.figure() ax = plt.gca() else: fig = ax.get_figure() cmap = plt.get_cmap(colormap) if is_class: if color_map is None: if isinstance(colors, dict): cmap = colors else: from pandas.tools.plotting import _get_standard_colors classes = class_col.drop_duplicates() color_values = _get_standard_colors(num_colors=len(classes), colormap=colormap, color_type='random', color=colors) cmap = dict(zip(classes, color_values)) else: cmap = color_map if zorder is None: indices = range(n) else: indices = [i[0] for i in sorted(enumerate(df[zorder]), key=lambda x : x[1])] for i in indices: y = df.iloc[i].values kls = class_col.iat[i] if is_class: label = str(kls) if label not in used_legends: used_legends.add(label) ax.plot(x, y, label=label, color=cmap[kls], **kwds) else: ax.plot(x, y, color=cmap[kls], **kwds) else: ax.plot(x, y, color=cmap((kls - class_min)/(class_max-class_min)), **kwds) for i in x: ax.axvline(i, linewidth=2, color='black') format = "%.2f" if target == "top": value = df_min[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_max[i] if model.responses[df.columns.values[i]].dir != Response.INFO: format = format + "*" elif target == "bottom": value = df_max[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_min[i] else: value = df_max[i] if model.responses[df.columns.values[i]].dir == Response.MAXIMIZE: format = format + "*" ax.text(i, 1.001, format % value, ha="center", fontsize=10) format = "%.2f" if target == "top": value = df_max[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_min[i] elif target == "bottom": value = df_min[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE else df_max[i] if model.responses[df.columns.values[i]].dir != Response.INFO: format = format + "*" else: value = df_min[i] if model.responses[df.columns.values[i]].dir == Response.MINIMIZE: format = format + "*" ax.text(i, -0.001, format % value, ha="center", va="top", fontsize=10) ax.set_yticks([]) ax.set_xticks(x) ax.set_xticklabels(df.columns, {"weight" : "bold", "size" : 12}) ax.set_xlim(x[0]-0.1, x[-1]+0.1) ax.tick_params(direction="out", pad=10) bbox_props = dict(boxstyle="rarrow,pad=0.3", fc="white", ec="black", lw=2) if target == "top": ax.text(-0.05, 0.5, "Target", ha="center", va="center", rotation=90, bbox=bbox_props, transform=ax.transAxes) elif target == "bottom": ax.text(-0.05, 0.5, "Target", ha="center", va="center", rotation=-90, bbox=bbox_props, transform=ax.transAxes) if is_class: ax.legend(loc='center right', bbox_to_anchor=(1.25, 0.5)) fig.subplots_adjust(right=0.8) else: cax,_ = mpl.colorbar.make_axes(ax) cb = mpl.colorbar.ColorbarBase(cax, cmap=cmap, spacing='proportional', norm=mpl.colors.Normalize(vmin=class_min, vmax=class_max), format='%.2f') cb.set_label(c) cb.set_clim(class_min, class_max) mpl.rcParams["axes.facecolor"] = orig_facecolor return fig
def scatter2d(model, data, x = None, y = None, c = None, s = None, s_range = (10, 50), show_colorbar = True, show_legend = False, interactive = False, brush = None, is_class = False, colors = None, **kwargs): df = data.as_dataframe() fig = plt.figure(facecolor='white') ax = plt.gca() if brush is not None: brush_set = BrushSet(brush) c, color_map = color_brush(brush_set, df) if isinstance(x, six.string_types): x_label = x x = df[x_label] else: x_label = None if isinstance(y, six.string_types): y_label = y y = df[y_label] else: y_label = None if isinstance(c, six.string_types): c_label = c c = df[c_label] else: c_label = None if isinstance(s, six.string_types): s_label = s s = df[s_label] else: s_label = None used_keys = set([x_label, y_label, c_label, s_label, None]) used_keys.remove(None) remaining_keys = list(model.responses.keys()) for key in used_keys: if key in remaining_keys: remaining_keys.remove(key) for key in remaining_keys: if x is None: x_label = key x = df[x_label] elif y is None: y_label = key y = df[y_label] elif c is None: c_label = key c = df[c_label] elif s is None: s_label = key s = df[s_label] if c is None: c = 'b' show_colorbar = False if s is None: s = 20 show_legend = False else: s_min = min(s) s_max = max(s) s = (s_range[1]-s_range[0]) * ((s-s_min) / (s_max-s_min)) + s_range[0] if is_class: if isinstance(colors, dict): cmap = colors else: from pandas.tools.plotting import _get_standard_colors classes = c.drop_duplicates() color_values = _get_standard_colors(num_colors=len(classes), colormap=kwargs["cmap"] if "cmap" in kwargs else None, color_type='random', color=colors) cmap = dict(zip(classes, color_values)) c = [cmap[c_i] for c_i in c] show_colorbar = False elif "cmap" not in kwargs: kwargs["cmap"] = RhodiumConfig.default_cmap handle = plt.scatter(x = x, y = y, c = c, s = s, **kwargs) ax.set_xlabel(x_label) ax.set_ylabel(y_label) if show_colorbar: if brush is None: cb = fig.colorbar(handle, shrink=0.5, aspect=5) cb.set_label(c_label) else: handle.set_array(np.asarray(color_indices(c, color_map))) handle.cmap = mpl.colors.ListedColormap(list(six.itervalues(color_map))) off = (len(color_map)-1)/(len(color_map))/2 height = (len(color_map)-1)-2*off ticks = [0] if len(color_map) <= 1 else [(i/(len(color_map)-1) * height + off) for i in range(len(color_map))] cb = fig.colorbar(handle, shrink=0.5, aspect=5, ticks=ticks) cb.set_label("") cb.ax.set_xticklabels(color_map.keys()) cb.ax.set_yticklabels(color_map.keys()) if show_legend: proxy = mpatches.Circle((0.5, 0.5), 0.25, fc="b") ax.legend([proxy], [s_label + " (" + str(s_min) + " - " + str(s_max) + ")"], handler_map={mpatches.Circle: HandlerSizeLegend()}) if interactive: def formatter(**kwargs): i = kwargs.get("ind")[0] point = data[i] keys = model.responses.keys() label = "Index %d" % i for key in keys: label += "\n%s: %0.2f" % (key, point[key]) return label mpldatacursor.datacursor(artists=handle, formatter=formatter, hover=True) return fig