Plot horizontal bars using seaborn.objects interface (v0.12)

Question:

In the original API, barplot provided an orient parameter to swap bar orientation:

import pandas as pd, numpy as np, seaborn as sns

df = pd.DataFrame(data=np.random.randint(10, size=(3, 5)), columns=[*"abcde"])
#    a  b  c  d  e
# 0  8  6  5  2  3
# 1  0  0  0  1  8
# 2  6  9  5  6  9

sns.barplot(data=df, orient="h") 

In the objects API, it seems we should now add the orientation to the mark layer, but I’m not sure how:

import seaborn.objects as so

so.Plot(data=df).add(so.Bar(), so.Agg(), orient="h")
# ValueError: No grouping variables are present in dataframe

What is the idiomatic way to change bar orientation with the new objects API?

Asked By: tdy

||

Answers:

You can draw horizontal bars with the objects interface and add does have an orient parameter (although it accepts x/y rather than v/h as the former generalize better over all kinds of marks).

But what is not currently supported here is plotting "wide form" data.

The problem with wide-form data is that different kinds of plots will use the dimensions of the dataset differently. With the function interface, each function kind can specify the mapping (e.g. barplot maps columns to x (or y for horizontal bars), while lineplot maps the index to x.

I don’t know whether the objects interface will support this at some point, but it does not currently. You’ll need to convert your dataframe to long-form and explicitly assign x/y.

Answered By: mwaskom