Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addressing issue #525: Added helper for stacked #870

Merged
merged 7 commits into from
Jun 4, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 73 additions & 1 deletion yellowbrick/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@

from .base import Visualizer
from .exceptions import YellowbrickValueError
from .style.colors import resolve_colors

from matplotlib import patches

import matplotlib.pyplot as plt

import numpy as np

##########################################################################
## Legend Drawing Utilities
Expand Down Expand Up @@ -89,3 +90,74 @@ def manual_legend(g, labels, colors, **legend_kwargs):
# Return the Legend artist
return g.legend(handles=handles, **legend_kwargs)


def bar_stack(data, ax=None, labels=None, ticks=None, colors=None,
orientation='vertical', colormap=None, **kwargs):
"""
An advanced bar chart plotting utility that can draw bar and stacked bar charts from
data, wrapping calls to the specified matplotlib.Axes object.

Parameters
----------
data : 2D array-like
The data associated with the bar chart where the columns represent each bar
and the rows represent each stack in the bar chart. A single bar chart would
be a 2D array with only one row, a bar chart with three stacks per bar would
have a shape of (3, b).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might need to make this a bit more readable and understandable. @DistrictDataLabs/team-oz-maintainers does this make sense to you? Any thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked it up from the #525. It had a basic function signature. We can change this if you suggest.


ax : matplotlib.Axes, default: None
The axes object to draw the barplot on, uses plt.gca() if not specified.

labels : list of str, default: None
The labels for each row in the bar stack, used to create a legend.

ticks : list of str, default: None
The labels for each bar, added to the x-axis for a vertical plot, or the y-axis
for a horizontal plot.

colors : array-like, default: None
Specify the colors of each bar, each row in the stack, or every segment.

colormap : string or matplotlib cmap
Specify a colormap for each bar, each row in the stack, or every segment.

kwargs : dict
Additional keyword arguments to pass to ``ax.bar``.
"""
if ax is None:
ax = plt.gca()

colors = resolve_colors(n_colors=data.shape[0],
colormap=colormap,
colors=colors)

idx = np.arange(data.shape[1])
prev = np.zeros(data.shape[1])
if orientation == 'vertical':
bbengfort marked this conversation as resolved.
Show resolved Hide resolved
for index,element in enumerate(data):
bbengfort marked this conversation as resolved.
Show resolved Hide resolved
ax.bar(idx,
element,
bottom = prev,
color = colors[index])
prev+=element
ax.set_xticks(idx)
if ticks is not None:
ax.set_xticklabels(ticks, rotation=90)

if orientation == 'horizontal':
for index,element in enumerate(data):
ax.barh(idx,
element,
left = prev,
color = colors[index])
prev+=element
ax.set_yticks(idx)
if ticks is not None:
ax.set_yticklabels(ticks)

# Generates default labels is labels are not specified.
if labels is None:
labels = np.arange(data.shape[0])
bbengfort marked this conversation as resolved.
Show resolved Hide resolved

manual_legend(ax, labels=labels, colors=colors)
return ax