def add_value_labels(ax: Axes, spacing: int = 5, percentage: bool = False) -> None: """ Add labels to the end of each bar in a bar chart. Overwrite labels on axes if they already exist. Args: ax (Axes): The matplotlib object containing the axes of the plot to annotate. spacing (int): The distance between the labels and the bars. percentage (bool): if y-value is a percentage """ for child in ax.get_children(): if isinstance(child, Annotation): child.remove() for rect in ax.patches: y_value = rect.get_height() x_value = rect.get_x() + rect.get_width() / 2 label = ("{:.2f}%".format(y_value * 100) if percentage else "{:.1f}".format(y_value)) ax.annotate( label, (x_value, y_value), xytext=(0, spacing), # Vertically shift label by `space` textcoords="offset points", # Interpret `xytext` as offset in points ha="center", # Horizontally center label va="bottom", # Vertically align label )
def ShowErrorBarCaps(ax: axes.Axes): """Show error bar caps. Seaborn paper style hides error bar caps. Call this function on an axes object to make them visible again. """ for child in ax.get_children(): if str(child).startswith("Line2D"): child.set_markeredgewidth(1) child.set_markersize(8)
def draw_reset(ax: axes.Axes, width: float, height: float) -> None: """ Fully reset a plot by deleting all annotations and patches, and drawing a blank background :param ax: matplotlib axes on which to draw :param width: the width of the plot :param height: the height of the plot """ for child in ax.get_children(): if isinstance(child, mtext.Annotation): child.remove() for p in reversed(ax.patches): p.remove() p1 = mpatches.Rectangle((0, 0), width, height, fill=True, color='w') ax.add_patch(p1)
def get_children(self): return [self.zaxis,] + Axes.get_children(self)