Skip to content

Blog - bar#25

Open
kdorr wants to merge 3 commits into
matplotlib:gh-pagesfrom
kdorr:blog-bar
Open

Blog - bar#25
kdorr wants to merge 3 commits into
matplotlib:gh-pagesfrom
kdorr:blog-bar

Conversation

@kdorr
Copy link
Copy Markdown
Collaborator

@kdorr kdorr commented Aug 15, 2018

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

Comment thread _posts/altair-to-mpl-bar.md Outdated
## 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
Copy Markdown
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)

Comment thread _posts/altair-to-mpl-bar.md Outdated
fig, ax = plt.subplots()

(_, a), (_, b) = df.groupby('variable')
ax.bar(a['group'], a['scores'], label='a')
Copy link
Copy Markdown
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