Skip to content

Bar

model_feature_importances(X_test, model, title='Feature importance', horizontal=True, size=None, show=True, write_html_path=None)

Creates a bar chart with the feature importance of a model.

Example: ```python from blitzly.plots.bar import model_feature_importance import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.datasets import make_classification from sklearn.model_selection import train_test_split

X, y = make_classification( n_samples=100, n_features=4, n_informative=2, n_redundant=0, random_state=42, shuffle=False, )

X = pd.DataFrame(X, columns=["foo", "bar", "blitz", "licht"]) y = pd.Series(y)

X_train, X_test, y_train, _ = train_test_split(X, y)

model = RandomForestClassifier() model.fit(X_train, y_train)

model_feature_importances(X_test, model)

Args: X_test (pd.DataFrame): The test data for the model. You can also use the train data but it is recommend to use test. model (Any): The model to get the feature importance from. The model must have a feature_importances_ attribute! title (Optional[str]): The title of the plot. Defaults to "Feature importance". horizontal (bool): Whether to plot the bar chart horizontally or vertically. size (Optional[Tuple[int, int]]): The size of the plot. show (bool): Whether to show the plot. write_html_path (Optional[str]): The path to write the plot as HTML.

Raises: AttributeError: If the model does not have a feature_importances_ attribute.

Returns: BaseFigure: The plotly figure.

Source code in blitzly/plots/bar.py
def model_feature_importances(
    X_test: pd.DataFrame,
    model: Any,
    title: str = "Feature importance",
    horizontal: bool = True,
    size: Optional[Tuple[int, int]] = None,
    show: bool = True,
    write_html_path: Optional[str] = None,
) -> BaseFigure:
    """Creates a bar chart with the feature importance of a model.

    Example:
    ```python
    from blitzly.plots.bar import model_feature_importance
    import pandas as pd
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.datasets import make_classification
    from sklearn.model_selection import train_test_split

    X, y = make_classification(
        n_samples=100,
        n_features=4,
        n_informative=2,
        n_redundant=0,
        random_state=42,
        shuffle=False,
    )

    X = pd.DataFrame(X, columns=["foo", "bar", "blitz", "licht"])
    y = pd.Series(y)

    X_train, X_test, y_train, _ = train_test_split(X, y)

    model = RandomForestClassifier()
    model.fit(X_train, y_train)

    model_feature_importances(X_test, model)

    Args:
        X_test (pd.DataFrame): The test data for the model. You can also use the `train` data but it is recommend to use `test`.
        model (Any): The model to get the feature importance from. The model must have a `feature_importances_` attribute!
        title (Optional[str]): The title of the plot. Defaults to "Feature importance".
        horizontal (bool): Whether to plot the bar chart horizontally or vertically.
        size (Optional[Tuple[int, int]]): The size of the plot.
        show (bool): Whether to show the plot.
        write_html_path (Optional[str]): The path to write the plot as HTML.

    Raises:
        AttributeError: If the model does not have a `feature_importances_` attribute.

    Returns:
        BaseFigure: The plotly figure.
    """

    if hasattr(model, "feature_importances_"):
        df = pd.DataFrame(
            {"feature": X_test.columns, "importance": model.feature_importances_}
        ).sort_values("importance", ascending=True)
    else:
        raise AttributeError(
            "The model does not have a `feature_importances_` attribute!"
        )

    if horizontal:
        fig = px.bar(df, x="importance", y="feature")
    else:
        fig = px.bar(df, x="feature", y="importance")

    fig = update_figure_layout(fig, title, size)
    return save_show_return(fig, write_html_path, show)

multi_bar(data, group_labels, x_labels, mark_x_labels=None, mark_x_label_color='crimson', title='Bar chart', stack=False, text_position='none', hover_texts=None, errors=None, size=None, show_legend=True, show=True, write_html_path=None)

Creates a bar chart with multiple groups. Each group is represented by a bar. The bars are grouped by the x-axis. The number of group_labels must be equal to the number of rows in the data. The number of x_labels must be equal to the number of columns in the data.

Example:

from blitzly.plots.bar import multi_bar
import numpy as np

data=np.array([[1, 2, 3], [4, 5, 6]])
errors=np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]),

multi_bar(
    data,
    x_labels=["X1", "X2", "X3"],
    group_labels=["Z1", "Z2"],
    hover_texts=["foo", "bar", "blitzly"],
    errors=errors
)

Parameters:

Name Type Description Default
data Union[pd.DataFrame, pd.Series, NDArray]

The data to plot.

required
group_labels List[str]

The labels for the groups.

required
x_labels List[str]

The labels for the x-axis.

required
mark_x_labels Optional[List[str]]

The bars of x_label which should be marked.

None
mark_x_label_color str

The color of the marked bars.

'crimson'
title Optional[str]

The title of the bar chart.

'Bar chart'
stack Optional[bool]

Whether to stack the bars. Values are summed up by columns. By default, the bars are grouped. Stacked bars don't support errors. If provided, they will be ignored.

False
text_position Optional[str]

The position of the text. Can be "auto", "inside", "outside", "none".

'none'
hover_texts Optional[List[str]]

The hover texts for the data.

None
errors Optional[Union[pd.DataFrame, pd.Series, NDArray]]

The errors for the data.

None
size Optional[Tuple[int, int]

Size of the plot.

None
show_legend Optional[bool]

Whether to show the legend.

True
show bool

Whether to show the figure.

True
write_html_path Optional[str]

The path to which the histogram should be written as an HTML file. If None, the histogram will not be saved.

None

Returns:

Name Type Description
BaseFigure BaseFigure

The figure.

Source code in blitzly/plots/bar.py
def multi_bar(
    data: Union[pd.DataFrame, pd.Series, NDArray],
    group_labels: List[str],
    x_labels: List[str],
    mark_x_labels: Optional[List[str]] = None,
    mark_x_label_color: str = "crimson",
    title: str = "Bar chart",
    stack: bool = False,
    text_position: str = "none",
    hover_texts: Optional[List[str]] = None,
    errors: Optional[Union[pd.DataFrame, pd.Series, NDArray]] = None,
    size: Optional[Tuple[int, int]] = None,
    show_legend: bool = True,
    show: bool = True,
    write_html_path: Optional[str] = None,
) -> BaseFigure:
    """Creates a bar chart with multiple groups. Each group is represented by a
    bar. The bars are grouped by the x-axis. The number of `group_labels` must
    be equal to the number of rows in the data. The number of `x_labels` must
    be equal to the number of columns in the data.

    Example:
    ```python
    from blitzly.plots.bar import multi_bar
    import numpy as np

    data=np.array([[1, 2, 3], [4, 5, 6]])
    errors=np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]),

    multi_bar(
        data,
        x_labels=["X1", "X2", "X3"],
        group_labels=["Z1", "Z2"],
        hover_texts=["foo", "bar", "blitzly"],
        errors=errors
    )
    ```

    Args:
        data (Union[pd.DataFrame, pd.Series, NDArray]): The data to plot.
        group_labels (List[str]): The labels for the groups.
        x_labels (List[str]): The labels for the x-axis.
        mark_x_labels (Optional[List[str]]): The bars of `x_label` which should be marked.
        mark_x_label_color (str): The color of the marked bars.
        title (Optional[str]): The title of the bar chart.
        stack (Optional[bool]): Whether to stack the bars. Values are summed up by columns.
            By default, the bars are grouped. Stacked bars don't support errors. If provided, they will be ignored.
        text_position (Optional[str]): The position of the text. Can be "auto", "inside", "outside", "none".
        hover_texts (Optional[List[str]]): The hover texts for the data.
        errors (Optional[Union[pd.DataFrame, pd.Series, NDArray]]): The errors for the data.
        size (Optional[Tuple[int, int]): Size of the plot.
        show_legend (Optional[bool]): Whether to show the legend.
        show (bool): Whether to show the figure.
        write_html_path (Optional[str]): The path to which the histogram should be written as an HTML file.
            If None, the histogram will not be saved.

    Returns:
        BaseFigure: The figure.
    """

    data = check_data(data)

    if isinstance(errors, np.ndarray):
        errors = check_data(errors)

    _check_data_ready_for_bar(data, group_labels, x_labels, hover_texts, errors)

    colors = None
    if mark_x_labels:
        colors = [
            "lightslategray",
        ] * len(x_labels)
        for mark in mark_x_labels:
            colors[x_labels.index(mark)] = mark_x_label_color

    fig = go.Figure()
    for idx, dt in enumerate(data):
        error_dict = None
        if isinstance(errors, np.ndarray) and stack is False:
            error = errors[idx,]
            error_dict = dict(type="data", array=error, visible=True)
        fig.add_trace(
            go.Bar(
                name=group_labels[idx],
                x=x_labels,
                y=dt,
                error_y=error_dict,
                hovertext=hover_texts,
                text=dt,
                textposition=text_position,
                marker_color=colors,
            )
        )

    fig.update_layout(barmode="stack" if stack else "group", xaxis_tickangle=-45)

    fig = update_figure_layout(
        fig, title, size, (show_legend and mark_x_labels is None)
    )
    return save_show_return(fig, write_html_path, show)