Skip to content

Blog - bar #25

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

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open

Blog - bar #25

wants to merge 3 commits into from

Conversation

kdorr
Copy link
Collaborator

@kdorr kdorr commented Aug 15, 2018

This is the bar chart section from the blog post Nabarun and I were working on.

## Matplotlib
This is a little more complicated in Matplotlib. Since Matplotlib is procedural, we have to manually tell Matplotlib to stack the bars. Also notice that we are calling a new function now (`ax.bar()`) to get a bar plot.

To stack the bars, we have to create new subset dataframes and then plot each one separately (specifying that bottom of one plot should be the top of another `bottom=a_scores`). One way to do this is to subset via indexing (option 1). Another way to do this is to use the `df.groupby()` function (option 2).
Copy link
Member

Choose a reason for hiding this comment

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

Worried that this phrasing may be confusing since the bottom=a uses the top of a as the bottom of b. Maybe just state it explicitly? In this example, we first plot group a. Then we plot group b, using the bottom=a kwarg to plot b starting from the top of each a bar (and yes that can be phrased more cleanly too)

fig, ax = plt.subplots()

(_, a), (_, b) = df.groupby('variable')
ax.bar(a['group'], a['scores'], label='a')
Copy link
Member

Choose a reason for hiding this comment

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

This might be clearer as

bottom = np.zeros(len(a['group'])
for label, scores in df.groupby('variable');
    ax.bar(scoren['group'], scores['scores'], bottom=bottom, label=label)
    bottom += scores['scores']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants