Skip to content

Barplot

Barplot is deprecated, use barhplot instead.

Barplot

Barplot is deprecated, use Barhplot instead

BarChartRace module that requires a valid time index.The data should be in this format where time is set to index

    Example:
    >>> time  col1 col2 col3 ...
    >>> 2012   1    0    2
    >>> 2013   2    3    1

Parameters:

Name Type Description Default
data pd.DataFrame

The data to be prepared

required
time_format str

Index datetime format

required
ip_freq str

Interpolation frequency

required
ip_frac float

Interpolation fraction (check end of docstring), by default 0.5

0.5
n_bars int

Number of bars to be visible on the plot, by default 10 or less

10
palettes list[str]

List of color palettes to generate bar colors, by default ["viridis"]

['viridis']
post_update Callable[[plt.Axes, int, Datafier, SimpleNamespace], None]

callback function for additional customization, by default None

None
annot_bars bool

Sets bar annotations, by default True

True
fixed_xlim bool

If False xlim will gradually change in every frame, by default True

True
xticks bool

Sets xticks, by default True

True
yticks bool

Sets yticks, by default True

True
grid bool

Sets xgrid, by default True

True
rounded_edges bool

Sets rounded bar edges, by default False

False

post_update args:

    plt.Axes: The matplotlib Axes used for the barplot
    int: Current animation frame or dataframe row
    Datafier: The underlying datafier instance
    SimpleNamespace: Contains the following attributes -
        bar_rank, bar_length, top_bars, bar_colors

example:

>>> def post_update(ax, i, datafier, bar_attr):
>>>     # sets log scale for x-axis
>>>     ax.set_xscale("log")
ip_frac description:
    ip_frac is the percentage of NaN values to be linearly
    interpolated for column ranks

    Consider this example
    >>>               a    b
    >>> date
    >>> 2021-11-13  1.0  4.0
    >>> 2021-11-14  NaN  NaN
    >>> 2021-11-15  NaN  NaN
    >>> 2021-11-16  NaN  NaN
    >>> 2021-11-17  NaN  NaN
    >>> 2021-11-18  2.0  6.0

    with ip_frac set to 0.5, 50% of NaN's will be linearly
    interpolated while the rest will back filled.

    >>>              a      b
    >>> 2021-11-13  1.00  4.00  << original value --------
    >>> 2021-11-14  1.33  4.67                            |
    >>> 2021-11-15  1.67  5.33                            |  50% linearly
    >>> 2021-11-16  2.00  6.00  <- linear interpolation   |  interpolated
    >>> 2021-11-17  2.00  6.00      upto here             |  rest are filled.
    >>> 2021-11-18  2.00  6.00  << original value---------

    This adds some stability in the barChartRace
    and reduces constantly shaking of bars.

add_var(row_var=None, col_var=None)

Adds additional variables to the data, both row and column wise.

Row wise data format: The index should be equal to that of the actual data

    time  leap_year col2   ...
    2012    yes      0
    2013    no       3
Column wise data format: The index should be equal to the columns of the actual data.
    index  continent   col2 ...
    ind    Asia         0
    usa    N America    3
    jap    Asia         2

Parameters:

Name Type Description Default
row_var pd.DataFrame

Dataframe containing variables related to time, by default None

None
col_var pd.DataFrame

Dataframe containing variables related to columns, by default None

None

set_bar_color(colors)

If colors is a list, length of colors should be equal to no of datafier.bar_colors. If it is a dict, all columns of datafier.top_cols should be mapped to a color

Parameters:

Name Type Description Default
colors Union[list, dict[str, str]]

list of colors or dict of column to color mapping

required

getTopXY(i)

Prepares top n_bar columns and their respective attributes such as position, length, colors. Not meant to be used outside animation update

Parameters:

Name Type Description Default
i int

Animation frame index

required

Returns:

Type Description
SimpleNamespace

Bar rank, length. Top columns and their respective colors

set_xylim(xlim=[], ylim=[])

Sets xlim and ylim

Parameters:

Name Type Description Default
xlim list[float]

x axis limits in this format [min, max], by default [min, max + 5]

[]
ylim list[float]

y axis limits in this format [min, max], by default [0.5, n_bars + 0.6]

[]

set_title(title, x=0, y=1.01, size=13, color='#777777', **kwargs)

Sets the plot title and additional kwargs are passed to plt.text(**kwargs)

Parameters:

Name Type Description Default
title str

Title text

required
x float

x coordinate of the text, by default 0

0
y float

y coordinate, by default 1.01

1.01
size float

text size, by default 13

13
color str

text color, by default "#777777"

'#777777'

set_xlabel(text, x=0.43, y=-0.09, size=13, color='#777777', **kwargs)

Sets the plot xlabel and additional kwargs are passed to plt.text(**kwargs)

Parameters:

Name Type Description Default
text str

The xlabel text

required
x float

X coordinate of the text, by default 0.43

0.43
y float

Y coordinate, by default -0.09

-0.09
size float

Text size, by default 13

13
color str

Text color, by default "#777777"

'#777777'

