1. eject_legend() - Move Legend Outside Plot¶
Moves the legend to the right side of the plot, preventing it from obscuring data.
2. rotate_discrete_xticks() - Rotate X-Axis Labels¶
Rotates x-axis labels for better readability with long category names.
3. grab_legend() - Extract Legend to Separate Figure¶
The grab_legend() function extracts a legend to a separate figure, useful for creating independent legend files for publications.
New in this version: The remove parameter controls whether the legend is removed from the original axes:
remove=True(default): Legend is removed from the original plotremove=False: Legend remains on the original plot
Original plot still has legend: True
4. get_density() - Compute Point Density¶
Computes the local density of points for coloring scatter plots by concentration.
5. shuffle() - Randomize Data Order¶
Shuffles various data types (DataFrames, arrays, lists) to prevent plotting artifacts from data ordering.
Shuffle also works with: - NumPy arrays: <class 'numpy.ndarray'> - Lists: <class 'list'> - Tuples: <class 'tuple'>
6. number_to_color() - Map Values to Colors¶
Maps numeric values to colors from a palette, supporting both continuous and discrete binning.
Combined Example: Density + Shuffle + Number to Color¶
Combining multiple helper functions for a publication-quality visualization.
7. eject_colorbar() - Add Colorbar Outside Plot¶
Creates a colorbar placed outside the plot area for images or colored scatter plots.
If a colorbar already exists for the mappable, it is removed and recreated.
Analogous to eject_legend() but for colorbars.
8. grab_colorbar() - Extract Colorbar to Separate Figure¶
Extracts a colorbar into a standalone figure, useful for creating separate colorbar files
for publications or pairing a single colorbar with multiple panels.
Analogous to grab_legend() but for colorbars.
remove=True(default): Colorbar is removed from the original figureremove=False: Colorbar remains on the original figureorientation='vertical'or'horizontal': Controls orientation in the new figure
/Users/austinv11/PycharmProjects/PyBuenColors/src/buencolors/helpers.py:309: UserWarning: Adding colorbar to a different Figure <Figure size 800x600 with 1 Axes> than <Figure size 40x462 with 1 Axes> which fig.colorbar is called on. cb = fig.colorbar(mappable, cax=cb_ax_new, orientation=orientation)
Original still has colorbar: <Axes.ArtistList of 1 collections>
/Users/austinv11/PycharmProjects/PyBuenColors/src/buencolors/helpers.py:309: UserWarning: Adding colorbar to a different Figure <Figure size 800x600 with 2 Axes> than <Figure size 40x462 with 1 Axes> which fig.colorbar is called on. cb = fig.colorbar(mappable, cax=cb_ax_new, orientation=orientation)
/Users/austinv11/PycharmProjects/PyBuenColors/src/buencolors/helpers.py:309: UserWarning: Adding colorbar to a different Figure <Figure size 800x600 with 1 Axes> than <Figure size 150x462 with 1 Axes> which fig.colorbar is called on. cb = fig.colorbar(mappable, cax=cb_ax_new, orientation=orientation)
Summary¶
The BuenColors helper functions provide:
eject_legend()- Move legends outside plotsrotate_discrete_xticks()- Rotate axis labels for readabilitygrab_legend()- Extract legends to separate figuresget_density()- Compute point density for scatter plotsshuffle()- Randomize data to prevent plotting artifactsnumber_to_color()- Map numeric values to colorseject_colorbar()- Add a colorbar outside the plot areagrab_colorbar()- Extract colorbars to separate figures
These functions work seamlessly together to create publication-quality visualizations!