我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用matplotlib.colors.to_rgba()。
def get_colors(color_list): """ Get color combinations for Venn diagram. This converts color names to RGBA """ rgba_colors = [] a = [0.5,0.5,0.6,0.4,0.3,0.2] i = 0 for c in color_list: rgba_colors.append(list(colors.to_rgba(c))) rgba_colors[i][3] = a[i] i+=1 return rgba_colors
def _update_colors(self, color_spec): """ Takes a sequence of 4 color tuples, builds the color maps, if the plot data isn't none will modify the plot colors """ self._colors = color_spec self._color_maps = [visualizer_colors.PMIColormap("PMIFriend", color_spec[0]), visualizer_colors.PMIColormap("PMITryst", color_spec[1]), visualizer_colors.PMIColormap("PMIHeadToHead", color_spec[2]), visualizer_colors.PMIColormap("PMIArmsRace", color_spec[3])] self.color_mappers = [cm.ScalarMappable(norm=self._normalizer, cmap=self._color_maps[0]), cm.ScalarMappable(norm=self._normalizer, cmap=self._color_maps[1]), cm.ScalarMappable(norm=self._normalizer, cmap=self._color_maps[2]), cm.ScalarMappable(norm=self._normalizer, cmap=self._color_maps[3])] self.color_samples = dict() self._legend_proxies = [] for mapper, name in zip(self.color_mappers, self.data.relation_types): rgba = mapper.to_rgba(0.7, bytes=True) self.color_samples[name] = rgba self._legend_proxies.append(mpatches.Patch(color=[i/255 for i in rgba], label=name)) self._on_color_changed() self._mpl.redraw()
def _color_plot(self): colors = [] for x, y in zip(self.x_values, self.y_values): distance = math.sqrt(x ** 2 + y ** 2) if x >= 0 and y >= 0: # First quadrant, someone has to select zero colors.append(self.color_mappers[0].to_rgba(distance)) elif x < 0 and y > 0: # Second quadrant colors.append(self.color_mappers[1].to_rgba(distance)) elif x < 0 and y < 0: # Third quadrant colors.append(self.color_mappers[2].to_rgba(distance)) elif x > 0 and y < 0: # Fourth quadrant colors.append(self.color_mappers[3].to_rgba(distance)) else: logging.error("({x}, {y}) couldn't be mapped onto grid".format(x=x, y=y)) colors.append((0, 0, 0, 0)) self.plot_data.set_color(colors) self._point_colors = colors self.axes.legend(handles=self._legend_proxies, loc="best")
def _change_selected_color(self, eve): ind = self._get_point_index_from_event(eve) if self._prev_selected_ind == ind: return None if self._prev_selected_ind is not None: self.plot_data._edgecolors[self._prev_selected_ind] = self._point_colors[self._prev_selected_ind] self.plot_data._edgecolors[ind] = colors.to_rgba(self.selected_color) self._prev_selected_ind = ind self._mpl.redraw()
def volume_overlay(ax, opens, closes, volumes, colorup='r', colordown='g', width=4, alpha=1.0): """Add a volume overlay to the current axes. The opens and closes are used to determine the color of the bar. -1 is missing. If a value is missing on one it must be missing on all Parameters ---------- ax : `Axes` an Axes instance to plot to opens : sequence a sequence of opens closes : sequence a sequence of closes volumes : sequence a sequence of volumes width : int the bar width in points colorup : color the color of the lines where close >= open colordown : color the color of the lines where close < open alpha : float bar transparency Returns ------- ret : `barCollection` The `barrCollection` added to the axes """ colorup = mcolors.to_rgba(colorup, alpha) colordown = mcolors.to_rgba(colordown, alpha) colord = {True: colorup, False: colordown} colors = [colord[open < close] for open, close in zip(opens, closes) if open != -1 and close != -1] delta = width / 2. bars = [((i - delta, 0), (i - delta, v), (i + delta, v), (i + delta, 0)) for i, v in enumerate(volumes) if v != -1] barCollection = PolyCollection(bars, facecolors=colors, edgecolors=((0, 0, 0, 1), ), antialiaseds=(0,), linewidths=(0.5,), ) ax.add_collection(barCollection) corners = (0, 0), (len(bars), max(volumes)) ax.update_datalim(corners) ax.autoscale_view() # add these last return barCollection
def index_bar(ax, vals, facecolor='b', edgecolor='l', width=4, alpha=1.0, ): """Add a bar collection graph with height vals (-1 is missing). Parameters ---------- ax : `Axes` an Axes instance to plot to vals : sequence a sequence of values facecolor : color the color of the bar face edgecolor : color the color of the bar edges width : int the bar width in points alpha : float bar transparency Returns ------- ret : `barCollection` The `barrCollection` added to the axes """ facecolors = (mcolors.to_rgba(facecolor, alpha),) edgecolors = (mcolors.to_rgba(edgecolor, alpha),) right = width / 2.0 left = -width / 2.0 bars = [((left, 0), (left, v), (right, v), (right, 0)) for v in vals if v != -1] sx = ax.figure.dpi * (1.0 / 72.0) # scale for points sy = ax.bbox.height / ax.viewLim.height barTransform = Affine2D().scale(sx, sy) offsetsBars = [(i, 0) for i, v in enumerate(vals) if v != -1] barCollection = PolyCollection(bars, facecolors=facecolors, edgecolors=edgecolors, antialiaseds=(0,), linewidths=(0.5,), offsets=offsetsBars, transOffset=ax.transData, ) barCollection.set_transform(barTransform) minpy, maxx = (0, len(offsetsBars)) miny = 0 maxy = max([v for v in vals if v != -1]) corners = (minpy, miny), (maxx, maxy) ax.update_datalim(corners) ax.autoscale_view() # add these last ax.add_collection(barCollection) return barCollection
def plot(params): ''' beautify tf log Use better library (seaborn) to plot tf event file''' log_path = params['logdir'] smooth_space = params['smooth'] color_code = params['color'] acc = ea.EventAccumulator(log_path) acc.Reload() # only support scalar now scalar_list = acc.Tags()['scalars'] x_list = [] y_list = [] x_list_raw = [] y_list_raw = [] for tag in scalar_list: x = [int(s.step) for s in acc.Scalars(tag)] y = [s.value for s in acc.Scalars(tag)] # smooth curve x_ = [] y_ = [] for i in range(0, len(x), smooth_space): x_.append(x[i]) y_.append(sum(y[i:i+smooth_space]) / float(smooth_space)) x_.append(x[-1]) y_.append(y[-1]) x_list.append(x_) y_list.append(y_) # raw curve x_list_raw.append(x) y_list_raw.append(y) for i in range(len(x_list)): plt.figure(i) plt.subplot(111) plt.title(scalar_list[i]) plt.plot(x_list_raw[i], y_list_raw[i], color=colors.to_rgba(color_code, alpha=0.4)) plt.plot(x_list[i], y_list[i], color=color_code, linewidth=1.5) plt.show()