set_time(callback=lambda i, datafier: datafier.data.index[i], x=0.97, y=0.27, size=46, weight=800, ha='right', color='#777777', **kwargs)

Annotates the time in the plot and additional kwargs are passed to plt.text(**kwargs)

Parameters:

Name Type Description Default
callback Callable[[int, pd.DataFrame], str]

Callback function to customize the time text, by default lambda i, datafier: datafier.data.index[i]

lambda i, datafier: datafier.data.index[i]
x float

x coordinate of the text, by default 0.97

0.97
y float

y coordinate of the text, by default 0.27

0.27
size float

text size, by default 46

46
weight float

text weight, by default 800

800
ha str

horizontal alignment, by default "right"

'right'
color str

text color, by default "#777777"

'#777777'

callback args:

    i: Animation frame / data row index
    datafier: The datafier instance,
        access the data using datafier.data

set_text(key, text=None, callback=None, x=0, y=0, size=13, color='#777777', **kwargs)

General function to add custom texts in the plot. Either text or callback should be passd but not both.

Parameters:

Name Type Description Default
key str

Unique identifier for each texts, note: These keys, title, xlabel, time, are reserved. overwrite them if you wish to use callbacks instead of texts in title or xlabel

required
text str

The text to be added in the plot, by default None

None
callback Callable[[int, pd.DataFrame], str]

Callback function to customize the text, by default None

None
x float

X coordinate of the text, by default 0

0
y str

Y coordinate of the text, by default 0

0
size float

Text size, by default 13

13
color str

Text color, by default "#777777"

'#777777'

Callback args:

    args:
    i: Animation frame / data row index
    datafier: The datafier instance

    Example:
    >>> lambda i, datafier: datafier.data.index[i]

remove_text(keys)

Removes texts by key

Parameters:

Name Type Description Default
keys list[str]

List of keys to be removed

required

set_bar_border_props(edge_color='k', radius=0.5, pad=-0.004, mutation_aspect=0.2, **kwargs)

Sets bar border properties. Additional kwargs are passed to FancyBboxPatch. See https://matplotlib.org/3.1.0/api/_as_gen/matplotlib.patches.FancyBboxPatch.html

Parameters:

Name Type Description Default
edge_color str

Bar edge color, by default "k"

'k'
radius float

Bar border radius, by default 0.5

0.5
pad float

See above link, by default -0.0040

-0.004
mutation_aspect float

See above link, by default 0.2

0.2

set_barh(bar_height=0.86, **kwargs)

Sets barh properties, addition kwargs are passed to ax.barh(**kwargs)

Parameters:

Name Type Description Default
bar_height float

Height of the bars (Note this is horizontal barplot), by default 0.86

0.86

set_xticks(axis='x', colors='#777777', labelsize=12, **kwargs)

Sets xtick properties, additional kwargs are passed to ax.tick_params(**.kwargs)

Parameters:

Name Type Description Default
axis str

Defines tick axis, by default "x"

'x'
colors str

Sets tick color, by default "#777777"

'#777777'
labelsize float

Sets tick size, by default 12

12

set_yticks(axis='y', colors='#777777', labelsize=10, **kwargs)

Sets ytick properties, additional kwargs are passed to ax.tick_params(**kwargs)

Parameters:

Name Type Description Default
axis str

Defines tick axis, by default "y"

'y'
colors str

Sets tick color, by default "#777777"

'#777777'
labelsize float

Sets tick size, by default 10

10

set_grid(which='major', axis='x', linestyle='-', grid_behind=True, **kwargs)

Sets the plots grid, additional kwargs are passed to ax.grid(**kwargs)

Parameters:

Name Type Description Default
which str

The grid lines to apply the changes on, by default "major"

'major'
axis str

Sets the axis of the grid, by default "x"

'x'
linestyle str

Grids line style, by default "-"

'-'
grid_behind bool

Sets the grid behind the bars, by default True

True

set_bar_annots(text_callback=lambda val: np.round(val, 2), xoffset=0.1, yoffset=-0.1, ha='left', **kwargs)

Sets bar annotation properties, additional kwargs are passed to ax.text(**kwargs). (Note these annotations are the texts near the bars)

Parameters:

Name Type Description Default
text_callback Callable[[float], Union[str, float]]

Callback function for customizing the text, by default lambda val:np.round(val, 2)

lambda val: np.round(val, 2)
xoffset float

X offset relative to bar length, by default 0.1

0.1
yoffset float

Y offset relative to bar height, by default -0.1

-0.1
ha str

Horizontal alignment, by default "left"

'left'

add_extras(key, callback)

Adds extra callback functions for additional customizations

Parameters:

Name Type Description Default
key str

Unique identifier for each callback function

required
callback list[Callable[[plt.Axes, int, pd.DataFrame, pd.DataFrame], None]]

Callback function for additional customization

required

Callback args:

    plt.Axes: The matplotlib Axes used for the barplot
    int: Current animation frame / dataframe row
    Datafier: The underlying datafier instance
    SimpleNamespace: Contains the following attributes -
    bar_rank, bar_length, top_bars, bar_colors

    Example:
    >>> lambda ax, *args: ax.set_xcale("log)