Last active
February 3, 2022 04:33
-
-
Save izikeros/7d7a325bd487180f29df420160a02c47 to your computer and use it in GitHub Desktop.
Add value labels to matplotlibs' bar or barh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # See: plot_enhancements.py gist | |
| def add_value_labels_barh(ax, spacing=5): | |
| """ | |
| Add labels to the end of each bar in a barh chart. | |
| Tip: usually one need to add expansion of xlimit to keel labels inside plot, e.g.: | |
| ax.set_xlim(0, 1.1) | |
| Arguments: | |
| ax (matplotlib.axes.Axes): The matplotlib object containing the axes | |
| of the plot to annotate. | |
| spacing (int): The distance between the labels and the bars. | |
| """ | |
| # from: https://stackoverflow.com/a/48372659/3247880 | |
| # For each bar: Place a label | |
| for rect in ax.patches: | |
| # Get X and Y placement of label from rect. | |
| x_value = rect.get_width() | |
| y_value = rect.get_y() + rect.get_height() / 2 | |
| # Number of points between bar and label. Change to your liking. | |
| space = spacing | |
| # Vertical alignment for positive values | |
| ha = 'left' | |
| # If value of bar is negative: Place label left of bar | |
| if x_value < 0: | |
| # Invert space to place label to the left | |
| space *= -1 | |
| # Horizontally align label at right | |
| ha = 'right' | |
| # Use X value as label and format number with one decimal place | |
| label = "{:.2f}".format(x_value) | |
| # Create annotation | |
| plt.annotate( | |
| label, # Use `label` as label | |
| (x_value, y_value), # Place label at end of the bar | |
| xytext=(space, 0), # Horizontally shift label by `space` | |
| textcoords="offset points", # Interpret `xytext` as offset in points | |
| va='center', # Vertically center label | |
| ha=ha) # Horizontally align label differently for | |
| # positive and negative values. | |
| def add_value_labels_bar(ax, spacing=5): | |
| """ | |
| Add labels to the end of each bar in a bar chart. | |
| Tip: usually one need to add expansion of ylimit to keel labels inside plot, e.g.: | |
| ax.set_ylim(0, 1.1) | |
| Arguments: | |
| ax (matplotlib.axes.Axes): The matplotlib object containing the axes | |
| of the plot to annotate. | |
| spacing (int): The distance between the labels and the bars. | |
| """ | |
| # For each bar: Place a label | |
| for rect in ax.patches: | |
| # Get X and Y placement of label from rect. | |
| y_value = rect.get_height() | |
| x_value = rect.get_x() + rect.get_width() / 2 | |
| # Number of points between bar and label. Change to your liking. | |
| space = spacing | |
| # Vertical alignment for positive values | |
| va = 'bottom' | |
| # If value of bar is negative: Place label below bar | |
| if y_value < 0: | |
| # Invert space to place label below | |
| space *= -1 | |
| # Vertically align label at top | |
| va = 'top' | |
| # Use Y value as label and format number with one decimal place | |
| label = "{:.1f}".format(y_value) | |
| # Create annotation | |
| ax.annotate( | |
| label, # Use `label` as label | |
| (x_value, y_value), # Place label at end of the bar | |
| xytext=(0, space), # Vertically shift label by `space` | |
| textcoords="offset points", # Interpret `xytext` as offset in points | |
| ha='center', # Horizontally center label | |
| va=va) # Vertically align label differently for | |
| # positive and negative values. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment