Bar Plot
This module provides flexible functionality for creating bar plots from pandas DataFrames or Series.
It allows you to create bar plots with optional grouping, sorting, orientation, and data labels. The module supports
both single and grouped bar plots, where grouped bars are created by providing a x_col
, which defines the x-axis
labels or categories.
Features
- Single or Grouped Bar Plots: Plot one or more value columns (
value_col
) as bars. Thex_col
is used to define categories or groups on the x-axis (e.g., products, categories, or regions). Grouped bars can be created by specifying bothvalue_col
(list of columns) andx_col
. - Sorting and Orientation: Customize the sorting of bars (ascending or descending) and choose between vertical (
"v"
,"vertical"
) or horizontal ("h"
,"horizontal"
) bar orientations. - Data Labels: Add data labels to bars, with options to show absolute values or percentages.
- Hatching Patterns: Apply hatch patterns to the bars for enhanced visual differentiation.
- Legend Customization: Move the legend outside the plot for better visibility, especially when dealing with grouped bars or multiple value columns.
Use Cases
- Sales and Revenue Analysis: Visualize sales or revenue across different products or categories by creating grouped
bar plots (e.g., revenue across quarters or regions). The
x_col
will define the products or categories displayed on the x-axis. - Comparative Analysis: Compare multiple metrics simultaneously by plotting grouped bars. For instance, you can
compare product sales for different periods side by side, with
x_col
defining the x-axis categories. - Distribution Analysis: Visualize the distribution of categorical data (e.g., product sales) across different
categories, where
x_col
defines the x-axis labels.
Limitations and Handling of Data
- Series Support: The module can also handle pandas Series, though
x_col
cannot be provided when plotting a Series. In this case, the index of the Series will define the x-axis labels.
plot(df, value_col=None, x_col=None, title=None, x_label=None, y_label=None, legend_title=None, ax=None, source_text=None, move_legend_outside=False, orientation='vertical', sort_order=None, data_label_format=None, use_hatch=False, num_digits=3, **kwargs)
Creates a customizable bar plot from a DataFrame or Series with optional features like sorting, orientation, and adding data labels. Grouped bars can be created with the use of a grouping column.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame | Series
|
The input DataFrame or Series containing the data to be plotted. |
required |
value_col |
str | list[str]
|
The column(s) containing values to plot as bars. Multiple value columns create grouped bars. Defaults to None. |
None
|
x_col |
str
|
The column to group data by, used for grouping bars. Defaults to None. |
None
|
title |
str
|
The title of the plot. Defaults to None. |
None
|
x_label |
str
|
The label for the x-axis. Defaults to None. |
None
|
y_label |
str
|
The label for the y-axis. Defaults to None. |
None
|
legend_title |
str
|
The title for the legend. Defaults to None. |
None
|
ax |
Axes
|
The Matplotlib Axes object to plot on. Defaults to None. |
None
|
source_text |
str
|
Text to be displayed as a source at the bottom of the plot. Defaults to None. |
None
|
move_legend_outside |
bool
|
Whether to move the legend outside the plot area. Defaults to False. |
False
|
orientation |
Literal['horizontal', 'h', 'vertical', 'v']
|
Orientation of the bars. Can be "horizontal", "h", "vertical", or "v". Defaults to "vertical". |
'vertical'
|
sort_order |
Literal['ascending', 'descending'] | None
|
Sorting order for the bars. Can be "ascending" or "descending". Defaults to None. |
None
|
data_label_format |
Literal['absolute', 'percentage'] | None
|
Format for displaying data labels. "absolute" shows raw values, "percentage" shows percentage. Defaults to None. |
None
|
use_hatch |
bool
|
Whether to apply hatch patterns to the bars. Defaults to False. |
False
|
num_digits |
int
|
The number of digits to display in the data labels. Defaults to 3. |
3
|
**kwargs |
dict[str, any]
|
Additional keyword arguments for the Pandas |
{}
|
Returns:
Name | Type | Description |
---|---|---|
SubplotBase |
SubplotBase
|
The Matplotlib Axes object with the generated plot. |
Source code in pyretailscience/plots/bar.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|