使用Seaborn的对象接口(v0.12)绘制水平条形图。

huangapple go评论70阅读模式
英文:

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

问题

在原始的API中,barplot 提供了一个 orient 参数来交换柱状图的方向:

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

df = pd.DataFrame(data=np.random.randint(10, size=(3, 5)), columns=["a", "b", "c", "d", "e"])
#    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") 

objects API 中,似乎现在应该在标记层中 add 方向,但我不确定如何做:

import seaborn.objects as so

so.Plot(data=df).add(so.Bar(), so.Agg(), orient="h")
# ValueError: 数据框中不存在分组变量

使用新的 objects API 更改柱状图方向的习惯方式是什么?

英文:

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") 

<a href="https://i.stack.imgur.com/Bn3oD.png"><img src="https://i.stack.imgur.com/Bn3oD.png" width="220"></a>

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=&quot;h&quot;)
# ValueError: No grouping variables are present in dataframe

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

答案1

得分: 3

你可以使用对象接口绘制水平条形图,add函数确实具有一个orient参数(尽管它接受x/y而不是v/h,因为前者在所有种类的标记中更通用)。

但目前不支持的是绘制“宽格式”数据。

宽格式数据的问题在于不同种类的图会以不同的方式使用数据集的维度。在函数接口中,每种函数类型可以指定映射(例如,barplot将列映射到x(或水平条形图的y),而lineplot将索引映射到x

我不知道对象接口是否将来会支持这一点,但目前还不支持。你需要将你的数据框转换为长格式,并显式分配x/y。

英文:

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.

答案2

得分: 1

为了保持完整性,以复制原始的水平 barplot 对于宽格式数据的行为:

# 旧接口
sns.barplot(data=df, orient="h")

现在,我们需要将数据melt成长格式,并明确交换 x/y

# 新接口
so.Plot(
    data=df.melt(), # 转换为长格式
    x="value",      # 将 x 设置为数值
    y="variable",   # 将 y 设置为分类
).add(
    so.Bar(),
    so.Agg(),
)
英文:

For completeness, to replicate the original horizontal barplot behavior for wide-form data:

# old interface
sns.barplot(data=df, orient=&quot;h&quot;)

We now need to melt into long-form and explicitly swap x/y:

# new interface
so.Plot(
    data=df.melt(), # convert to long-form
    x=&quot;value&quot;,      # set x as numeric
    y=&quot;variable&quot;,   # set y as categorical
).add(
    so.Bar(),
    so.Agg(),
)

<a href="https://i.stack.imgur.com/lH1fy.png"><img src="https://i.stack.imgur.com/lH1fy.png" width="300"></a>

huangapple
  • 本文由 发表于 2023年3月7日 06:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定